Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

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.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M92784514

Have any Question?


Related Questions in C/C++

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As