Output is Incorrect

The output is either a 0 or a 1. Never outputs the correct answer, which is suppose to be the standard deviation of 4 values. Here is the code. Is my calculation incorrect?

#include <iostream>
#include <cmath>
using namespace std;

void input(double score1, double score2, double score3, double score4);
void calc(double& stdDeviation, double score1, double score2, double score3, double score4, double avg, double data);
void output(double stdDeviation, double score1, double score2, double score3, double score4);

int main()
{
char selection;
double score1, score2, score3, score4;
double stdDeviation, avg, data;

//Calling The Funtions
input(score1, score2, score3, score4);
calc(stdDeviation, avg, score1, score2, score3, score4, data);
output(stdDeviation, score1, score2, score3, score4);

//Repeating The Program
cout << endl;
cout << "If you want to try again press Y\n";
cout << "If you wish to end the program press N\n";
cout << endl;

cin >> selection;

if(selection == 'Y')//If "Y" is used, program will repeat itself
{
system ("cls");
main();
}
else //If anything else is pressed, program will end
{
cout << "Goodbye\n";
}
}
void input(double score1, double score2, double score3, double score4)
{
cout << "Hello! This program will tell you the standard deviation within\n"; //Explaining the program
cout << "two values.";
cout << endl;
cout << "Please enter four values. Any number at all:\n"; //Prompting User
cin >> score1 >> score2 >> score3 >> score4;
cout << endl;
cout << "Your values are " << score1 << ", " << score2 << ", " << score3 << " and " << score4;
cout << endl;
}
void calc(double& stdDeviation, double avg, double score1, double score2, double score3, double score4, double data)
{
avg = (score1 + score2 + score3 + score4) / 4;
data = (pow(score1 - avg, 2) + pow(score2 - avg, 2) + pow(score3 - avg, 2) + pow(score4 - avg, 2)) / 4;
stdDeviation = sqrt(data);
}
void output(double stdDeviation, double score1, double score2, double score3, double score4)
{
cout << endl;
cout << "Your standard deviation for the four values is " << stdDeviation;
cout << endl;
}
You're passing score1, score2, score3 and score4 into input by value. These means that if you change the values of those variables inside input - which you do - those changes are not reflected in the values of the variables in main. The variables inside input are copies of the ones in main, not the same variables.

You need to pass by reference, using either pointers or references.
Last edited on
Topic archived. No new replies allowed.