I am trying to write a program that rounds a number to a certain decimal place that was specified by the user. It is a requirement to do this with both cout.fset and cout.precision, as well as with round and pow functions. In the end, the output of the program should be something like this:
123.4567
2
123.46
123.46
My main function used cout.fset and cout.precision, and it works fine. The problem is my double rounding function which uses round and pow. For some reason I am getting the following output:
123.4567
2
123.46
0.00
Why is it printing just zeros in that last line? I am new to programming and C++ as a whole so I would appreciate your help. Thank you. (I have a feeling it is a minor/beginner mistake I am overlooking but this is the extent of my knowledge at the moment)
#include <iostream>
#include <math.h>
using namespace std;
double rounding(int pp){
double d;
double k = std::pow(10, pp);
return std::round(d * k) / k;
}
int main(){
double p;
double d, round;
cin >> d;
cin >> p;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(p);
cout << d << endl;
round = rounding(d);
cout << round << endl;
return 0;
}
d in the rounding function is not initialized and have indeterminate value.
Try this:
#include <iostream>
#include <math.h>
using namespace std;
double rounding(double d, int pp){
double k = std::pow(10, pp);
return std::round(d * k) / k;
}
int main(){
double p;
double d, round;
cin >> d;
cin >> p;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(p);
cout << d << endl;
round = rounding(d, d);
cout << round << endl;
return 0;
}
Related
I am instructed to write a program where the user inputs two values, can be with decimals. Then create a void function that changes the precision to 4 digits. I created a program and it runs the first value (x) just fine but for some reason, it's giving me an error saying the second variable is uninitialized (h) any advice would be appreciated! I think that I've been looking at it for too long and just can't spot it!
Below is the code:
#include <iostream>
#include<cmath>
using namespace std;
void display_it(double x, double h);
int main(void) // getting input from user and displaying output
{
double x, h;
cout << "Please enter 2 values to be displayed with precision.\n";
cin >> x, h;
cout << x << endl;
cout << h << endl;
return 0;
}
void display_it(double x,double h) //does the precision change of x and h
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);
}
This line:
cin >> x, h;
is not reading in 2 values from cin. It's actually only reading in 1 value, after which the expression h that's after the , is evaluated (which does nothing). So the warning/error about h being uninitialized is correct.
The correct way to read 2 values is:
cin >> x >> h;
why it is printing as cout<<(4*(double)(r*r))<<endl; as integer as something wrong with my typecast.
I have to take input r as an int
input->1 output->4
and why
cout<<setprecision(2)<<ab2<<endl; rounding the answer at least it should give correct answer till two digits because i have set set precision 2
input->1 output->3.8
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long int r;
cin>>r;
double ab2 = 4*(double)(r*r) - double(0.25);
cout<<4*(double)(r*r)<<endl;
cout<<setprecision(2)<<ab2<<endl;
cout<<ab2+0.25<<endl;
}
}
I'm guessing you're going to see something more like what you are expecting with this code
int main() {
cout << fixed << setprecision(2); // fixed notation and two decimal places
int t;
cin >> t;
while (t--) {
long long int r;
cin >> r;
double ab2 = 4 * (double)(r*r) - double(0.25);
cout << 4 * (double)(r*r) << endl;
cout << ab2 << endl;
cout << ab2 + 0.25 << endl;
}
}
Input
1 1
Output
4.00
3.75
4.00
why it is printing as cout<<(4*(double)(r*r))<<endl; as integer as
It is a double, so it is printing it as a double. It is however a double that represents a whole number.
and why
cout<<setprecision(2)<<ab2<<endl; rounding the answer
Because you've used smaller precision than the value has significant digits.
I am using the following code to print out the code, and i doesn't show the correct area of the circle. it shows -215487854145 as the area of the circle..
please help me
the code below code:
kindly help me as i am new to this language, i think i did everything right please
#include <iostream>
using namespace std;
int main()
{
int a, r;
a = 3.14 * r * r ;
cout << "enter Radius";
cin >> r;
cout << "area of circle is";
cout << a;
return 0;
}
Two issues.
You are computing a using an unitialised value of r. The program behaviour is undefined. Move it after the cin >> r; statement.
Working in int could cause you issues with overflow. The largest possible value of an int in C++ can be as small as 32767. Use a double instead, and an improved value of PI. Note that the type of 3.14 * r * r is a double anyway, and you're currently forcing a conversion to int.
As for PI itself, it is not included in the C++ standard library. Consider
constexpr double pi = 3.14159265358979323846264338328;
or take one from a mathematics library if you're using one.
You used r in a calculation before you ever read in the value. Move it after you read it in.
cin >> r;
a = 3.14 * r * r ;
Unlike in mathematics,
a = 3.14 * r * r ;
does not define a relationship between a and r (it's not an equation).
Instead, it means "replace the current value of a with 3.14 times the square of the current value of r".
Since you haven't given r a value yet, the result is undefined.
You need to move the lines around a bit in order to not use values that don't exist yet.
You should also not use integers for this, but floating-point.
double r;
cout << "enter Radius";
cin >> r;
double a = 3.14 * r * r ;
cout << "area of circle is " << a;
#include <iostream>
using namespace std;
int main()
{
int a, r;
// your r was not initialized when you use it.
a = 3.14 * r * r ;
cout << "enter Radius";
cin >> r;
cout << "area of circle is";
cout << a;
return 0;
}
right answer:
#include <iostream>
using namespace std;
int main()
{
int a, r;
cout << "enter Radius";
cin >> r;
//after r being initialized.
a = 3.14 * r * r;
cout << "area of circle is";
cout << a;
return 0;
}
I have this code (very basic):
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float a = 0.0,
b = 0.0,
c = 0.0;
cout<<"Input a: ";
cin>>a;
cout<<"input b: ";
cin>>b;
cout<<endl;
c = a / b;
cout<<"Result: "<<fixed<<setprecision(2)<<c<<endl;
return 0;
}
When I enter two numbers (say, a = 513 and b = 791) I get 0.65. Calculator shows that the correct answer is 0.648. I understand that my code rounds up the last decimal number but this is not what I want.
How can I get it to where it just stays as 0.64 and not 0.65?
If you would like to truncate the value to two decimal places, you can multiply it by 100, truncate to integer, and then divide by 100, like this:
c = a / b;
c = floor(100 * c) / 100;
cout<<"Result: "<<fixed<<setprecision(2)<<c<<endl;
Demo on ideone.
You can use trunc to truncate to a certain number of digits:
c = a / b;
// truncate past two decimals:
c = trunc(c * 100) / 100;
cout<<"Result: "<<fixed<<setprecision(2)<<c<<endl;
of for a generic function:
int trunc(double val, int digits)
{
double pow10 = pow(10,digits);
return trunc(val * pow10) / pow10;
}
then use
cout << "Result: " << fixed << setprecision(2) << trunc(c,2) << endl;
I thought I can make life a little easier in data statistics by making a small program which returns the results of sampling distribution of the mean (with standard error). It does this part successfully but in an attempt to return the z-score by using the formula I found here, it returns -1#IND. My interpretation of that formula is:
((1 / (sqrt(2 * pi) * stdev)) * pow(e, (normalpow))
where
double normalpow = -0.5 * ((mean - popmean) * (mean-popmean) / stdev)
I did a little more investigating and found that (mean - popmean) * (mean - popmean) was evaluating to 0 no matter what. How can I get around this problem of normalpow evaluating to 0.
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
double number ;
double mean ;
double popmean ;
double stdev ;
double square = 2;
double e = 2.71828182845904523536;
double pi = 3.14159265358979323846;
double normalpow = -0.5*((mean-popmean)*(mean-popmean)/stdev);
int main ()
{
string continuer ;
do
{
cout << "Enter Sample Mean: " << endl;
cin >> mean;
cout << "Enter Population Mean: " << endl;
cin >> popmean;
cout << "Enter Standard Deviation: " << endl;
cin >> stdev;
cout << "Enter Sample Size: " << endl;
cin >> number;
if (stdev == 0)
cout << ((mean-popmean)/(number))<< endl;
else
{
cout << ((mean-popmean)/((stdev)/(sqrt(number))))<< endl;
cout << ((1/(sqrt(2*pi)*stdev))*pow(e, (normalpow)))<< endl;
}
cout << "If you want to continue, Press Y" << endl ;
cin >> continuer;
} while (continuer == "Y" || continuer == "y") ;
return 0;
}
Your problem is here:
double normalpow = -0.5*((mean-popmean)*(mean-popmean)/stdev);
At this point, mean, popmean and stdev have garbage values because they haven't been initialized. It sounds like what you want is a function.
double normalPow(double mean, double popmean, double stddev)
{
return -0.5*((mean-popmean)*(mean-popmean)/stdev);
}
Then call it in your main:
double normalpow = normalPow(mean, popmean, stdev);
Of course, you should check for stdev equal or close to 0. in the function.
using namespace std;
double number ;
double mean ;
double popmean ;
double stdev ;
double square = 2;
double e = 2.71828182845904523536;
double pi = 3.14159265358979323846;
double normalpow = -0.5*((mean-popmean)*(mean-popmean)/stdev);
These are all variables with static storage duration, so those without explicit initialisers are initialised to 0.
Hence mean, popmean and stdev are all 0 when normalpow is initialised, and that initialisation results in
double normalpow = -0.5*(0.0*0.0/0.0);
which gives a NaN.
You never change normalpow afterwards, so any computation involving it results in a NaN.
This depends on how mean and popmean are calculated. mean - popmean evaluates to zero, if they are identical: mean == popmean.
And the formula you are using is not correct cause you are not calculating the square of the standard deviation.