Question: Practice Question for processing input stream with 2 kinds of tokens. Any colored image can be decomposed into Cyan, Magenta and Yellow (i.e., the three

Practice Question for processing input stream with 2 kinds of tokens.

Any colored image can be decomposed into Cyan, Magenta and Yellow (i.e., the three primary pigment colors) and Black rectangles. The amount of ink needed for the image can be calculated from the areas of the rectangles and the intensity of the color in each rectangle. All the rectangles in any image can be described in the following format: C 2.0 3.0 .5 M 1.5 2.5 .8 K 1 1 1 M 2.3 2.3 .9 C 2.5 2.5 1 Y 3 4 .5 Z

The letters C, M, Y and K represent Cyan, Magenta, Yellow and Black respectively; the letter Z terminates the output. Each letter is followed by three numbers: the length of the rectangle, breath of the rectangle and the intensity of the color. The above example has 2 Cyan rectangles, 2 Magenta rectangles, one Yellow rectangle and one Black rectangle.

The first rectangle (C 2.0 3.0 0.5) is Cyan with length 2.0, breadth 3.0 and intensity 0.5. The amount of ink used for this rectangle is computed as 2.0 x 3.0 x 0.5 = 3.0 units.

The total amount of ink needed for this image is calculated as: Amount of Cyan ink = 2.0 x 3.0 x .5 + 2.5 x 2.5 x 1 = 9.25 Amount of Magenta ink = 1.5 x 2.5 x .8 + 2.3 x 2.3 x 0.9 = 7.761

Amount of Yellow ink = 3 x 4 x .5 = 6 Amount of Black ink = 1 x 1 x 1 = 1

Your task is to create a program that reads data in the above format and computes the total amount of ink for each of the colors. The data is input from the keyboard. There are no errors permitted in the data entry process.

The program prints the amount of ink needed for each of the colors. For the above example, the output is: Amount of Cyan ink = 9.25 Amount of Magenta ink = 7.761 Amount of Yellow ink = 6 Amount of Black ink = 1

Develop a program that reads data in the above format and produces the required output.

(a) In 2-3 sentences, describe your strategy for solving this problem.

(b) What is the shortest possible input?

(c) List the sequence of operations that will be performed in order to process the data. Identify the repetition in this sequence.

(d) What kind of a loop (pretest or post-test) seems most appropriate for this repetition and why?

(e) This program will need a branching statement to process each reading. What C++ construct will you choose to implement the branch? What led you to make this choice?

(f) Translate your sequence of operations from part (c) into C++ code.

Can you please answer all a, b,c,d, e and f question this is practice question for test.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!