Question about arrays 
Author Message
 Question about arrays

Quote:

> Hi

> I need some help .....Iam new at programming.  How do you put the contents of a
> file into a one dimensional parallel array???

> scanf ("%d%d", acct_num,over_due);   ??????

> in the file there are two sets of numbers.  I have to load them into two
> separate arrays....and i have no idea how???? Any help would be appreciated.

Try something like

int i;
int acct_num[n], over_due[n];
FILE *file = fopen(some_file, "r");
for (i = 0; i < n; ++i)
   fscanf(file, "%d %d", acct_num + i, over_due + i);

In this example;
1) some_file is a string that specifies the name of the file to read.
2) n is the number of values you want to read.  For the above code to
     work, n will have to be fixed at compile time.  If n is
     variable (eg read from a file), you will need to use dynamic
     memory allocation instead of hard coded arrays acct_num and
over_due
     [read up on malloc and free functions to learn about this].
3) acct_num + i is a shortcut notation for  &(acct_num[i]).  Similarly
     for over_due + i.  If you have a decent text, this sort of thing
     is described in a section involving equivalence of pointers
     and arrays.

-<Automagically included trailer>
Robert O'Dowd                       Ph    +61 (8) 8259 6546
MOD/DSTO                            Fax    +61 (8) 8259 5139
P.O. Box 1500                       Email:

Salisbury, South Australia, 5108

Disclaimer: Opinions above are mine and may be worth what you paid for
them
--



Sat, 16 Feb 2002 03:00:00 GMT  
 Question about arrays


Quote:
>I need some help .....Iam new at programming.  How do you put the contents of a
>file into a one dimensional parallel array???

>scanf ("%d%d", acct_num,over_due);   ??????

1) look up how to open a file with a file handle
2) look up fscanf (though some would prefer other options such as
combinations of fgets and sscanf)
3) You must know how many data pairs you have, or be able to over
dimensions the arrays, or learn how to use dynamic resources (malloc and
realloc and free)
4) use a loop to step through the addresses of the elements of the
arrays

Now post your best attempt even if it does not work.

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sat, 16 Feb 2002 03:00:00 GMT  
 Question about arrays


Quote:

>Hi

>I need some help .....Iam new at programming.  How do you put the contents of a
>file into a one dimensional parallel array???

>scanf ("%d%d", acct_num,over_due);   ??????

If acct_num and over_due are ordinary variables then the above line will
not work as scanf requires the addresses of such variables, so:

scanf("%d %d", &acct_num, %over_due);

If you are filling an array then you can do the following:

scanf("%d %d", acct_num[i], over_due[i]);

Where i is a loop index declared elsewhere.

Quote:
>in the file there are two sets of numbers.  I have to load them into two
>separate arrays....and i have no idea how???? Any help would be appreciated.

Sounds like homework so here is a hint rather than a solution.

I am assuming that the format of your file is something like:

<number> <separator> <number> <end of line>
<number> <separator> <number> <end of line>
........

Where <separator> is something like a space or a comma.

The following pseudo-code shows most of what to do.

DECLARE your arrays and other variables.

OPEN the file for reading.

FOR each line in the file
    READ a line from the file (use fgets)
    PUT the numbers into the array (use sscanf, similar to scanf)
END FOR

CLOSE the file.

PRINT the contents of the arrays (or whatever).

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bob Wightman
--



Sat, 16 Feb 2002 03:00:00 GMT  
 Question about arrays

Quote:

>Hi
>I need some help .....Iam new at programming.  How do you put the contents of a
>file into a one dimensional parallel array???

"One dimensional parallel array" has a dangerous smell for me. See below.

Quote:
>scanf ("%d%d", acct_num,over_due);   ??????
>in the file there are two sets of numbers.  I have to load them into two
>separate arrays....and i have no idea how???? Any help would be appreciated.

You could first read the values with

 scanf("%d%d", &acct_num, &over_due);

(watch those ampersands) and put them in two arrays in a loop, as in

  while(2 == scanf("%d%d", &acct_num, &over_due))
  {
    acct[i] = acct_num;
    due[i] = over_due;
    ++i;
  }

but if "identical array position" is supposed to express a relationship
between the "acct_num" and the "over_due" quantities, I'd suggest you
reconsider your design. Something like

  #define N 1024

  struct info
  {
     int acct_num;
     int over_due;
  } i[N], *p;

  p = i;
  while (p != i + N && 2 == scanf("%d%d", &p->acct_num, &p->over_due))
    ++p;

might be a better way to express that one acct_num belongs to one
over_due.

Kurt

--
| Kurt Watzka                            

--



Sat, 16 Feb 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Question: extern *array and array[]

2. Question about array initializers, const

3. question on arrays

4. a question on array!

5. Stupid question about array boundries

6. Question about array

7. question about array of pointers to chars

8. question about array?

9. Newbie question about arrays

10. Newbie question: Making arrays bigger

11. A question about arrays,pointers and structures

12. Question about arrays and pointers

 

 
Powered by phpBB® Forum Software