Where is my mistake in calculating the variance of 4 values? [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I get the correct output for mean, but I do not get the correct output for variance. Sample input is 8, 10, 12, 14.
It should output the mean as 11, and variance as 5. However, I get the mean is 11, and variance is 6.66667.
// Include statements
#include <iostream>
#include <cmath>
using namespace std;
// Main function
int main ()
{
// Input values
float n1, n2, n3, n4;
cout << "Enter four numbers:\n";
cin >> n1 >> n2 >> n3 >> n4;
// Calculate mean of 4 values
float mean;
mean = (n1+n2+n3+n4)/4;
// Calculate variance of 4 values
float variance;
float var1 = ((n1-mean)*(n1-mean));
float var2 = ((n2-mean)*(n2-mean));
float var3 = ((n3-mean)*(n3-mean));
float var4 = ((n4-mean)*(n4-mean));
variance = (var1+var2+var3+var4)/3;
// Print the output
cout << "mean = " << mean << endl;
cout << "variance = " << variance << endl;
return 0;
}

You've divided var1+var2+var3+var4 by 3 rather than 4.

Related

C++ different output on computer and on school coderunner [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
I have an exercise to do to implement the Gregory-Leibniz series approximation of Pi. This is the code I have:
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
int k = 0;
int d = 0;
std::cin >> k;
std::cin >> d;
double sum = 0;
for(int i;i<=k;i++){
sum = sum + (pow(-1, i) / (2*i + 1));
}
sum = 4 * sum;
std::cout << std::fixed << std::setprecision(d) << sum << "\n";
return 0;
}
On my computer, for example, the input "1 3" outputs result "2.667", but on the school coderunner website, the output is always 0s, so in this it's "0.000". Can someone please help me?

Expect four printouts from while loop, getting only one [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to write a program using a while loop that gives an output based on range of input variable declared.
#include <iostream>
using namespace std;
int main() {
float fahren = 0;
float celsius;
while (fahren < 400) {
// convert fahreneheit to celsius
// Subtract 32, then multiply it by 5, then divide by 9
celsius = 5 * (fahren - 32) / 9;
cout << celsius;
fahren+= 100;
}
return 0;
}
I expect 4 values to be returned, however I only get 1, which is the value at 0.
You print 4 values but without separator, you might add std::cout << std::endl;
while (fahren < 400) {
// convert fahreneheit to celsius
// Subtract 32, then multiply it by 5, then divide by 9
celsius = 5 * (fahren - 32) / 9;
std::cout << celsius << std::endl; // added new line here
fahren += 100;
}
Demo

Getting wrong output in any math operation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
#include
#include
using namespace std;
int main()
{
int x ;
int y ;
int sum ;
sum = x-y ;
cout << "Enter your first number here: ";
cin >> x ;
cout << "Enter your second one : ";
cin >> y;
cout << "your result is " << sum << endl;
system("pause") ;
}
Now when i run this program:
Enter your first number here: 88
Enter your second one : 22
your result is 2221280 .
i've searched a lot but don't know what to do .
C++ arithmetic on builtin types isn't symbolic (like syms library in MATLAB if you're familiar with that). It's numeric. sum = x - y doesn't establish a relationship that causes sum to be updated whenever x or y are updated. It takes the value of x and y at that moment, executes the operation, and assigns it to sum. The relationship between sum, x, and y is ended after this transaction.
As an aside this should be called difference not sum, since you're subtracting.

I keep getting the same answer, no matter what I put in [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
We're doing an assignment in my computer science class that requires us to find the future value of an investment after "n" number of years that the user inputs. It's written in C++. This is the code as I have it now:
#include <iostream>
using namespace std;
int main() {
int P=1000;
int i=0.0275;
int FV;
int n;
cout << "enter number of years:"<< endl;
cin >> n;
cout << "the future value is:"<< endl;
FV = P*(1+(i*n));
cout << FV << endl;
return 0;
}
I keep ending up with an answer of 1000 no matter what "n" I input. Can someone tell me what's wrong with the code?
#include <iostream>
using namespace std;
int main() {
int P=1000;
float i=0.0275; //float instead of int
float FV; //FV should also be float as it will be storing decimal values
int n;
cout << "enter number of years:"<< endl;
cin >> n;
cout << "the future value is:"<< endl;
FV = P*(1+(i*n));
cout << FV << endl;
return 0;
}
the mistake you have done is the type you assigned to your variables! as int only handles integer values i becomes 0 and your result becomes 1000! use float instead of int for numbers with decimal points!
Datatype of i is int as a result of which your floating point value of i will be rounded of to 0 and you will end up getting the same output doesn't matter what your n value is. Change the datatype of your i and FV varaiable from int to float then your output changes based on what n value you key in

Why does left shift float value work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
As I understand from
Left shift Float type
one cannot use left shift operator on float values. But when I tried it, it gave the same answer as multiplying by 2n.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
float a = 1.1234;
int b = (int)(a*(1<<10));
int c = (int)(a*pow(2,10));
cout << "\n a = " << a << " b = " << b << " c = " << c;
return 0;
}
It outputs a = 1.1234 b = 1150 c = 1150
In which case will the two outputs (b and c) differ?
int b = (int)(a*(1<<10));
Here, since both 1 and 10 are integers, you are performing left shift operation on integer instead of on floating-point number.
you are multiplying 1024 with value of a(1.1234) in both case.
it does not mean you are shifting float value.