Question: C++ Vectors Problem: Hello: This question might seem very long but I actually have most of it done. I just need help with a part

C++ Vectors Problem:

Hello: This question might seem very long but I actually have most of it done. I just need help with a part of the code, please continue reading, thank you for your patience!

*Edit: Please write the code in basic vector code! I am just a beginner, thank you!

Below is the link to the question:

http://imgur.com/NqsDr2i

I already have most of the code done, but I need help with the part of searching a specific number in the function: I will first post my code below: ( Please note that chegg takes away the smaller sign and greater sign for no reason, I have the functions declared.

#include #include #include #include using namespace std;

int countEven(vector v); int countOdd(vector v); int calclargest(vector v); int calcsmallest(vector v); int calctotal(vector v); int calcaverage(vector v);

int main() { int largest,smallest,total, even , odd ; double average; srand(time(0)); vector v; for (int i = 0; i < 5000; i++) { v.push_back(rand()); } smallest = calcsmallest(v); largest = calclargest(v); total = calctotal(v); even = countEven(v); odd = countOdd(v); average = calcaverage(v); cout << "Smallest Number: " << smallest << endl; cout << "Largest Number: " << largest << endl; cout << "Total Number: " << total << endl; cout << "Number of Evens: " << even << endl; cout << "Number of Odds: " << odd << endl; cout << "Average of the Values: " << average << endl;

return 0; }

int countEven(vector v) { int even = 0; for (int i = 0; i < 5000; i++) { if (v[i] % 2 == 0) even++; } return even; }

int countOdd(vector v) { int odd = 0; for (int i = 0; i < 5000; i++) { if (v[i] % 2 == 1) odd++; } return odd; }

int calclargest(vector v) { int largest = 0; for (int i = 0; i < 5000; i++) { if (v[i] > largest) largest = v[i]; } return largest; }

int calcsmallest(vector v) { int largest = 0; int smallest = 0; for (int i = 0; i < 5000; i++) { if (v[i] > largest) largest = v[i]; else smallest = v[i]; } return smallest; }

int calctotal(vector v) { int total = 0; for (int i = 0; i < 5000; i++) { total += v[i]; } return total; }

int calcaverage(vector v) { double average = 0; int total = 0; for (int i = 0; i < 5000; i++) { total += v[i]; } average = total / 5000; return average; }

I have tested this and it completely works, and our teacher has posted a help function online. But I really can't understand how to use it. Please insert the following funciton into my code so that it can search for a specific number and locate it. Thank you!

This is the code our teacher posted:

void shellSort(vector& V) { bool flag = true; int i, numLength = V.size();

int d = numLength; while (flag || (d>1)) // bool flag { flag = false; //reset flag to 0 to check for // future swaps d = (d + 1) / 2; for (i = 0; i < (numLength - d); i++) { if (V[i + d] < V[i]) { swapper(V[i], V[i + d]);

flag = true; //tells swap has occurred } } } } void swapper(int &a, int &b) { int temp; temp = a; a = b; b = temp; }

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!