You are here:

C++/Printing out the user's input

Advertisement


Question
QUESTION: 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.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.