Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Myrmidon, Inc. sells various products online, and has hired you to analyze produ

ID: 3811763 • Letter: M

Question

Myrmidon, Inc. sells various products online, and has hired you to analyze product feedback left by satisfied (or not-so-satisfied) customers in the form of star ratings. A customer can rate a product on a scale of one to ten stars the more stars, the better regarded the product is. Each product is identified by a unique (integer) product number, ranging from 1 through the total number of products offered. Your job is to write a program that reads in an arbitrary number of pairs of integers (a product number, followed by a single space, followed by an integer rating from 1 through 10) and then prints a neatly formatted report listing the rating breakdown for each product, plus the average rating (as a float with 2 decimal places) for that product and the total number of ratings processed. If a product has no ratings, it should be omitted from the final report. When you first run your program, it should ask for the total number of unique products that are sold. It should then use a loop (most likely a while loop) to read in as many product ratings as the user cares to enter. When the user input stops, the program should calculate and print out the final report. Use a two-dimensional int array to store the ratings (one row per product, one column per possible rating value). Remember that array indexes begin at 0, but product identifiers (and ratings) start at 1. You will need to either use an offset value in your array indexes or (this is the easier option) simply create your array with one more row and column than you need, and skip row 0 and column 0. To simplify your life later, you may also want to create extra array columns to store the quantities and sums of each product's ratings.

Explanation / Answer

#include <stdio.h>

int main()
{
int n;
printf("Enter number of unique product: ");
scanf("%d", &n);

// We will use 0th column for sum and 12 th column for count.
int productRatings[n][12];

int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < 12; j++)
productRatings[i][j] = 0;
}

int id, rating;
while((scanf("%d%d", &id, &rating) == 2))
{
productRatings[id-1][rating] += 1;
productRatings[id-1][0] += rating;
productRatings[id-1][11] += 1;
}

printf(" Rating breakdown of each product. ");
printf("Product id Rating: ");
for(i = 1 ; i < 11; i++)
{
printf("%d ", i);
}
printf(" Average rating ");
printf("========================================================== ");
for(i = 0; i < n; i++)
{
if (productRatings[i][0] == 0)
{
continue;
}

printf("%d ", i+1);
for(j = 1; j < 11; j++)
{
printf("%d ", productRatings[i][j]);
}
double averageRating = ((double)productRatings[i][0])/productRatings[i][11];
printf(" %.2f ", averageRating);
}
return 0;
}

While running please give some non int input like . so that scanf do not return 2.

Here is a sample run

Enter number of unique product: 10
1 2
1 5
1 9
1 6
2
3
2 4
2 6
2 5
1 2
9 8
4 5
6 7
8 5
10 5
.


Rating breakdown of each product.

Product id   Rating: 1 2 3 4 5 6 7 8 9 10    Average rating
==========================================================
1           0 2 0 0 1 1 0 0 1 0    4.80
2           0 0 1 1 1 1 0 0 0 0    4.50
4           0 0 0 0 1 0 0 0 0 0    5.00
6           0 0 0 0 0 0 1 0 0 0    7.00
8           0 0 0 0 1 0 0 0 0 0    5.00
9           0 0 0 0 0 0 0 1 0 0    8.00
10           0 0 0 0 1 0 0 0 0 0    5.00