This is a C++ program to be done with Microsoft Visual Studio. This program mani
ID: 3544648 • Letter: T
Question
This is a C++ program to be done with Microsoft Visual Studio.
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 to 20. Initialize the array to contain the values from 0 to MAXSIZE - 1, in
array positions 0 to MAXSIZE - 1, i.e., array position 3 has the value 3.
A user selects a transformation to apply to the array from a menu.
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
Repost since i believe i did the accumulate function incorrectly
#include <iostream>
using namespace std;
void printMenu()
{
cout << " Array Manipulation Program "
<< "1.Initialize "
<< "2.Square "
<< "3.Halve "
<< "4.Accumulate "
<< "5.Transpose "
<< "6.Shift "
<< "7.Reverse "
<< "8.Quit "
<< "Enter your choice: ";
}
void init(int *arr, int size)
{
for(int i = 0; i < size; i++)
arr[i] = i;
}
void square(int *arr, int size)
{
int temp;
for(int i = 0; i < size; i++){
temp = arr[i];
arr[i] = temp*temp;
}
}
void half(int *arr, int size)
{
int temp;
for(int i = 0; i < size; i++){
temp = arr[i];
arr[i] = temp/2;
}
}
void accum(int *arr, int size)
{
int temp;
for(int i = size-1; i > 0; i--){
temp = 0;
for(int j = 0; j <= i; j++){
temp += arr[j];
}
arr[i] = temp;
}
}
void trans(int *arr, int size)
{
int temp;
for(int i = 0; i < size; i+=2){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
void shift(int *arr, int size)
{
int temp;
//save last
temp = arr[19];
//shift all other values right
for(int i = size-1; i > 0; i--){
arr[i] = arr[i-1];
}
arr[0] = temp;
}
void reverse(int *arr, int size)
{
int temp;
for(int i = 0; i < size/2; i++){
temp = arr[i];
arr[i] = arr[size-i-1];
arr[size-i-1] = temp;
}
}
int main()
{
const int MAXSIZE = 20;
int arr[MAXSIZE];
int choice;
//first initialize
init(arr, MAXSIZE);
//while loop until quit command is applied
while(1){
cout << " Current array: ";
for(int i = 0; i < MAXSIZE; i++){
cout << arr[i] << " ";
}
cout << endl;
printMenu();
do{
cin >> choice;
//condition to force user to enter another integer if not in range
if(choice < 1 || choice > 9){
cout << "Choice is not valid please try again. "
<< "Enter your choice: ";
}
else if(choice == 8){
cout << "Exiting program.... ";
return 1;
}
}while(choice < 1 || choice > 8);
//switch to do function depending on choice
switch(choice){
case 1:
init(arr, MAXSIZE);
break;
case 2:
square(arr, MAXSIZE);
break;
case 3:
half(arr, MAXSIZE);
break;
case 4:
accum(arr, MAXSIZE);
break;
case 5:
trans(arr, MAXSIZE);
break;
case 6:
shift(arr, MAXSIZE);
break;
case 7:
reverse(arr, MAXSIZE);
break;
default:
cout << "This should never reach, if it does we have a problem...";
exit(-1);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.