Thread: Use arrays to talley ratings and give average

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Use arrays to talley ratings and give average

    I am working on a program that should tally the ratings of a product. There are 5 rating levels from 1 to 5 and 40 different ratings. The program should print out the ratting tallies for each of the 5 rating levels and rpint the average rating for the product. Here is what I have so far. I have commented out some of the parts I havnt totally figured out yet.

    Code:
    #include <stdio.h>
    
    #define MAX_RATERS 40
    
    #define NUM_RATING_LEVELS 6
    
    int main ()
    
    {
            int i, j;
            int avg;
            int ratings[MAX_RATERS] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5, 4, 1, 3, 3, 1, 5, 3, 3, 2, 2, 4, 5, 2, 1, 3, 4, 2, 5, 1, 4, 5, 1, 2, 5, 1, 4, 3, 1, 3, 5};
            int stars[NUM_RATING_LEVELS];
            float sum = 0;
    
            //stars[ratings[i]] = stars[ratings[i]] + 1;
    
    	for (i = 0; i < MAX_RATERS; i++)
            {   stars[ratings[i]] = stars[ratings[i]] + 1;
    		
                    
              // for (j = 0; j <= stars[i]; j++)  
              //    printf("%d, %d", i, ratings[i]);
            }
    
    	
    	     /* for (i = 0; i < 40; i++); //this calculates the average of all the ratings
                    sum += ratings[i];
                    avg = sum/40;
    
             printf("the average rating for the product is %d\n", avg);*/
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    What's the question?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    I would like to count out the and tally the ratings. A total of all the ones, twos, threes, fours and fives and then average them all. Something like this...

    stars[1] = 5
    stars[2] = 5
    stars[3] = 10
    stars[4] = 10
    stars[5] = 10
    The average rating is: 3.38

    So I cant figure out how to get the ratings sumed and then in nice columns

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I still can't see the question

    You should post a sample program (without the comments) that is supposed to work.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    How about something simple first. Why wont this work? I want to know the sum of this array. So I put the Ratings[] in for it to Sum. I get this huge negative number. I know that there are 40 ratings so it fits in my array, but still gives me a negative.
    Code:
    #include <stdio.h>
    
    #define MAX_RATERS 40
    
    #define NUM_RATING_LEVELS 6
    
    int main ()
    
    {
            int i, j;
            int avg;
            int ratings[MAX_RATERS] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5, 4, 1, 3, 3, 1, 5, 3, 3, 2, 2, 4, 5, 2, 1, 3, 4, 2, 5, 1, 4, 5, 1, 2, 5, 1, 4, 3, 1, 3, 5};
            //int stars[NUM_RATING_LEVELS];
            int sum = 0;
    
            
    
    	
    	      for (i = 0; i < MAX_RATERS; i++); 
                    sum = sum + ratings[i];
                    avg = sum/40;
    
             printf("the sum rating for the product is %d\n", sum);
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    for (i = 0; i < MAX_RATERS; i++);
    That semi-colon at the end is the problem. It makes the loop do nothing.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    Remove the semi-colon from the for loop (shown in red below). It's preventing the 2nd line below from being looped.
    Code:
    for (i = 0; i < MAX_RATERS; i++); 
                    sum = sum + ratings[i];
    EDIT: lol - beaten to it...

  8. #8
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by new2cstudent View Post
    How about something simple first. Why wont this work? I want to know the sum of this array. So I put the Ratings[] in for it to Sum. I get this huge negative number. I know that there are 40 ratings so it fits in my array, but still gives me a negative.
    Code:
    #include <stdio.h>
    
    #define MAX_RATERS 40
    
    #define NUM_RATING_LEVELS 6
    
    int main ()
    
    {
            int i, j;
            int avg;
            int ratings[MAX_RATERS] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5, 4, 1, 3, 3, 1, 5, 3, 3, 2, 2, 4, 5, 2, 1, 3, 4, 2, 5, 1, 4, 5, 1, 2, 5, 1, 4, 3, 1, 3, 5};
            //int stars[NUM_RATING_LEVELS];
            int sum = 0;
    
            
    
    	
    	      for (i = 0; i < MAX_RATERS; i++); 
                    sum = sum + ratings[i];
                    avg = sum/40;
    
             printf("the sum rating for the product is %d\n", sum);
    
    return 0;
    }
    First I see a semicolon next to your FOR loop! get rid of that.

    Edit: since your for loop only has one statement, it won't need curly braces
    as I originally suggested. However, the curly braces would make things more clear.

    Would it be too rash if I suggested making "avg" a float?
    Last edited by Char*Pntr; 09-26-2010 at 01:42 PM. Reason: curly braces

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Right, thanks, I should have seen that. So now I got an average of 2, but the real average is 2.95. So I changed my float to a double and changed the &d to a %6.2lf. I want to get the 2 decimal places. But all that prints is -0.00 any ideas?
    Code:
    #include <stdio.h>
    
    #define MAX_RATERS 40
    
    #define NUM_RATING_LEVELS 6
    
    int main ()
    
    {
            int i, j;
            int avg;
            int ratings[MAX_RATERS] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5, 4, 1, 3, 3, 1, 5, 3, 3, 2, 2, 4, 5, 2, 1, 3, 4, 2, 5, 1, 4, 5, 1, 2, 5, 1, 4, 3, 1, 3, 5};
            //int stars[NUM_RATING_LEVELS];
            double sum = 0;
    
            
    	      for (i = 0; i < 40; i++) //this calculates the average of all the ratings
                    sum += ratings[i];
                    avg = sum/40;
    
             printf("the avg rating for the product is %6.2lf\n", avg);
    
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to change your array, imo.

    Ratings are made FOR a <product, students, etc.> given SCORES, in a one to many, relationship (usually).

    Think of STUDENTS with test SCORES:

    Code:
    Name    Scores
    ======================
    Abby  20  22  18  90          (row 0)  Average Abby score, here
    Bob   15  20  17  77          (row 1)   Average Bob score, here
    Chris 21  20  19  91          (row 2)   Average Chris score, here
    ======================
    Average test score here
          18  21  18  86         (just guesses on numbers)
    So we can get 1) Each students average score or 2) the average score of all the students, for each quiz and test.

    In the first instance, we sum up the scores along the row, and in the second instance we sum up the scores along the columns.

    imo, you need to add a second array to your program, and keep this relationship: the names of the product in a row index[row][col] correspond to the products data [row][col]

    Like in the above, Abby's scores are on score[0][N], and her name is in students[0][N]

    Your code (first post) has this:
    Code:
    	      for (i = 0; i < MAX_RATERS; i++); 
                    sum = sum + ratings[i];
                    avg = sum/40;
    
             printf("the sum rating for the product is %d\n", sum);
    
    
    and needs this:
    
    	      for (i = 0; i < MAX_RATERS; i++) 
                    sum += ratings[i];
                  avg = sum/40;
    
             printf("the sum rating for the product is %d\n", sum);
    And integer division will cut off (truncate as they call it), any remainder. To keep it, use double or float data types.
    Last edited by Adak; 09-26-2010 at 01:53 PM.

  11. #11
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by new2cstudent View Post
    Right, thanks, I should have seen that. So now I got an average of 2, but the real average is 2.95. So I changed my float to a double and changed the &d to a %6.2lf. I want to get the 2 decimal places. But all that prints is -0.00 any ideas?
    Code:
    #include <stdio.h>
    
    #define MAX_RATERS 40
    
    #define NUM_RATING_LEVELS 6
    
    int main ()
    
    {
            int i, j;
            int avg;
            int ratings[MAX_RATERS] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5, 4, 1, 3, 3, 1, 5, 3, 3, 2, 2, 4, 5, 2, 1, 3, 4, 2, 5, 1, 4, 5, 1, 2, 5, 1, 4, 3, 1, 3, 5};
            //int stars[NUM_RATING_LEVELS];
            double sum = 0;
    
            
    	      for (i = 0; i < 40; i++) //this calculates the average of all the ratings
                    sum += ratings[i];
                    avg = sum/40;
    
             printf("the avg rating for the product is %6.2lf\n", avg);
    
    return 0;
    }
    You probably forgot to change your format specifier in the printf() to %f.

    Also you'll need to change int 40 to type float as well in your calculation,

    Edit:
    ADAK already pointed out using int in division. Sorry about that!

    But that will fix your result. :-)
    Last edited by Char*Pntr; 09-26-2010 at 02:15 PM.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    I still cant figure this part out.

    I had this...
    Code:
    for (i = 0; i < MAX_RATERS; i++) 
                    sum += ratings[i];
                    avg = sum/40;
            
            printf("the avg rating for the product is %d\n", avg);
    and I got and avg of 2

    and changed to this...
    Code:
            int i, j;
            int avg;
            int ratings[MAX_RATERS] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5, 4, 1, 3, 3, 1, 5, 3, 3, 2, 2, 4, 5, 2, 1, 3, 4, 2, 5, 1, 4, 5, 1, 2, 5, 1, 4, 3, 1, 3, 5};
            //int stars[NUM_RATING_LEVELS];
            float sum = 0;
    
            for (i = 0; i < MAX_RATERS; i++) 
                    sum += ratings[i];
                    avg = sum/40.0;
    
             printf("the avg rating for the product is %6.2f\n", avg);
    [
    and I get -24659887545etc

    what happened?

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Figured it out, I needed to change my average to a float as well.

  14. #14
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by new2cstudent View Post
    Figured it out, I needed to change my average to a float as well.
    That was one of my first "rash" suggestion! I suggest you slowly re-read all
    of the responses on this thread.

  15. #15
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Now I am trying to figure out how to sort or count the ratings. Tally up the 1s 2s etc. I have put a loop in another loop. Here it is...
    Code:
    #include <stdio.h>
    
    #define MAX_RATERS 40
    
    #define NUM_RATING_LEVELS 6
    
    int main ()
    
    {
            int i, j;
            float avg;
            int ratings[MAX_RATERS] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5, 4, 1, 3, 3, 1, 5, 3, 3, 2, 2, 4, 5, 2, 1, 3, 4, 2, 5, 1, 4, 5, 1, 2, 5, 1, 4, 3, 1, 3, 5};
            int stars[NUM_RATING_LEVELS];
            float sum = 0;
    
            //stars[ratings[i]] = stars[ratings[i]] + 1;
    
    	for (i = 0; i < NUM_RATING_LEVELS; i++)
            {  	printf("%dstars:, %d\n", i, stars[i]);
    			for (j = 0; stars[ratings[i]] = stars[ratings[i]] + 1; j++)
    			{	printf("%d\n", ratings[j]);
    			}
    }
    	      for (i = 0; i < MAX_RATERS; i++) 
                    sum += ratings[i];
                    avg = sum/40.00;
    
             printf("the avg rating for the product is %6.2f\n", avg);
    
    return 0;
    }
    im still not seeing how to get the tally of each rating. If I use a IF statement saying if rating[] =<5 print sum?

Popular pages Recent additions subscribe to a feed