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
Related
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?
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 4 years ago.
Improve this question
In C++, why does dividing 2 floating point numbers via 2 different mechanisms (inline and via a function call) output different results as per below?
After the following computations where 5 is divided into 4, f1 = 0 and f2 = 0.8.
#include <iostream>
using namespace std;
int main() {
float f1 = 4 / 5;
float f2 = FloatDivision(4, 5);
cout << "f1 equals " << f1 << endl;
cout << "f2 equals " << f2 << endl;
cin.ignore();
}
float FloatDivision(float f1, float f2) {
if (f2 != 0) {
return f1 / f2;
} else {
cout << "Error: The denominator cannot be zero" << endl;
return 0;
}
}
4 / 5 is integer division. You have defined them as such. Whereas in your division function you defined them as floats.
For f1, try
float f1 = 4.0 / 5.0;
in case of
float f1 = 4 / 5;
It is integer division which results in 0 and then it is converted to float and it remains 0.
in case of function call DivFloat, it takes float arguments, hence 4 and 5 are first converted to float and then division happens. Hence results 0.8.
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.
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
Here are the instructions:
Write a program that accepts an integer input from the keyboard and computes the sum of all the integers from 1 to that integer. Example, if 7 were input, the sum: 1 + 2 + 3 + 4 + 5 + 6 + 7 would be computed. Use a while or for loop to perform the calculations. Print out the result after the sum has been calculated. NOTE: if you input a large integer, you will not get the correct results.
What I can't figure out is how to ADD all of the integers together. Any help would be greatly appreciated.
//preprocessor directives
int main ()
{
//declare and initialize variables
int n, i;
int total;
//user input
cout << "Enter an integer: ";
cin >> n;
//compute sum of all integers from 1 to n
total=0;
for (i = 1; i <= n; i++)
cout << i;
return 0;
}
You add by using the += operator:
for (i = 1; i <= n; i++)
total += i;
cout << total;
Note this is short for total = total + i.
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.