Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CS 10, Lab 8

Structures


In this lab, we will use getline() and cin>> in order to interact with the user.


Part A:  Online shopping

1. Create a structure Item that represents an item sold on a website that sells books and music albums. Included in your struct should be the item’s name, author/artist, price, a list of either the chapters or tracks, and its release date. You may go straight to C++ for this if you wish.  Check your answer with one of the lab teachers before moving on, and get your lab report signed.

2. Write pseudocode for two functions: void printItem(Item it) that prints out all of it’s information in a nice format, and void initializeItem(Item& it) that prompts the user to fill in all of an item’s information.  Include pseudocode for a main function which calls these.  Check your answer with one of the lab teachers before moving on, and get your lab report signed.

3. Turn your pseudocode from 2 into C++ code.  Copy‐paste your code into your lab report.  Create a main that uses your stuff and run your code to be sure it’s right.

4. Write pseudocode for a function double fillCart(Item a[], int size) that shows the user each item in the array a, asks if they’d like to buy it, and returns the total price of all the items they chose. A sample execution is below (>> placed in front of input for clarity only).

Item: Slaughterhouse 5, by Kurt Vonnegut for $7.99
Would you like to "purchase", see more "details", or "pass"?
>>pass
Item: Let it Be, by The Replacements for $11.49
Would you like to "purchase", see more "details", or "pass"?
>>details
Name: Let it Be, by: The Replacements
Price: 11.49
Contents: I Will Dare, Favorite Thing, We're Comin' Out, Tommy Gets His
Tonsils Out, Androgynous, Black Diamond, Unsatisfied, Seen Your Video,
Gary's Got, Sixteen Blue, Answering Machine,
Released: 10/2/1984
Would you like to "purchase" or "pass"?
>>purchase
Item: Let it Be, by The Beatles for $16.29
Would you like to "purchase", see more "details", or "pass"?
>>details
Name: Let it Be, by: The Beatles
Price: 16.29
Contents: Two Of Us, Dig A Pony, Across The Universe, I Me Mine, Dig It, Let
It Be, Maggie Mae, I've Got A Feeling, One After 909, The Long And Winding
Road, For You Blue, Get Back,
Released: 5/11/1970
Would you like to "purchase" or "pass"?
>>pass
Total cost: 11.49
Process returned 0 (0x0) execution time : 12.529 s
Press any key to continue.

5.  Turn your pseudocode from 4 into C++ code.  Copy‐paste your code into your lab report. Write a main function and run it with several outputs to be sure it’s correct. Check your answer with one of the lab teachers before moving on, and get your lab report signed.


Part B:  An application from Math: Matrices.

Matrices are used in mathematics, often to represent vectors or systems of equations. A matrix is a set of numbers arranged in a particular pattern. For example, the following is a 3x3 matrix

1 5 3
6 3 2
8 6 0

In this problem you will work exclusively with 3x3 matrices.

1.  Create a structure that represents a 3x3 matrix.  Use 3 arrays; one to represent each row in the matrix. You may go straight to C++ for this if you wish. Check your answer with one of the lab teachers before moving on, and get your lab report signed.

2. Write pseudocode for the function void printMatrix(Matrix m) that prints out all of m’s information in a nice format.  Include pseudocode for a main function which calls these.  Initialize your matrix using {} notation. Check your answer with one of the lab teachers before moving on, and get your lab report signed.

3. Turn your pseudocode from 2 into C++ code.  Copy‐paste your code into your lab report.  Run your code to be sure it’s right, check your answer with one of the lab teachers before moving on, and get your lab report signed.

4. Write pseudocode for a function Matrix add(Matrix m1, Matrix m2) which returns a new matrix which is the sum of m1 and m2. Adding matrices works as you might expect, where the sum at each location is the sum of the individual values in those locations; e.g.

1 2 3       9 8 7    10 10 10
4 5 6  +  6 5 4  =  10 10 10
7 8 9      3 2 1    10 10 10

Check your answer with one of the lab teachers before moving on, and get your lab report signed.

5. Turn your pseudocode from 4 into C++ code. Copy‐paste your code into your lab report.  Run your code to be sure it’s right.

6.  Write pseudocode for a function Matrix sum(Matrix mats[], int size) that adds up all the matrices in mats and returns the result. Check your answer with one of the lab teachers before moving on, and get your lab report signed.

7. Turn your pseudocode from 2 into C++ code.  Copy‐paste your code into your lab report.  Run your code to be sure it’s right.