C++/Printing out the user's input
Expert: vijayan - 2/2/2011
QuestionQUESTION: this is what I have so far:
// average2.cpp
// This program calculates the average of any number of numbers.
// Using the for structure
#include <iostream>
using namespace std;
#include <iostream>
int main()
{
int n, count;
float x, sum, avg;
cout << "Output the sum, mean, and standard deviation of any set of numbers.";
cout << "
";
cout << "
";
sum = 0;
cout << "Type how many numbers in the set?
";
cout << "Set = ? ";
cin >> n;
cout << "
";
cout << "Type each number, followed by the enter key. ";
cout << "
";
cout << "
";
for (count=1; count <= n; count++){
cout << "? ";
cin >> x;
sum = sum + count;
} //end for
cout << "
";
cout << " You have entered the following: ";
cout << "
";
cout << "
";
// this is where i input the code to list out what the user has typed for all
// seven numbers. I dont know what the code is.
cout << "
";
cout << "
";
avg = sum / n;
cout << "The average is " << avg << endl;
system("PAUSE");
return 0; //successful termination
} //end main
* I'm having trouble figuring out the code to print out the user's input printing out vertically.
*
ANSWER: To print out all the numbers entered by the user at the end, you need to store all these numbers somewhere as they are entered. Use a std::vector<> fot this.
See:
http://www.codeguru.com/cpp/cpp/cpp_mfc/arrays/article.php/c4071
#include <iostream>
#include <vector>
using namespace std ;
int main()
{
int n, count;
float x, sum, avg;
sum = 0;
cout << "Type how many numbers in the set? ";
cout << "Set = ? ";
cin >> n;
cout << "Type each number, followed by the enter key. ";
std::vector<int> numbers ; // ***********
for (count=1; count <= n; count++)
{
cout << "? ";
cin >> x;
sum = sum + count;
numbers.push_back(x) ; // ***********
} //end for
cout << " You have entered the following: ";
for( int i=0 ; i<n ; ++i ) cout << numbers[i] << ' ' ; // *********
cout << '\n' ;
avg = sum / n;
cout << "The average is " << avg << endl;
} //end main
---------- FOLLOW-UP ----------
QUESTION: After the user decides however many numbers he/she wants to input then types each number out, how can I get the program to printout the users numbers vertically instead of horizontally?
I'm also having problems trying to code my program to use only a 'single for-loop' to input data and calculate all the 'sum', 'mean', 'variance' and 'standard deviation'.
Thanks a lot for the help.
Answer> how can I get the program to printout the users numbers vertically instead of horizontally?
Change for( int i=0 ; i<n ; ++i ) cout << numbers[i] << ' ' ;
To for( int i=0 ; i<n ; ++i ) cout << numbers[i] << '\n' ;
Put a new line after each number (instead of a space).
> I'm also having problems trying to code my program to use only a 'single for-loop' to input data and calculate all the 'sum', 'mean', 'variance' and 'standard deviation'.
You will need two loops - one to accumulate the sum and compute the mean (average). Another to compute and accumulate the square of the number from the mean.