quick question about storing values in an array

This is a discussion on quick question about storing values in an array within the C Programming forums, part of the General Programming Boards category; Here's a code snippet i got from a book: Code: /* Declare an array to hold expenses, and a counter ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    10

    quick question about storing values in an array

    Here's a code snippet i got from a book:

    Code:
    /* Declare an array to hold expenses, and a counter variable */
    
    float expenses[13];
    int count;
    
    int main(void)
    {
        /* Input data from keyboard into array */
        for (count = 1; count < 13; count++)
        {
            printf("Enter expenses for month %d: ", count);
            scanf("%f", &expenses[count]); <-------???
        }
    Why is count inside the array expenses? can someone explain to me how the values are stored in the array expenses?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by houler
    can someone explain to me how the values are stored in the array expenses?
    Incorrectly.

    The first element in the expenses array is 0, but the for loop starts storing something at expenses[1].

    I'm not quite sure what you mean by "why is count in the array expenses?"
    Last edited by itsme86; 12-10-2004 at 01:11 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by itsme86
    Incorrectly.

    The highest element in the expenses array is 12, but the for loop will attempt to store something at expenses[13].
    How is that? I can only see that it leaves out element 0.

    edit: ignore this, post changed.
    Last edited by Nyda; 12-10-2004 at 01:14 PM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by Nyda
    How is that? I can only see that it leaves out element 0.
    Damn, you caught me before I could fix my mistake. My brain was thinking at the beginning of the array, but my fingers were thinking at the end of it
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Quote Originally Posted by itsme86
    Incorrectly.

    The first element in the expenses array is 0, but the for loop starts storing something at expenses[1].

    I'm not quite sure what you mean by "why is count in the array expenses?"
    Can you explain this part to me? I thought count was only used as a counter? why is it in between the braces?

    scanf("%f", &expenses[count]);

    i

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    It's a variable. It represents the index in the array where you want to store the value. Sure it's a counter variable, but each time through the loop you can use its value.
    Code:
    {
      int array[5];
      int i;
    
      for(i = 0;i < 5;++i)
      {
        array[i] = 1;
      }
    }
    ...is the same thing as...
    Code:
    {
      int array[5];
      int i;
    
      array[0] = 1;
      array[1] = 1;
      array[2] = 1;
      array[3] = 1;
      array[4] = 1;
    
      i = 5;
    }
    If you understand what you're doing, you're not learning anything.

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Because think of it this way
    First time through the loop
    Code:
    for(count = 1; count < 13; count++)
    {
       scanf("%f",&expenses[count]);
       /* count equals one */
       /*count goes up one so count equals 2 now */
       /*count continues to go up until it reaches 12 and then exits the loop */
       /*so basically every time it goes through the loop it replaces count with the number */
    
    }
    Woop?

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    10

    Thumbs up

    I get it. Thanks alot guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 07:24 AM
  3. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-22-2008, 12:18 AM
  4. Storing values from an array pointer
    By newatc in forum C Programming
    Replies: 21
    Last Post: 01-07-2008, 06:46 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21