1. Engineering
  2. Computer Science
  3. can you please fix this code to match the sample...

Question: can you please fix this code to match the sample...

Question details

Suggested Input: Vector Size Maximum Random Number 12 Sample Output: Enter size for vectors:5 Enter Maximun Random Number for Vector:12 9 11 6 1 11 1e nter Action: vector addition(0), vector subtraction(1), average(2), edit(3), print vectors(4), clear screen(5), exit(-1) Vector Addition: nter Actionvector additionte)vector Subtraction(l average2), edit3) print vectors) ), vector subtraction(1), average (2), edit(3), print 20 21 Vector Subtraction:

Enter Action: vector addition(e), vector subtraction(1), average(2), edit(3), print vectors (4), clear screen(5), exit(-1):1 Enter Action: vector addition(e), vector subtraction(1), average(2), edit(3), print vectors(4), clear sereen(5), exit(-1): Two consecutive Average Calculations: Requested vector 1 average first, then the same for vector 2 Enter Action: vector addition(e), vector subtraction(1), average(2), edit (3), print vectors(4), clear sereen(5), exit(-1):2 Average of Vector 1, or Vector 2 (Enter 1 or 2)? erage:9.6 Enter Action: vector addition(e), vector subtraction(1), average(2), edit(3), print vectors(4), clear screen(5), exit(-1):2 Average of Vector 1, or vector 2 《Enter 1 or 2)? erage:8. Enter Action: vector addition(e), vector subtraction(1), average(2), edit(3), print vectors (4), clear screen(S), exit(-1): Print Vectors: Enter Action: vector addition(), vector subtraction(1), average(2), edit(3), print vectors(4), clear screen(5), exit(-1):4 Enter Action: vector addition(), vector subtraction(1), average(2), edit(3), print vectors(4), clear screen(5), exit(-1):

Can you please fix this code to match the sample output I've attached

here is the code:

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <bits/stdc++.h>

using namespace std;


int v_size = 0; //variable for the size of the arrays, its the same for both so that we can do addition and subtraction
int v_id = 0; //use it to determine which vector to operate on, used for average computation
int ceiling = 0; //when generating random numbers to place in vector, this constraints the maximum number attainable by the random number generator
int action = 0; //determine which course of action the program will take
double avg = 0.0;

void InvalidInput(){
system("CLS");
cout<<"Invalid Input. Please Re-enter vector size: ";
cin.clear();
cin.ignore(10000, ' ');
}
void printVector(int v[], int size){
for(int i=0; i< size; i++)
cout<<setw(5)<<v[i]<<endl;
}

void printVectorSidebySide(int v1[],int v2[],int size){
for(int i=0; i<size; i++)
cout<<setw(5)<<v1[i]<<setw(5)<<v2[i]<<endl;
}

void InitializeVectors(int v1[],int v2[],int size){
GetCeiling:
cout<<"Enter Maximum Random Number for Vector: ";
cin>>ceiling;

if(cin.fail() == true)
{
InvalidInput();
goto GetCeiling;
}
//randomly initialize the vectors
srand (time(NULL)); //seed
for(int i=0; i<v_size; i++)
{
v1[i]=rand() % ceiling;
v2[i]=rand() % ceiling;
}
printVectorSidebySide(v1,v2,v_size);
}

void EditVector(int v1[],int v2[],int size){
int index;
GetVector:
cout<<"Which Vector would you like to edit? (Enter 1 or 2): ";
cin>>v_id;
if(cin.fail() == true || (v_id!=1 && v_id!=2))
{
InvalidInput();
goto GetVector;
}
GetIndex:
cout<<"Select Index to be Updated: "<<endl;
cin>>index;
if(cin.fail() == true || index<0 || index>(v_size+1))
{
InvalidInput();
goto GetIndex;
}
cout<<"Enter updated value: ";
if(v_id == 1)
{
cin>>v1[index];
printVector(v1,v_size);

}

else if(v_id == 2)
{
cin>>v2[index];
printVector(v2,v_size);
}
}


double CalculateAverage(int v[],int size){
int run_sum =0;
double average;
for(int i=0; i<size; i++)
run_sum += v[i];

average = (double)run_sum/(double)size;
return average;
}

int main()
{
GetSize:
cout<<"Enter size for vectors: ";
cin>>v_size;

if(cin.fail() == true)
{
InvalidInput();
goto GetSize;
}

int v1[v_size];
int v2[v_size];

int result[v_size];

InitializeVectors(v1,v2,v_size);

GetAction:
cout<<"Enter Action: vector addition(0), vector subtraction(1), average(2), edit(3), print vectors(4), clear screen(5), exit(-1): ";
cin>>action;
//system("CLS");
if(cin.fail() == true || action<-1 || action>5)
{
InvalidInput();
goto GetAction;
}

if(action == -1)
goto end;

if(action == 0)
{
for(int i=0; i<v_size; i++)
{
result[i]=v1[i]+v2[i];
cout<<result[i]<<endl;
}
}

else if(action == 1)
for(int i=0; i<v_size; i++)
{
result[i]=v1[i]-v2[i];
cout<<result[i]<<endl;
}

else if(action == 2)
{
getVector:
cout<<"Average of Vector 1, or Vector 2 (Enter 1 or 2)? "<<endl;
cin>>v_id;
if(cin.fail() == true || (v_id!=1 && v_id!=2))
{
InvalidInput();
goto getVector;
}
if(v_id == 1 )
avg = CalculateAverage(v1,v_size);


else if(v_id == 2)
avg = CalculateAverage(v2,v_size);

// avg = (double)run_sum/(double)v_size;
cout<<"Average: "<<avg<<endl;
// run_sum = 0; //reset
}

else if(action == 3)
{
EditVector(v1,v2,v_size);
}
if(action == 4)
printVectorSidebySide(v1,v2,v_size);

if(action == 5)
system("CLS");

goto GetAction;
end:
return 0;
}

I still need the InitialzeVectors, EditVector, CalculateAverage, and printVector functions.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution