0
MyUserID

C Program Homework Help??

Recommended Posts

Ok,

I have compiled my program, and it runs, but it isnt doing what it should be. Basicaly, I'm suppose to get a list of numbers from a file and dump them into an array and then calculate the contents and stuff. I can work with the arrays fine when they have the right data in them. I think my problem is taking the information from the file and putting it into the array. The file is basically just a list of numbers. This is what I am doing now:

fscanf(inputx,"%d",inputarrayx);

Do I need to put a "&" before the array??

Hopefully somebody knows C programming enough to help me out. B|

-Chris

------------------------------------------------------

Remember kids, eagles may soar, but at least weasels dont get sucked into jet engines.

Share this post


Link to post
Share on other sites
First off it's been years since I looked at a C program (I am a Java developer) and without seeing the rest of the code, it makes things even more obscure. But it looks like you are missing the '&'. The '&' this tells the compiler is that you are specifying the address of the 'inputarrayx' variable not the contents and it's the address which likely needs to be passed in.


Try not to worry about the things you have no control over

Share this post


Link to post
Share on other sites
I tried it again with the '&' and it made absolutely no difference in what the program does. Arrghh! Thanks for the help though. Hopefully somebody else can add some insight. Here is the source code if you want to look further at it.

------------------------------------------------------

Remember kids, eagles may soar, but at least weasels dont get sucked into jet engines.

project4a.c

Share this post


Link to post
Share on other sites
You input method is broken. fscanf only reads one line at a time, at least on my system. So what you need to do is loop and keep a count of what line you are on, then do your fscanf like:

fscanf(inputy,"%d",&inputarrayy[lineNum]); /* hint: start at zero */

There are far more efficient ways to do this. Your method of input takes O(n^2) operations, while it can be easily done in O(n) time.

Share this post


Link to post
Share on other sites
Try this, I use a sequence of fgets() then sscanf():

#include
#define MAX 32

main(){

FILE *file_ptr;
int data[50],counter=0, max_value;
char file_name[] = "data.txt";
char buffer[32];

if(file_ptr = fopen(file_name, "r")){
while(fgets(buffer, MAX, file_ptr)){
sscanf(buffer, "%i", &data[counter++]);
}
}
else{
printf("file is not there\n");
}

max_value = counter;
printf("Contents of array is:\n");

for(counter = 0; counter < max_value; counter++){
printf("%d\n", data[counter]);

}
fclose(file_ptr);
}

The file data.txt is a single column of ints:

22
14
15
35
45
67
999

Compile & run it and see if this is what you want.

-G.L.-

Share this post


Link to post
Share on other sites
I don't write much C anymore but when I did, I didn't use sscanf much.
Wouldn't something like this work?

if(file_ptr = fopen(file_name, "r")){
while(fgets(buffer, MAX, file_ptr)){
data[counter++] = atoi(buffer);
}
}

Now days I'd do it like this: (PERL)
while(<>) {
print;
chomp;
$arr[$i]=$_;
$total=$total+$arr[$i++];
}
print "Total=$total\n";
-Carbon

Share this post


Link to post
Share on other sites
See if this is what you are trying to do:

#include
#include
#include

void main(void)
{

int iDone = 0;
int iCounter = 0;
FILE *inputx;
char inputarrayx[20];
char buf[5];

inputx = fopen("datax.txt", "r");

while(!iDone)
{
if(feof(inputx))
{
iDone = 1;
break;
}
else
{
memset(buf, '\0', sizeof(buf));
fgets(buf, 5, inputx);
inputarrayx[iCounter] = atoi(buf);
printf("%i\n", inputarrayx[iCounter]);
iCounter++;
}
}

fclose(inputx);
}
My other ride is the relative wind.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0