(a) Develop a flowchart and then write a menu-driven C++ program to solve the fo
ID: 3809977 • Letter: #
Question
(a) Develop a flowchart and then write a menu-driven C++ program to solve the following problem.
Upon execution of the program, the menu will be displayed, properly centered as shown below. You will be using user-defined functions and arrays and other constructs and references for this assignment.
Help SizeArray Arithmetic Quit
Once the menu is displayed, the user is prompted for a menu selection. A wrong menu option is flagged as an error and the menu is displayed again.
H or h (for Help) will call the function help which briefly explains the problem being solved and the purpose of each menu options. Once the user strikes a key followed by the return key, the menu will appear again.
S or s (for SizeArray) will prompt the user for the actual size of the arrays. Allow the arrays to work up to a size of 10. For the purposes of this assignment, size the arrays to 4. You need to check for the size of the arrays before exiting this option.
A or a (for Arithmetic) will call a function arithmetic which prompts the user for two operations (addArrays or SubArrays). This option must be executed after option S. For each case, call the appropriate functions that fill the arrays using random number generator of your choice. The program will then compute the requested operation and displays the arrays and the results side-by side. At no time the program should be terminated. Once the user viewed the results, striking a key will display the menu and the user is prompted again for a menu selection. You can remain in this mode if you like by adding additional logic to your design. Parameter passing to the function arithmetic is by reference.
Q or q (for Quit) will clear the screen and terminate the program.
Hints:
1. Your program will only have one main() function and many other user-defined functions as stated above. In addition to the main() function which must have a return statement, other functions may have return statements as well. There will be one .cpp file with multiple functions.
2. It is essential that function declarations (prototypes), definitions, and invocations follow the instructions given in class.
3. Make sure your program executes correctly
4. Make sure the variables are correctly declared without any conflicts with other variables. You may have to change some of the variables to make each variable unique.
5. Your program will terminate when the user selects Q or q.
6. Use arrays, functions, and references for this assignment.
Explanation / Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
//Class Arithmetic Array Definition
class ArithmeticArray
{
//Data member of class
int originalArr1[10];
int originalArr2[10];
int resultArray[10];
int arraySize;
public:
//Member function prototype
char menu();
void Help();
void SizeArray();
void ArithmaticAdd();
void ArithmaticSub();
void FillArray();
void Display();
};//End of class
//To display menu and return choice
char ArithmeticArray::menu()
{
char ch;
//Display menu
cout<<" H or h for Help";
cout<<" S or s for Size of array";
cout<<" A or a for Arithmetic Operations";
cout<<" Q or q for Quit";
//Accept the choice
cout<<" Enter your choice: ";
cin>>ch;
//Return choice
return ch;
}//End of function
//To display help to the user
void ArithmeticArray::Help()
{
cout<<" S option is to get the size of the array. It must be less than 10.";
cout<<" A option is to do Arithmetic Addition or subtraction of array.";
cout<<" Q option is to Quit the program.";
}//End of function
//To the the size of array
void ArithmeticArray::SizeArray()
{
cout<<" Enter the size of array (below 10): ";
cin>>arraySize;
}//End of function
//To fill the array with random number
void ArithmeticArray::FillArray()
{
//Loops till the size entered by the user
for(int x = 0; x < arraySize; x++)
{
//Generates random number between 1 and 10 and stores in the arrays
originalArr1[x] = rand() % (10 + 1 - 1) + 1;
originalArr2[x] = rand() % (10 + 1 - 1) + 1;
}//End of loop
}//End of function
//For arithmetic addition of two arrays
void ArithmeticArray::ArithmaticAdd()
{
//Loops till the size entered by the user
for(int x = 0; x < arraySize; x++)
//Add two arrays and store the result in result array
resultArray[x] = originalArr1[x] + originalArr2[x];
}//End of function
//For subtraction operation of two arrays
void ArithmeticArray::ArithmaticSub()
{
//Loops till the size entered by the user
for(int x = 0; x < arraySize; x++)
//Subtracts two arrays and store the result in result array
resultArray[x] = originalArr1[x] - originalArr2[x];
}//End of function
//Display the arrays with result
void ArithmeticArray::Display()
{
cout<<endl;
cout<<" Original Array1: ";
//Loops till the size entered by the user
for(int x = 0; x < arraySize; x++)
cout<<originalArr1[x]<<" ";
//Loops till the size entered by the user
cout<<" Original Array2: ";
for(int x = 0; x < arraySize; x++)
cout<<originalArr2[x]<<" ";
//Loops till the size entered by the user
cout<<" Result Array: ";
for(int x = 0; x < arraySize; x++)
cout<<resultArray[x]<<" ";
}//End of function
//Main function
int main()
{
//Creates an object of ArithmeticArray class
ArithmeticArray aa;
char ch;
//Loops till q or Q is entered by the user
do
{
switch(aa.menu())
{
case 'H': case 'h':
aa.Help();
break;
case 'S': case 's':
aa.SizeArray();
break;
case 'A': case 'a':
cout<<" A or a for Addition arrays: S or s for Subtract Arrays: ";
cout<<" Enter your choice: ";
cin>>ch;
switch(ch)
{
case 'A': case 'a':
aa.FillArray();
aa.ArithmaticAdd();
cout<<" After Addition: ";
aa.Display();
break;
case 'S': case 's':
aa.FillArray();
aa.ArithmaticSub();
cout<<" After Subtraction: ";
aa.Display();
break;
default:
cout<<" Invalid choice!";
}//End of inner switch
break;
case 'Q': case 'q':
exit(0);
default:
cout<<" Invalid choice!";
}//End of outer switch
}while(1);
}//End of main
Output:
H or h for Help
S or s for Size of array
A or a for Arithmetic Operations
Q or q for Quit
Enter your choice: H
S option is to get the size of the array. It must be less than 10.
A option is to do Arithmetic Addition or subtraction of array.
Q option is to Quit the program.
H or h for Help
S or s for Size of array
A or a for Arithmetic Operations
Q or q for Quit
Enter your choice: s
Enter the size of array (below 10): 5
H or h for Help
S or s for Size of array
A or a for Arithmetic Operations
Q or q for Quit
Enter your choice: a
A or a for Addition arrays:
S or s for Subtract Arrays:
Enter your choice: a
After Addition:
Original Array1: 2 5 10 9 3
Original Array2: 8 1 5 9 5
Result Array: 10 6 15 18 8
H or h for Help
S or s for Size of array
A or a for Arithmetic Operations
Q or q for Quit
Enter your choice: a
A or a for Addition arrays:
S or s for Subtract Arrays:
Enter your choice: s
After Subtraction:
Original Array1: 6 2 2 6 8
Original Array2: 6 8 2 3 7
Result Array: 0 -6 0 3 1
H or h for Help
S or s for Size of array
A or a for Arithmetic Operations
Q or q for Quit
Enter your choice: q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.