Thread: quick prob w/summing and averaging the elements of an array

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    15

    quick prob w/summing and averaging the elements of an array

    Yes, another hw assignment..

    the assignment is as follows:

    12 DIRECTIONS: Write a program which will prompt the
    13 user to enter 10 int values each in the range 0 to 9
    14 inclusive. Next, display the 10 ints on one line,
    15 each separated by a single space[ HINT: use '%2d' format string].
    16 Compute the average of the 10 ints, using integer division.
    17
    18 On the following line, display underneath each number
    19 either a '<', '=', or '>' symbol according to these rules:
    20
    21 a) if the number is less than the average, print '<'.
    22 b) if the number is equal to the average, print '='.
    23 c) otherwise, display a '>' character.
    24
    25 Ensure that your program concludes by displaying
    26 "PROGRAM ENDS" followed by a single newline character.
    27
    28 See the examples below. Also execute 'pe4.exe' at the
    29 admiral prompt to see correct output.
    30
    31 EXAMPLES:
    32
    33 Enter 10 digits: 0 1 2 3 4 5 6 7 8 9
    34 0 1 2 3 4 5 6 7 8 9
    35 < < < < = > > > > >
    36 PROGRAM ENDS
    37
    38 Enter 10 digits: 2 2 2 2 2 4 4 4 4 4
    39 2 2 2 2 2 4 4 4 4 4
    40 < < < < < > > > > >
    41 PROGRAM ENDS
    42
    43 Enter 10 digits: 1 1 1 1 1 1 1 1 1 1
    44 1 1 1 1 1 1 1 1 1 1
    45 = = = = = = = = = =
    46 PROGRAM ENDS
    47
    48 Enter 10 digits: 5 5 5 4 4 4 4 3 3 3
    49 5 5 5 4 4 4 4 3 3 3
    50 > > > = = = = < < <
    51 PROGRAM ENDS
    52
    53
    54 HINTS: Ensure that the characters on the second line
    55 line up exactly underneath the digits on the first line.
    56
    57 ASSUMPTIONS:
    58 a) all values entered are ints in the range 0 - 9 inclusive.
    59 b) 10 values will be entered.

    I have this
    Code:
         1  #include <stdio.h>
         2
         3  int main(void)
         4  {
         5  int i, avg, sum, arr[10];
         6
         7  printf("Enter 10 digits: ");
         8  for (i = 0; i < 10; i++) scanf("%d", &arr[i]);
         9  for (i = 0; i < 10; i++) printf("%2d", arr[i]);
        10  for (i = 0; i < 10; i++) sum+= arr[i];
        11  avg = (sum/10);
        12
        13  printf("\n");
        14  printf("%d\n", sum);
        15  printf("%d\n", avg);
        16
        17  for (i = 0; i < 10; i++)
        18  {    if (arr[i]<avg)
        19        printf(" <");
        20      else if (arr[i]==avg)
        21        printf(" =");
        22      else
        23        printf(" >");
        24  }
        25  printf("\n");
        26  printf("PROGRAM ENDS\n");
        27  
        28  return 0;
        29  }
    I put lines 14 & 15 in to figure out if my sum and avg loops were working, and obviously they're not

    and when I enter:

    0 1 2 3 4 5 6 7 8 9 0
    this is returned
    0 1 2 3 4 5 6 7 8 9
    -4194903
    -419490
    > > > > > > > > > >
    what am I doing wrong?

    super extra mad props if you can help me fix this in the next 10 minutes, and I'll buy you a beer

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you forgot to initialise sum to 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You never initialized sum. Initialize it to 0 before entering your loops.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    15

    Thumbs up

    hmm, weird..

    Adding the curly braces at lines 25 and 31 seemed to fix the problem (before I changed line 12 to initialize sum to 0), as I wasn't getting any compile warnings or errors and the program was working correctly...

    but when I submitted it to the turn in site, it said there were compile errors
    Code:
         8  #include <stdio.h>
         9
        10  int main(void)
        11  {
        12  int i, j, avg, sum=0, arr[10];
        13
        14  printf("Enter 10 digits: ");
        15  for (i = 0; i < 10; i++) scanf("%d", &arr[i]); /*For each element of array (named 'arr'), scan in a value*/
        16  for (i = 0; i < 10; i++) printf("%2d", arr[i]); /*For each element of arr display value*/
        17  for (j = 0; j < 10; j++) sum+= arr[j]; /*Compute sum of all elements of array*/
        18  avg = (sum/10); /*divide sum by 10 (compute average)*/
        19
        20  printf("\n");
        21  /*printf("%d\n", sum);*/
        22  /*printf("%d\n", avg);*/
        23
        24  for (i = 0; i < 10; i++) /*For each element of arr...*/
        25  {    if (arr[i]<avg)
        26        printf(" <"); /*if value of element < average, print ' <'*/
        27      else if (arr[i]==avg) /*else if value of element == average, print ' ='*/
        28        printf(" =");
        29      else
        30        printf(" >");
        31  }
        32  printf("\n");
        33  printf("PROGRAM ENDS\n");
        34  
        35  return 0;
        36  }
    once I initialized 'sum' to 0 in line 12, the turn in site accepted it. I was thinking that line 17 would initialize 'sum' to 0 on its own in the first iteration of the for loop...guess not. But why the discrepancy? I'm almost positive our teacher said the turn in site uses the same compiler we use to write and test our code

    anyway, thanks for the help, guys

    EDIT: LMAO, love the quote in your sig brewbuck!!
    Last edited by pinkfloyd4ever; 12-01-2009 at 12:30 AM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I was thinking that line 17 would initialize 'sum' to 0 on its own in the first iteration of the for loop...guess not. But why the discrepancy?
    It doesn't.

    sum can start with any value. It's undefined by the standard. It could be 0, 1, 5000, -999, log(e^pi)... anything.

    I'm almost positive our teacher said the turn in site uses the same compiler we use to write and test our code
    It's possible that they only accept code without warnings. The compiler will most certainly produce a warning for something like this.

    BTW, warnings are the compilers' way of saying "the code is syntactically right, but will probably not do what you want".

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    Quote Originally Posted by cyberfish View Post
    It's possible that they only accept code without warnings. The compiler will most certainly produce a warning for something like this.
    I always compile with all warnings on, and it wasn't showing any warnings or errors

    I'll see what my teacher says tomorrow

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What compiler are you using? What options?

Popular pages Recent additions subscribe to a feed