Join 117,527 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,984 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
I have to do the following assignment for school, and I cant seem to figure out how to get started. I'm going to continue to serch through th forums and do some google searches, but I wanted to go ahead and post to get ahead start. These are the guidelines for the assignment:
1.Create an empty vector
2.Add values that the user inputs to the vector until the user enters "0.0"
3.After a "0.0" is entered, the user is done adding values and the vector is "finished"
4.Print the values in the vector to the screen
5.Loop through each of those values and find both the average of all the values and the value that is the highest number and output that information to the screen
1) #include <vector> 2) A loop to prompt the user continuously for a float value 3) A float variable to hold their input 4) A vector variable needs to be defined 5) The push_back method of the vector 6) A vector<float>::iterator variable. 7) A second loop to iterator across the vector and add them to a sum variable. 8) size() function of the vector (sum / size() = avg) 9) A variable to hold the "max" value. Hint: Initialize it to the first value in the vector and compare all other values against it.
That should be enough to get you started. Enjoy!
"At DIC we be vector wielding code ninjas... we also wielded 'Victor' and threw him out the window. Lamahhhh"
// Now that we have the number from the user, // append it at the end of the vector
input_numbers.push_back(number);
cout << "Do you want to enter more numbers (y/n)? "; cin >> answer;
cin.get(); }
for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } // '\t' is the code for the TAB character
Check this out (need to iterate over the vector and add it for the average and there are many many ways to sort it, I chose the simplest one I could think of to illustrate the power of STL built in container functions):
// Now that we have the number from the user, // append it at the end of the vector
input_numbers.push_back(number); totalNums++;
cout << "Do you want to enter more numbers (y/n)? "; cin >> answer;
cin.get(); }
for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } // '\t' is the code for the TAB character
//iterate over and add all values to get average number for (int i = 0; i < input_numbers.size(); i++) { total += input_numbers[i]; }
//Neat little built in sort thing, see function at top sort(input_numbers.begin(), input_numbers.end(), sortFunction); //output sorted list for debugging purposes cout << "\nSorted vector:\n"; for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } average = total/totalNums; highest = input_numbers[0]; cout << "\nAverage of this vector is: " << average; cout << "\nHighest number in the vector is: " << highest; cin.get();
// Now that we have the number from the user, // append it at the end of the vector
input_numbers.push_back(number);
cout << "Do you want to enter more numbers (Y/N)? "; cin >> answer;
cin.get(); }
for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } // '\t' is the code for the TAB character
//iterate over and add all values to get average number for (int i = 0; i < input_numbers.size(); i++) { total += input_numbers[i]; }
//Neat little built in sort thing, see function at top sort(input_numbers.begin(), input_numbers.end(), sortFunction); //output sorted list for debugging purposes cout << "\nSorted vector:\n"; for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } average = total/totalNums; highest = input_numbers[0]; cout << "\nAverage of this vector is: " << average; cout << "\nHighest number in the vector is: " << highest; cin.get();
// Now that we have the number from the user, // append it at the end of the vector
input_numbers.push_back(number);
cout << "Do you want to enter more numbers (Y/N)? "; cin >> answer;
cin.get(); }
for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } // '\t' is the code for the TAB character
//iterate over and add all values to get average number for (int i = 0; i < input_numbers.size(); i++) { total += input_numbers[i]; }
//Neat little built in sort thing, see function at top sort(input_numbers.begin(), input_numbers.end(), sortFunction); //output sorted list for debugging purposes cout << "\nSorted vector:\n"; for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } average = total/input_numbers.size(); highest = input_numbers[0]; cout << "\nAverage of this vector is: " << average; cout << "\nHighest number in the vector is: " << highest; cin.get();
Thank you for all your help. The program runs great, but I think I'm gonna have to make another change. I sent my instructor an email asking to be able to use the Yes/No option for inputing numbers into the vector. He said no, that I needed to use a loop to keep asking for input numbers until the user inputs 0.0
bool sortFunction(int i, int j) { return (i > j); }
int main() { vector<float> input_numbers; float number; char answer; float highest; float total = 0.0; float average;
cout << "Please enter your number: If no other number is to be entered, Enter 0 " << endl; cin >> number;
while (number != 0) {
input_numbers.push_back(number);
cout << "Please enter your number: If no other number is to be entered, Enter 0 " << endl; cin >> number;
cin.get(); }
for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } // '\t' is the code for the TAB character
//iterate over and add all values to get average number for (int i = 0; i < input_numbers.size(); i++) { total += input_numbers[i]; }
//Neat little built in sort thing, see function at top sort(input_numbers.begin(), input_numbers.end(), sortFunction); //output sorted list for debugging purposes cout << "\nSorted vector:\n"; for (int i = 0; i < input_numbers.size(); i++) { cout << "Value #" << i+1 << '\t' << input_numbers[i] << endl; } average = total/input_numbers.size(); highest = input_numbers[0]; cout << "\nAverage of this vector is: " << average; cout << "\nHighest number in the vector is: " << highest; cin.get();