This program manipulates an array of integers, performing various transformation
ID: 3568352 • Letter: T
Question
This program manipulates an array of integers, performing various transformations on the values within the
array. For this program, there will be only one array. In your main function, define a constant MAXSIZE for
an array. Please initialize the MAXSIZE to10. Initialize the array to contain all values equal 0
A user selects a transformation to apply to the array from a menu as illustrated
in the following screen shot.
The program will continue to prompt the user for a transformation until the user chooses to exit.
The transformations are described in detail below:
Explanation / Answer
#include <iostream>
using namespace std;
// prototypes for all the methods
void populate(int* arr, int size);
void cube(int* arr, int size);
void maximum(int* arr, int size);
void minimum(int* arr, int size);
void reverse(int* arr, int size);
void clear(int* arr, int size);
void printArray(int* arr, int size);
int main() {
const int MAXSIZE = 10; //initializing the constant MAXSIZE for array to 10
int array[MAXSIZE]; // array of integers
bool flag = true;
int choice;
// Initializing the array to contain all values equal 0
for(int i = 0; i < MAXSIZE; i++)
{
array[i] = 0;
}
do{
// Transformations menu
cout << "Menu : " << endl;
cout << " 0. Populate" << endl;
cout << " 1. Cube" << endl;
cout << " 2. Maximum" << endl;
cout << " 3. Minimum" << endl;
cout << " 4. Reverse" << endl;
cout << " 5. Clear" << endl;
// prompt the user for a transformation
cout << "Enter your choice : " ;
cin >> choice;
cout << endl;
// calling the appropriate transformation method requested by user
switch(choice)
{
case 0 : populate(array,MAXSIZE); //calling populate method
break;
case 1 : cube(array,MAXSIZE); // calling cube method
break;
case 2 : maximum(array,MAXSIZE); // calling maximum method
break;
case 3 : minimum(array,MAXSIZE); // calling minimum method
break;
case 4 : reverse(array,MAXSIZE); // calling reverse method
break;
case 5 : clear(array,MAXSIZE); // calling clear method
break;
default : flag = false; // flag which exits the program if user enters other than 0-5
}
}while(flag); // program will continue until user chooses to exit.
return 0;
}
//0-populate-Populate the array to contain values entered by the user
void populate(int* arr, int size)
{
for(int i = 0; i < size; i++)
{
cout << "Enter number " << (i+1) << " : ";
cin >> arr[i];
}
cout << "Array after populating : ";
printArray(arr, size); // printing the array
}
//1-cube
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.