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;
}
Related
I'm new to programming so please write your answer as basic as possible. I made a simple calulator in C++. It's supposed to add 2 numbers but for some reason the output is always 16, no matter the numbers. Can someone explain this to me? This is the code:
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int sum = a + b;
cout << "Enter a number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << sum;
return 0;
}
But, when i do this (creating the int sum first and then assigning it later), it works:
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int sum;
cout << "Enter a number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
sum = a + b;
cout << sum;
return 0;
}
int sum = a + b;
Is not an algebraic rule, it is a statement evaluated at that point in the sequence of statements.
Just do it after your inputs.
In the first example, you're initializing the sum variable before the initialization of a and b so before initialization, the a and b will contain some garbage value and that's why you're getting output 16 the garbage value can be anything. Just initialize your sum variable after a and b variables have some user inputted values.
And if you're doing some addition then it's a good practice to initialize your result variable(sum) with zero sum=0 so it also doesn't contain any garbage values
When you use
int sum = a + b;
sum initilized to whatever a + b evaluates to. The value of sum does not change when you set the values of a and b after that statement. In your case, neither a nor b has been initialized before that statement. Hence, it causes undefined behavior.
The second version of your program works correctly since you are assigning a + b to sum after a and b have been assigned values from user input.
Heres the given:
Write a program to compute for the surface area and volume of a sphere if the unit of the radius
is in centimeters (cm).
Filename: exer10.cpp
Formulas: area = 4*pi*radius2
Volume = (4/3)*pi*radius3
I was skeptical because of the "if" on the given as you read it, now I don't know if what I did is right. But I have some ideas on how to do it
I Have a 2 ideas in mind 1st is where if I input a value there will be a formula to distinguish if that value is in centimeter, the thing is I don't know how.
2nd idea is I will use the if else method where after I input a value, it will ask if its in centimeter or not, if I type "Y" it will do its thing and continue its computation but if I type "N" it`ll will not compute and end the program.
Any suggestion guys?
By the way here's my code (My given ideas are not written here)
#include <iostream>
using namespace std;
int main()
{
float radius, area, volume;
cout << "This program computes for the surface area and volume of a sphere (centimeters)"
"\n\n";
cout << "Input the radius of a sphere : ";
cin >> radius;
area = (4 * 3.1416 * radius * radius);
volume = (4 / 3) * (3.1416 * radius * radius * radius);
cout << "The surface area of a sphere is : " << area << "\n";
cout << "The volume of a sphere is : " << volume;
return 0;
}
You can ask the user to input the unit. Consider this example, if it's in centimeters then perform the operation/task or else exit the program.
#include <iostream>
#include <string>
#include <cstdio>
int main() {
int radius;
std::string unit;
std::cout << "Enter radius of sphere in centimeters (e.g. 5 cm) : ";
std::cin >> radius >> unit;
if (unit != "cm") {
std::cout << "\nPlease enter in centimeters (e.g. 5 cm)";
exit(1);
}
// perform operation
return 0;
}
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;
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;
}
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.