Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Split the main function given into multiple functions. You have been given a very simple program that performs basic operations (addition, subtraction, editing) on two

  1. Split the main function given into multiple functions.

You have been given a very simple program that performs basic operations (addition, subtraction, editing) on two randomly generated integer vectors. All functionality has been included in main, causing code segments to be repeated as well as diminishing the readability.

Rewrite the program by grouping calculations and related operations into functions. In particular, your program should include the following functions.

  • InitializeVectors: This is a void function that initializes the two vectors by random numbers. Inside this function, the user will be prompted to enter the maximum random number. After the vectors have been populated with random numbers, print the vectors side by side. The parameters are the two arrays and their size.
  • EditVector: This is a void function that allows the user to update a value belonging to either vector. The user specifies the vector he wants to edit, the index he wants edit, and finally the updated value. The entire vector must then be printed on the screen. You need to pass the two arrays and their sizes.
  • CalculateAverage: This is a function that returns the average value in a vector. It returns a double and receives as parameters an array and its size.
  • printVector: A void function that takes a vector as a parameter along with its size, and prints it on the screen.

As you introduce each function, replace the code in main() by the appropriate function call. Also, keep in mind that for some functions, any changes that occur within the function body must also be visible in main. Finally, some functions can be called within other functions.

Remember that this program was already working. You should not alter the code, you just need to restructure it.

By the end of this assignment, the programs structure and functionality will be more transparent, making it both easier and faster to understand.

Here is the code I need altered:

#include  #include  #include  #include  using namespace std; int main() { 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; GetSize: cout<<"Enter size for vectors: "; cin>>v_size; if(cin.fail() == true) { system("CLS"); cout<<"Invalid Input. Please Re-enter vector size: "; cin.clear(); cin.ignore(10000, ' '); goto GetSize; } GetCeiling: cout<<"Enter Maximum Random Number for Vector: "; cin>>ceiling; if(cin.fail() == true) { system("CLS"); cout<<"Invalid Input. Please Re-enter Maximum Random Number: "; cin.clear(); cin.ignore(10000, ' '); goto GetCeiling; } int v1[v_size]; int v2[v_size]; int result[v_size]; int run_sum =0; //randomly initialize the vectors srand (time(NULL)); //seed for(int i=0; i>action; //system("CLS"); if(cin.fail() == true || action<-1 || action>5) { system("CLS"); cout<<"Invalid Input. Please Re-enter Desired Action: "; cin.clear(); cin.ignore(10000, ' '); goto GetAction; } if(action == -1) goto end; if(action == 0) { for(int i=0; i>v_id; if(cin.fail() == true || (v_id!=1 && v_id!=2)) { system("CLS"); cout<<"Invalid Input. Please Re-enter Vector Selection(1 or 2): "; cin.clear(); cin.ignore(10000, ' '); goto getVector; } if(v_id == 1 ) for(int i=0; i>v_id; if(cin.fail() == true || (v_id!=1 && v_id!=2)) { system("CLS"); cout<<"Invalid Input. Please Re-enter Vector Selection(1 or 2): "; cin.clear(); cin.ignore(10000, ' '); goto GetVector; } GetIndex: cout<<"Select Index to be Updated: "<>index; if(cin.fail() == true || index<0 || index>(v_size+1)) { system("CLS"); cout<<"Invalid Input. Please Re-enter index to be updated: "; cin.clear(); cin.ignore(10000, ' '); goto GetIndex; } cout<<"Enter updated value: "; if(v_id == 1) { cin>>v1[index]; for(int i=0; i< v_size; i++) cout<>v2[index]; for(int i=0; i< v_size; i++) cout<                        

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

=+1 What would you do if you were the IHR manager?

Answered: 1 week ago