Need help with arrays 
Author Message
 Need help with arrays

Quote:

>int main ()
>{
>int     acct_num[10],i  /*account number*/;
>int     day_o[10],z             /*days overdue*/;
>int     count           /*counter*/;
>int     balance         /*balance*/;
>int     tot_bal         /*total of all balances*/;
>int     hi_bal          /*highest balance*/;
>int     hi_bal_acct     /*highest balance account number*/;
>int     lo_bal          /*lowest balance*/;
>int     lo_bal_acct     /*lowest balance account number*/;
>int     avg             /*average of balances*/;
>FILE    *book_data      /*pointer to book.data file*/;

>book_data = fopen ("book.data","r");
>i = 0;
>z = 0;
>printf ("%Account Number%20sDays Overdue%20sBalance Due\n\n", " ");  
>fscanf (book_data,"%d%d",&acct_num[i],&day_o[z]);
>while (i <= 10)
>{
>printf ("%d%d\n",acct_num[i],day_o[z]);
>i += 1;
>}
>return 0;
>}          
>I'm having trouble with getting the information from the file book_data to
>load
>onto the array.  Any help would be appreciated.

Some of the problems in this program are:

fscanf() is only being called once, so it only reads the first two numbers
from the file.  The call to fscanf() should be within a loop.

You increment i, but neglected to increment z.  Since these variables
should always have the same value, you don't need z - just use i for
indexing both arrays.

You have
   i = 0;
Followed by a few other statements, then
   while (i <= 0)
and inside the while loop
   i += 1;
Although this will work, it would be MUCH better to use a 'for' loop.
The initialization, test, and update of i would be on one line,
instead of spread out through the program.  The program would be
easier to read; more importantly, it would be less likely that future
modifications to the program would accidentally break it.
The thing I find particularly unnerving about what you have now is
that after you initialize i in preparation for the loop, you do several
other things before entering the loop.  In the future, someone might
change those intervening lines to something that modifies i, without
noticing that doing so ruins the loop.  (I realize that's not really
going to happen because this is a homework problem, but
homework problems are for learning, and it's better to learn good
habits than bad ones.)
If you don't change to a 'for' loop, then at the very least,
   i = 0;
should be moved to the line _immediately_ before
   while (i <= 10)

You don't check the return status from any of the library calls.
That's OK for printf().  Missing, misplaced, or misnamed data
files are _very_ common problems, so you should _always_
check the result from fopen():
   book_data = fopen ("book.data","r");
   if (book_data == NULL) {

      /* output informative message here with perror(), printf(), or
fprintf(stderr, ) */

      return EXIT_FAILURE;
   }
You should check the results from fscanf() as well.  I never use
fscanf(), so I will leave it to you or another respondent to figure
out the details.

Since you use standard library functions, you should include the
corresponding standard header file:
   #include <stdio.h>
If you use my suggested code above for checking the return value
from fopen(), you will need the definition of EXIT_FAILURE, so
you will also need:
   #include <stdlib.h>

Finally a couple of style tips:

You have:
   i += 1;
which is OK, but
  i++;
or
  ++i;
is generally considered better.

A consistent indenting scheme would make the program easier to
read.  (But inconsistent indenting is worse than none, so your
program is a lot easier to follow than some I've seen.)

-- Gary Culp
--



Sun, 17 Feb 2002 03:00:00 GMT  
 Need help with arrays

Quote:

>int main ()
>{
>int     acct_num[10],i  /*account number*/;
>int     day_o[10],z             /*days overdue*/;
>int     count           /*counter*/;
>int     balance         /*balance*/;
>int     tot_bal         /*total of all balances*/;
>int     hi_bal          /*highest balance*/;
>int     hi_bal_acct     /*highest balance account number*/;
>int     lo_bal          /*lowest balance*/;
>int     lo_bal_acct     /*lowest balance account number*/;
>int     avg             /*average of balances*/;
>FILE    *book_data      /*pointer to book.data file*/;

>book_data = fopen ("book.data","r");
>i = 0;
>z = 0;
>printf ("%Account Number%20sDays Overdue%20sBalance Due\n\n", " ");  

This line will give you a lot of trouble. First the line starts with a
non-exestant format specifier (%A), and secondly there are not enough
arguments to satisfy all format specifiers.
Quote:
>fscanf (book_data,"%d%d",&acct_num[i],&day_o[z]);

This reads _one_ element of each array, if you want to read more, put
it in a loop, like the printf statement below.
Quote:
>while (i <= 10)
>{
>printf ("%d%d\n",acct_num[i],day_o[z]);
>i += 1;
>}
>return 0;
>}          
>I'm having trouble with getting the information from the file book_data to load
>onto the array.  Any help would be appreciated.

>thanks
>Jennifer
>--


HTH

Bart v Ingen Schenau
--



Sun, 17 Feb 2002 03:00:00 GMT  
 Need help with arrays

Quote:

> int main ()
> {
> int     acct_num[10],i  /*account number*/;
> int     day_o[10],z             /*days overdue*/;
> int     count           /*counter*/;
> int     balance         /*balance*/;
> int     tot_bal         /*total of all balances*/;
> int     hi_bal          /*highest balance*/;
> int     hi_bal_acct     /*highest balance account number*/;
> int     lo_bal          /*lowest balance*/;
> int     lo_bal_acct     /*lowest balance account number*/;
> int     avg             /*average of balances*/;
> FILE    *book_data      /*pointer to book.data file*/;
> book_data = fopen ("book.data","r");
> i = 0;
> z = 0;
> printf ("%Account Number%20sDays Overdue%20sBalance Due\n\n", " ");
> fscanf (book_data,"%d%d",&acct_num[i],&day_o[z]);
> while (i <= 10)
> {
> printf ("%d%d\n",acct_num[i],day_o[z]);
> i += 1;
> }
> return 0;
> }
> I'm having trouble with getting the information from the file book_data to load
> onto the array.  Any help would be appreciated.
> thanks
> Jennifer

Well, there are some problems here. Minor problems are that you have some
variables
you don't use, but those probably remain from stripping the program.
Furthermore,
you don't check if fopen succeeds - test if book_data == NULL.

A somewhat more serious problem is in your design. Apparently for each acct_num
there should be a day_o. That should have been expressed as
struct account{
        int acct_num;
        int day_o;

Quote:
};

This (probably) is a design error, not a C error.

The use of i and z is not clear to me. Add some comment.

The fscanf reads two variables, acct_num[0] and day_o[0]. I doubt that's
what you intended, since you go on to print 11 accounts. That by itself is
an error, since you only have 10 accounts. If i==10, garbage will be printed
even if you had read 10 accounts. They start at 0.

Try these improvements:

- Check if the file is there
- Make a struct
- Use a single index, and drop z
- Move the read into the loop, so it will be repeated
- Check if the read succeeds
- Only if the read succeeds, print the account data.

HTH, HAND,

Michiel Salters
--



Sun, 17 Feb 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need help Sorting Arrays with an insert function.

2. Need help with Arrays: Specific program question

3. need help with arrays please

4. need help with array of string pointers

5. Need help: Passing array as parameter.

6. need help With Arrays

7. Need Help with array...Please Advice

8. Need help with arrays

9. Aarghh! I need help with array of structs

10. Need help with array's

11. Need help with array of pointers to structure

12. Need help with array realloc's

 

 
Powered by phpBB® Forum Software