C++ I need to have a two dimensional array in the below code. What would work? W
ID: 3722454 • Letter: C
Question
C++ I need to have a two dimensional array in the below code. What would work? Would having the output in rows and columns be a good example and if so, where should it go?
#include <iostream>
#include <string>
using namespace std;
int index = 0; //Variable to show how many Media Items are entered.
struct Media { // Media struct
string title;
string starring;
string format;
string genre;
string yearProduced;
string comments;
};
int displayMenu();
Media getMedia();
void showMedia(Media);
void allMedia(Media[]);
void sortMedia(Media[]);
void findMedia(Media[], int);
int main()
{
Media med[1000]; // Declare array of Media struct
while (true)
{
int choice = displayMenu();
switch (choice)
{
case 1:
med[index] = getMedia();
index++;
break;
case 2:
allMedia(med);
break;
case 3:
sortMedia(med);
break;
case 4:
findMedia(med, index);
break;
case 5:
cout << "You have selected to exit the program. Thank you." << endl;
return 0;
break;
default:
cout << "That is not a valid choice. Please try again." << endl;
}
cout << endl;
}
system("pause");
return 0;
}
int displayMenu() //Display menu to user
{
cout << "The Greely Family Media Library" << endl;
cout << endl;
cout << "Please select from the below menu." << endl;
cout << endl;
cout << "1. Enter New Media Item." << endl;
cout << "2. Display all Media Items." << endl;
cout << "3. Sort all Media Items." << endl;
cout << "4. Display a particular Media Item." << endl;
cout << "5. Exit the program." << endl;
cout << endl;
int choice; //User selects what they want to do.
cout << "Enter choice: ";
cin >> choice;
cin.ignore();
cout << endl;
return choice;
}
Media getMedia()
{
Media a;
cout << "Enter Title: ";
getline(cin, a.title);
cout << endl;
cout << "If Media Item is a music CD, enter the singer or group. If a PC Game, enter N/A. " << endl;
cout << "Enter Starring: ";
getline(cin, a.starring);
cout << endl;
cout << "Format can be DVD, VHS, CD, Blue-Ray or PC Game." << endl;
cout << "Enter format: ";
getline(cin, a.format);
cout << endl;
cout << "Genre can be Main theme of item followed by TV Show, Movie, Music or PC Game." << endl ;
cout << "Example of Genre: Childen's Comedy TV Show " << endl;
cout << "Enter genre: ";
getline(cin, a.genre);
cout << "Enter Year Produced:";
getline(cin, a.yearProduced);
cout << endl;
cout << "Comments are exactly that. A way to refresh your memory what the Media Item is about." << endl;
cout << "Example, Daktari was a show that I watched on Sundays when growing up. " << endl;
cout << endl;
cout << "Enter Comments: ";
getline(cin, a.comments);
return a;
}
void showMedia(Media c)
{ //Display Media Item Details
cout << "Title: " << c.title << endl;
cout << "Starring: " << c.starring << endl;
cout << "Format: " << c.format << endl;
cout << "Genre: " << c.genre << endl;
cout << "Year Produced: " << c.yearProduced << endl;
cout << "Comments: " << c.comments << endl;
}
void allMedia(Media med[])
{
for (int i = 0; i < index; i++)
{
showMedia(med[i]);
cout << endl;
}
}
void sortMedia(Media *med) //Sort by format and then title
{
for (int i = 0; i < index; i++)
{
for (int j = 0; j < index; j++)
{
if (med[i].format == med[j].format) //Checking to see if formats are equal
{
if (med[i].title < med[j].title) //Formats are equal then compare titles
{
Media t = med[i]; //Swapping addresses of elements
med[i] = med[j];
med[j] = t;
}
}
else if (med[i].format < med[j].format) //Checking with formats
{
Media t = med[i];
med[i] = med[j];
med[j] = t;
}
}
}
cout << "After sorting by Format and Title. " << endl; //Display Lists
allMedia(med);
}
void findMedia(Media med[], int size) //Ask User to enter a title.
{
string title;
cout << "Enter Title: ";
cin >> title;
for (int i = 0; i < size; i++)
{
if(med[i].title==title)
{
showMedia(med[i]);
return ;
}
}
cout << "This title cannot be found. It appears that you do not have this in your selection." << endl;
}
Explanation / Answer
Media is a struct with 6 string fields
We can place the information in an string array of size n x 6.
n - number of medias in the library.
The function showMedia can have one more version: showMediaMod(String a[][6],n)
void showMediaMod(String a[][6], int n){
cout << setw(10) << left << "Title";
cout << setw(10) << "Starring";
cout << setw(10) << "Format";
cout << setw(10) << "Genre";
cout << setw(10) << "Year Produced";
cout << setw(10) << "Comments" << endl;
for (int i = 0; i<n; i++){
cout << setw(10) << left << a[n][0];
cout << setw(10) << a[n][1];
cout << setw(10) << a[n][2];
cout << setw(10) << a[n][3];
cout << setw(10) << a[n][4];
cout << setw(10) << a[n][5] << endl;
}
}
The current array of Media can be copied in the 2-D array as follows:
void copyTo2DArray(Media a[], string &b[][], int n){
for (int i = 0; i<n; i++){
b[i][0] = a[i].title;
b[i][1] = a[i].starring;
b[i][2] = a[i].format;
b[i][3] = a[i].genre;
b[i][4] = a[i].yearProduced;
b[i][5] = a[i].comments;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.