Write a c program that prompts the user to enter a


Assignment: Introduction to Programming in C

Instructions

This assignment consists of two problems.

1. Run-Length Decoding

Run-length encoding is a relatively simple technique for compressing data; if a particular value (or substring) appears multiple times in a row, it is only represented once, followed by an integer indicating the number of times it should be repeated. For example, the string "AAAAA" can be compressed to "A5" (5 occurrences of 'A'), saving three characters.

Write a C program that prompts the user to enter a character to count and and string (character sequence) to process. Store the string in a char array; you may assume that the user will never enter more than 500 characters, so make the array 501 elements long (to account for the null-terminator). You may further assume that the string is run-length encoded with an even number of characters (each pair of characters consists of a printing character followed by a digit representing its repetition count). Your program should define and print the results of calling two functions (be sure to use pointers carefully in your loops!):

a. int countTotal (char *)

This function processes the input string and returns the total number of characters in the decoded result. For example, given the input "a3b7a2c4", the function would return 16. Hint: You don't need to reconstruct the original string to solve this problem!

b. int countChar (char *, char)

This function takes the input string, followed by the search character, as arguments. It returns the total number of times the search character will occur in the decoded result. For example, given the input string "a3b7a2c4", the function would return 5 ('a' occurs in two places in the decoded result, first in a triplet and then in a pair). Hint: As with countTotal(), you don't need to reconstruct the original string to solve this problem!

2. Processing Product Ratings

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. 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.

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: Write a c program that prompts the user to enter a
Reference No:- TGS02741330

Expected delivery within 24 Hours