Myrmidon, Inc,. sells various products online, and has hired you to analyze prod
ID: 3707643 • 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 betterregarded 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. Hint: Use scanf() rather than getchar() to read in two values at a time and test the value that scanf() returns each time it is called. When scanf() returns a value other than 2, stop the loop and generate your 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. For example, consider the following sample program execution (program output is in italics, and user input is in bold): How many unique products are there? 4 Enter product number and rating (1-10), separated by a space, one pair at a time: 1 1 1 1 2 1 2 8 4 1 4 8 1 5 1 3 3 8 3 3 2 7 4 10 3 5 3 3 3 9 1 1 3 10 1 9 4 6 2 3 3 3 1 5 2 6 4 9 2 4 Product 1 Information: 1 star: 3 2 star: 0 3 star: 1 4 star: 0 5 star: 2 6 star: 0 7 star: 0 8 star: 0 9 star: 1 10 star: 0 Average rating: 3.57 out of 10 (7 ratings total). -------------------- Product 2 Information: 1 star: 1 2 star: 0 3 star: 1 4 star: 1 5 star: 0 6 star: 1 7 star: 1 8 star: 1 9 star: 0 10 star: 0 Average rating: 4.83 out of 10 (6 ratings total). -------------------- Product 3 Information: 1 star: 0 2 star: 0 3 star: 3 4 star: 0 5 star: 1 6 star: 0 7 star: 0 8 star: 1 9 star: 1 10 star: 1 Average rating: 5.86 out of 10 (7 ratings total). -------------------- Product 4 Information: 1 star: 1 2 star: 0 3 star: 0 4 star: 0 5 star: 0 6 star: 1 7 star: 0 8 star: 1 9 star: 1 10 star: 1 Average rating: 6.80 out of 10 (5 ratings total). -------------------- 25 product ratings evaluated in all. In order to save you from spending too much time typing and retyping input data to test your code, we have provided you with several small-to-medium data files that contain test input. If you run your program from the command line (e.g., the Terminal in OS X or the command prompt on Sparky), you can use the left arrow (<) operator to redirect the contents of these files into standard input for your program, e.g.: a.out < testdata1.txt You may also be able to copy and paste the contents of the sample data files into the console in CLion, Netbeans, or Xcode.
Explanation / Answer
CODE:
#include <stdio.h>
int main()
{
int n;
printf("Enter number of unique product: ");
scanf("%d", &n);
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.