Thread: Seemingly simple array program yielding bizarre output

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    Seemingly simple array program yielding bizarre output

    #include <stdio.h>
    #include <stdlib.h>


    int main(void) {
    int grades[10];
    int i, sum;

    for (i=0; i<=10; ++i)
    {
    sum = sum+grades [i];

    printf("\n %d",sum);

    }
    return 0;
    }
    Output:

    2009291924
    2009147472
    -1
    2009288233
    2009288258
    4200880
    2359128
    4200982
    4200880
    2354536
    10
    Tried my best to debug & finally gave up. IMO output is irrelevant. Kindly help.
    Last edited by alter.ego; 04-07-2011 at 04:45 AM. Reason: Typo

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Both sum and grades are uninitialized, meaning they will contain what ever garbage happens to reside at that memory location currently.

    To initialize them:

    Code:
    int grades[10] = {0};
    int i, sum = 0;
    That way they will have initial values of 0. Another thing I noticed is that your loop gives you 11 outputs, (meaning you print one value outside the array) due to the condition: i <= 10
    you probably want: i < 10

    This comes down to how arrays are indexed, starting from 0, so in your case valid indexes are 0-9.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks! that helped a bit & I have tweaked the program a little. Why isn't value of sum changing when i is getting iterated? I am sorry if my doubts are slightly noobish; I am just working on some basic concepts of C programming.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
    int grades[5] = {0};
    int i, sum = 0;
    
    for (i=0; i<5; ++i)
    {
    	sum = sum+grades [i];
    
    printf("\n%d \t%d",i, sum);
    }
    
    return 0;
    }
    Output:

    Code:
    0     0
    1     0
    2     0
    3     0
    4     0
    Last edited by alter.ego; 04-07-2011 at 05:16 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by alter.ego View Post
    Why isn't value of sum changing when i is getting iterated?

    Code:
    ...
    int grades[5] = {0};
    ...
    for (i=0; i<5; ++i)
    {
    	sum = sum+grades [i];
    }
    ...
    The statement "grades[5] = {0}" will set all the members of grades to 0, meaning that each time through your loop you're adding 0 to sum, so it stays the same.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, there are no grades in your array. If you want to sum grades, you must first input these grades into the array, how else can you sum them?

    Edit: you can initialize the array with the grades if you want to.

    Code:
    int grades[5] = {1, 2, 3, 4, 5};
    Last edited by Subsonics; 04-07-2011 at 05:35 AM.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    That's a great enlightment for me (if I understood correctly).

    So for loop just references the memory location where the values are initialized. It doesn't actually provide the values in int grades[5]. Am I in right direction?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, if you are talking about your specific for loop here, you are stepping through the array with the index and adding them one at a time to sum.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks guys! Got it. Much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 05:07 PM
  2. How do you read gzip output from 'C' program?
    By brett in forum C Programming
    Replies: 5
    Last Post: 03-13-2006, 12:01 AM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM