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
using namespace std;
void randomIntVector(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
// sorting an int vector. void sortIntVector(vector
// 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
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
void randomIntArray(int* A, int size) { for (int i = 0; i < size; ++i) A[i] = i + rand() % (size * 10); }
int sumOfIntVector(const vector
return sum; }
void printIntVector(const vector
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
Get step-by-step solutions from verified subject matter experts
