1. Engineering
  2. Computer Science
  3. task description you have been given a very simple program...

Question: task description you have been given a very simple program...

Question details

Task Description:

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 program’s structure and functionality will be more transparent, making it both easier and faster to understand.

Submission notes for Task:

  • Zip the entire Code::Blocks project containing all the .cpp, .h, .cbp files and name the zipped file “Assg0_cslogin.zip”, where the cslogin is your login ID for the computers at the Department of Computer Science at ODU. Submission with a different file name other than Assg0_cslogin will receive -10 points off your final points for the assignment.
  • Submit the zipped file using the appropriate Blackboard link.
  • Using global variables will cause a 10 point deduction to your final score.
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <bits/stdc++.h>

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, '\n');
        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, '\n');
        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<v_size; i++)
    {
        v1[i]=rand() % ceiling;
        v2[i]=rand() % ceiling;
        cout<<setw(5)<<v1[i]<<setw(5)<<v2[i]<<endl;
    }

    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)
    {
        system("CLS");
        cout<<"Invalid Input. Please Re-enter Desired Action: ";
        cin.clear();
        cin.ignore(10000, '\n');
        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))
        {
        system("CLS");
        cout<<"Invalid Input. Please Re-enter Vector Selection(1 or 2): ";
        cin.clear();
        cin.ignore(10000, '\n');
        goto getVector;
        }
        if(v_id == 1 )
            for(int i=0; i<v_size; i++)
                run_sum += v1[i];

        else if(v_id == 2)
            for(int i=0; i<v_size; i++)
                run_sum += v2[i];

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

    else if(action == 3)
    {
        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))
        {
        system("CLS");
        cout<<"Invalid Input. Please Re-enter Vector Selection(1 or 2): ";
        cin.clear();
        cin.ignore(10000, '\n');
        goto GetVector;
        }
        GetIndex:
        cout<<"Select Index to be Updated: "<<endl;
        cin>>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, '\n');
        goto GetIndex;
        }
        cout<<"Enter updated value: ";
        if(v_id == 1)
        {
            cin>>v1[index];
            for(int i=0; i< v_size; i++)
                cout<<v1[i]<<endl;

        }

        else if(v_id == 2)
        {
             cin>>v2[index];
             for(int i=0; i< v_size; i++)
                cout<<v2[i]<<endl;
        }
    }
    if(action == 4)
         for(int i=0; i<v_size; i++)
             cout<<setw(5)<<v1[i]<<setw(5)<<v2[i]<<endl;


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

    goto GetAction;
    end:
    return 0;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution