Click here to Skip to main content
15,922,894 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questiontrouble with calculations Pin
lindag178313-May-06 2:30
lindag178313-May-06 2:30 
AnswerRe: trouble with calculations Pin
Justin Tay13-May-06 2:56
Justin Tay13-May-06 2:56 
GeneralRe: trouble with calculations Pin
lindag178313-May-06 15:10
lindag178313-May-06 15:10 
AnswerRe: trouble with calculations Pin
Gary R. Wheeler13-May-06 3:04
Gary R. Wheeler13-May-06 3:04 
GeneralRe: trouble with calculations Pin
lindag178313-May-06 15:16
lindag178313-May-06 15:16 
GeneralRe: trouble with calculations Pin
Gary R. Wheeler14-May-06 1:27
Gary R. Wheeler14-May-06 1:27 
GeneralRe: trouble with calculations Pin
lindag178314-May-06 4:44
lindag178314-May-06 4:44 
GeneralRe: trouble with calculations Pin
Gary R. Wheeler14-May-06 6:13
Gary R. Wheeler14-May-06 6:13 
using namespace std;
int main(void)

{

double M, N, total, score1, score2, score3, score4, score5, score6, score7, score8,
       V1, V2, V3, V4, V5, V6, V7, V8, s1, s2, s3, s4, s5, s6, s7, s8, Variance;

score1 = 0.0;
score2 = 0.0;
score3 = 0.0;
score4 = 0.0;
score5 = 0.0;
score6 = 0.0;
score7 = 0.0;
score8 = 0.0;

cout << "Input the number of people in sample ";
cin >> N;
cout << "input the 1st score ";
cin >> score1;
cout << "input the 2nd score ";
cin >> score2;
cout << "input the 3rd score ";
cin >> score3;
cout << "input the 4th score ";
cin >> score4;
cout << "input the 5th score ";
cin >> score5;
cout << "input the 6th score ";
cin >> score6;
cout << "input the 7th score ";
cin >> score7;
cout << "input the 8th score ";
cin >> score8;
total = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8;
M = total / N;
cout << "the mean for these scores is: " << M << "\n";
s1 = score1 - M;
V1 = s1 * s1;
s2 = score2 - M;
V2 = s2 * s2;
s3 = score3 - M;
V3 = s3 * s3;
s4 = score4 - M;
V4 = s4 * s4;
s5 = score5 - M;
V5 = s5 * s5;
s6 = score6 - M;
V6 = s6 * s6;
s7 = score7 - M;
V7 = s7 * s7;
s8 = score8 - M;
V8 = s8 * s8;
Variance = (V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8) / N;
cout << "the variance for these scores is: " << Variance << "\n";
return 0;

}
My modifications are highlighted in bold.

To give you another view, here is how I would do this:
int main(void)
{
  int N = 0;
  double score[8];
  for (int i = 0; i < 8; i++) score[i] = 0.0;
  cout << "Enter number of people in sample [1-8] ";
  cin >> N;
  if ((N > 0) && (N <= 8)) {
    double total = 0;
    for (int i = 0; i < N; i++) {
      cout << "Enter score " << (i + 1) << " ";
      cin >> score[i];
      total = total + score[i];
    }
    double mean = total / (double)N;
    double variance = 0.0;
    for (int i = 0; i < N; i++) {
      variance = variance + ((score[i] - mean) * (score[i] - mean));
    }
    variance = variance / (double)N;
    cout << "Variance is " << variance;
  }
  else {
    cout << "Incorrect number of samples.";
  }
}
First, instead of having 8 discrete variables score1 through score8, I use an array. This makes it easier to step through each value using a for loop. Next, I initialize the score array, even though the logic doesn't strictly require it; it's just good practice to always initialize your variables. I then prompt the user for the number of samples. Note that I tell them the range of values that they may enter, e.g. the [1-8]. The if statement ensures that the number they enter is valid; if not, they get an error message. This is an example of a fundamental principle: always validate input from a user. The for loop that follows prompts them for N samples. Note that I compute the 'total' of the scores as the user enters them. I then compute the mean of the scores after the for loop. The next for loop computes the sum of the squares of the deviation values, and the statements after the for computes and outputs the variance.

Taking a guess, it sounds like this is one of your very first programs. My first program was something similar; let the user enter three test scores, and print the average. My intention in posting my alternative was not to do your homework for you, but to give you an example to think about.

Good luck.



Software Zen: delete this;
GeneralRe: trouble with calculations Pin
lindag178314-May-06 15:33
lindag178314-May-06 15:33 
GeneralRe: trouble with calculations Pin
lindag178314-May-06 15:46
lindag178314-May-06 15:46 
GeneralRe: trouble with calculations Pin
Gary R. Wheeler15-May-06 13:00
Gary R. Wheeler15-May-06 13:00 
AnswerRe: trouble with calculations Pin
Stephen Hewitt13-May-06 5:47
Stephen Hewitt13-May-06 5:47 
AnswerRe: trouble with calculations Pin
Saurabh.Garg13-May-06 16:20
Saurabh.Garg13-May-06 16:20 
GeneralRe: trouble with calculations Pin
lindag178313-May-06 21:29
lindag178313-May-06 21:29 
GeneralRe: trouble with calculations Pin
Justin Tay13-May-06 23:59
Justin Tay13-May-06 23:59 
GeneralRe: trouble with calculations Pin
lindag178314-May-06 0:46
lindag178314-May-06 0:46 
GeneralRe: trouble with calculations Pin
Saurabh.Garg14-May-06 15:04
Saurabh.Garg14-May-06 15:04 
Questionmysql database Pin
J512198213-May-06 2:13
J512198213-May-06 2:13 
AnswerRe: mysql database Pin
Hamid_RT13-May-06 3:58
Hamid_RT13-May-06 3:58 
AnswerRe: mysql database Pin
Kleser13-May-06 4:56
Kleser13-May-06 4:56 
Questiongetstdhandle returns INVALID_HANDLE_VALUE Pin
e_prabhu13-May-06 2:02
e_prabhu13-May-06 2:02 
AnswerRe: getstdhandle returns INVALID_HANDLE_VALUE Pin
Hamid_RT13-May-06 5:01
Hamid_RT13-May-06 5:01 
AnswerRe: getstdhandle returns INVALID_HANDLE_VALUE Pin
Ryan Binns13-May-06 22:21
Ryan Binns13-May-06 22:21 
QuestionError in List Box Pin
Anu_Bala13-May-06 1:57
Anu_Bala13-May-06 1:57 
AnswerRe: Error in List Box Pin
Hamid_RT13-May-06 3:32
Hamid_RT13-May-06 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.