Question: C++ using vectors, arrays, and functions Only code where it says your code below, no changes to existing code #include #include #include #include #include #include

C++ using vectors, arrays, and functions

Only code where it says "your code below", no changes to existing code

#include #include #include #include #include #include

using namespace std;

void randomIntVector(vector& A); // fill in random numbers for an int vector. void randomIntArray(int* A, int size); // fill in random numbers for an int array. void printIntVector(const vector& A); // print an int vector. void printIntArray(const int* A, int size); // print an int vector. int sumOfIntVector(const vector& A); // find sum of an int vector.

/* ======================================================================= Your are required to define the following three functions after main().

<<<<<< Your code below... >>>>> ======================================================================== */

/* ========================= This function will find the maximum and minimum with their indexes for a given integer vector.

@param A: the vector we need to find its maximum and minimum. @param max: used to return the maximum. @param min: used to return the minimum. @param i; used to return the index of maximum. @param j; used to return the index of minimum. */ void findMinMaxIntVector(const vector& A, int& max, int& i, int& min, int& j);

// sorting an int vector. void sortIntVector(vector &A);

// find the average of an int array. double averageIntArray(const int A[], int size);

int main() { // Print header. cout << "================================================================ "; cout << "This program will fill in data for an integer array or a vector, "; cout << "find the sum, maximum and minimum elements and average. Sorting "; cout << "is also implemented. "; cout << "================================================================ ";

const int SIZE = 10; // set size of the vector vector A(SIZE, 0); // declare vector A with SIZE, initialized with 0. srand((int)time(0)); // seed the rand() by using the time function. cout << "==> Generate some random numbers for vector A... "; randomIntVector(A); printIntVector(A);

cout << " ==> Find the sum of all elements in this vector... "; cout << "The sum is: " << sumOfIntVector(A) << endl;

cout << " ==> Find the maximum and minimum values in this vector... "; int max, min, max_i, min_i; // for max/min values and their indexes max = min = A[0]; max_i = min_i = 0;

//findMinMaxIntVector(A, max, max_i, min, min_i); cout << "The maximum value is: A[" << max_i << "] = " << max << endl; cout << "The minimum value is: A[" << min_i << "] = " << min << endl;

cout << " ==> Sort the vector in an ascending order... "; //sortIntVector(A); cout << "After sorting, the vector A: "; printIntVector(A);

cout << " ============================================ "; cout << "The following code is for integer array... "; cout << "============================================ ";

int size = 10; int* B = new int[size];

cout << " ==> Fill in some random numbers for array B... "; randomIntArray(B, size); printIntArray(B, size);

cout << fixed << setprecision(2); cout << " ==> Find the average of B... "; //cout << "The average is: " << averageIntArray(B, size) << " ";

delete B; // release the memory.

return 0; }

/* ============================================================== Please define the three functions declared before main().

<<<<<< Your code below... >>>>> ============================================================== */

//=================================================================== void randomIntVector(vector& A) { int SIZE = A.size(); for (int i = 0; i < SIZE; ++i) A[i] = i + rand() % (SIZE * 10); }

void randomIntArray(int* A, int size) { for (int i = 0; i < size; ++i) A[i] = i + rand() % (size * 10); }

int sumOfIntVector(const vector& A) { int sum = 0; // for the total for (size_t i = 0; i < A.size(); i++) sum = sum + A[i];

return sum; }

void printIntVector(const vector& A) { cout << "Elem #" << setw(10) << "Value" << endl; cout << "~~~~~~" << setw(10) << "~~~~~" << endl; for (size_t i = 0; i < A.size(); ++i) cout << setw(6) << i << setw(10) << A[i] << endl; }

void printIntArray(const int* A, int size) { cout << "Elem #" << setw(10) << "Value" << endl; cout << "~~~~~~" << setw(10) << "~~~~~" << endl; for (int i = 0; i < size; ++i) cout << setw(6) << i << setw(10) << A[i] << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!