Area of a circle Using points from a cartesian plane - c++

I have the following code which says that 'distance' is used uninitialized in this function.
This is a code that accepts two coordinates from a cartesian plane and uses the distance between them as radius to find the area of a circle. This is the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Point {
int x, y;
};
double getDistance(struct Point a, struct Point b)
{
double distance;
distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
return distance;
}
int main()
{
float Area;
double distance;
struct Point a, b;
printf("Enter coordinate of point a: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter coordinate of point b: ");
scanf("%d %d", &b.x, &b.y);
printf("Distance between a and b: %lf\n", getDistance(a, b));
Area= 3.14 * distance * distance;
printf("\nArea of Circle : %f", Area);
return 0;
}

This is correct: variable distance inside getDistance and variable distance inside main are two different variables.
When you write this
printf("Distance between a and b: %lf\n", getDistance(a, b));
distance inside main does not get set.
You can fix it by adding an assignment
distance = getDistance(a, b);
printf("Distance between a and b: %lf\n", distance);
Implementation note: Since you need distance squared, you can avoid taking square root by defining a function getDistanceSquared, and using it instead.

You should read more carefully the compiler warning, because it is refering to the variable distance in your main function not the one in getDistance.
I think, what you actually wanted to do was this:
distance = getDistance(a, b);
printf("Distance between a and b: %lf\n", distance);
Then, you can use the result of getDistance anywhere in your main function. ;)

You forgot to assign distance variable, try something like:
int main()
{
float Area;
double distance;
struct Point a, b;
printf("Enter coordinate of point a: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter coordinate of point b: ");
scanf("%d %d", &b.x, &b.y);
distance = getDistance(a, b);
printf("Distance between a and b: %lf\n", distance);
Area= 3.14 * distance * distance;
printf("\nArea of Circle : %f", Area);
return 0;
}

Related

Final Answer for volume of a spherical tank not valid

I created this program that is supposed to basically use the formula V=Pi*h^2(3r-h)/3 but my final answer isn't adding up.
For example: if I substitute 1 for the radius and 2 for height, I should get 4.18 but instead through the program I am getting -1.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double calculatevolume(double);
double main() {
double radius, height;
double sum; //for storing the radius of the reservoir given by the user
//for storing the calculated volume of the water.
printf("Please enter the Radius in meters first and then the Height in meters right after:\n"); //Displays the message on the screen
scanf("%lf",&radius);
printf("Value of r: %f\n", radius);
scanf("%lf",&height);
printf("Value of h: %f\n", height);
sum = calculatevolume(sum);
printf("For a reservoir of radius: %.2f m\nAnd a water depth: %.2f m\nThe water volume is: %.2f m^3\n",radius,height,sum);
system("PAUSE");
}
double calculatevolume(double sum) {
double radius;
double height;
sum = ((((3 * radius) - height)/3) * 3.14 * height * height);
return sum;
}
Try this code, you should pass height and radius to the function:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double calculatevolume(double,double,double);
double main() {
double radius, height;
double sum; //for storing the radius of the reservoir given by the user
//for storing the calculated volume of the water.
printf("Please enter the Radius in meters first and then the Height in meters right after:\n"); //Displays the message on the screen
scanf("%lf", &radius);
printf("Value of r: %f\n", radius);
scanf("%lf", &height);
printf("Value of h: %f\n", , height);
sum = calculatevolume(sum,radius,height);
printf("For a reservoir of radius: %.2f m\nAnd a water depth: %.2f m\nThe water volume is: %.2f m^3\n",radius,height,sum);
system("PAUSE");
}
double calculatevolume(double sum, double radius, double height) {
sum = ((((3 * radius) - height)/3) * 3.14 * height * height);
return sum;
}
You may also delete sum from the function header, it's not necessary to pass it

Classes arrays of objects value "nan"

I have a problem when I execute the program,the results that I have got is "nan" for the values of vector.I do not exactly where is the mistake. the method distancias generates a correct value but the method variogram does not generate the expected value instead of that generates a value "nan".Sorry for my english.
#include <iostream>
#include <math.h>
//this program is to calculate the kriging puntual
using namespace std;
////
class Points{
private:
float x;
float y;
public:
Points(float a,float b);
Points();
float distancia(float x_1,float y_1);
float variogram(float h);
float valor_1();
float valor_2();
void show(void);
};
Points::Points(){
}
Points::Points(float a,float b){
x=a;
y=b;
}
float Points::distancia(float x_1,float y_1){
float d;
d=pow(pow((x-x_1),2)+pow((y-y_1),2),0.5);
return d;
}
float Points::variogram(float h){
float v,c_0,c_1,a_1;
v=c_0+c_1*(1.5*(h/a_1)-0.5*pow((h/a_1),3));
return v;
}
void Points::show(void){
printf("%.2f,%.2f\n",x,y);
}
float Points::valor_1(){
return x;
}
float Points::valor_2(){
return y;
}
///////
int main(int argc, char** argv) {
float a_1,c_0,c_1; //parameters of variogram
float c,d; // position of point to determinate
float a,b; //positions of all points except the point to determinate
int i=0,n;
int j,k;
Points final; //point to determinate
//this part is to enter the values of function sphere variogram
printf("Enter the paramters of sphere variogram\n");
printf("Enter the value of c_0: ");
scanf("%f",&c_0);
printf("Enter the value of c_1: ");
scanf("%f",&c_1);
printf("Enter the value of a: ");
scanf("%f",&a_1);
//determinating the postion of final point
printf("Enter the position of the point to determinate: ");
scanf("%f,%f",&c,&d);
final=Points(c,d);
final.show();
printf("Enter the name of points for the krigeage: ");
scanf("%i",&n);
Points punto[n];
float vector[n];
do{
printf("Enter the position x,y of the point %i: ",i+1);
scanf("%f,%f",&a,&b);
punto[i]=Points(a,b);
punto[i].show();
vector[i]=punto[i].variogram(punto[i].distancia(c,d));
cout<<vector[i]<<endl;
i=i+1;
}while(i<n);
return 0;
}
The problem is in the following function:
float Points::variogram(float h){
float v,c_0,c_1,a_1;
v=c_0+c_1*(1.5*(h/a_1)-0.5*pow((h/a_1),3));
return v;
}
Here, you are declaring the local variables v, c_0, c_1 and a_1. These values are not initialized, they can contain anything. However, it is not unlikely they actually are equal to zero. So when you calculate h/a_1, the result of that is probably plus or minus infinity. When you subtract two infinities with the same sign from each other, the result will be NaN.
What you should do is pass the values for c_0, c_1 and a_1 from main() to the function:
float Points::variogram(float h, float c_0, float c_1, float a_1){
return c_0+c_1*(1.5*(h/a_1)-0.5*pow((h/a_1),3));
}
...
vector[i]=punto[i].variogram(punto[i].distancia(c,d), c_0, c_1, a_1);
Please compile your code with warnings enabled (if you use GCC, then use the -Wall command line option). Your compiler should then warn you about these uninitialized variables.

Declarations, Definitions and Calls

i need to gain a better understanding of function definition, declarations and proper calls using this program. I really need the understanding of how to use them. Could you show me the proper way to write this program with all three correct and explained?
#include <stdio.h>
#include <math.h>
quad_equation(float a, float b, float c);
int main()
{
float a, b, c, determinant, r1,r2;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0) { r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else (determinant<0);
{
printf("Both roots are complex");
}
return 0;
I just solved this exact question here : (I guess this is a part of an assignment )
https://stackoverflow.com/a/19826495/1253932
Also looking at your code .. you never use the function quad equation .. also you haven't defined the type of the function ( int/void/float/char) etc.
For ease: ( here is the entire code ) -- ask me if you don't understand anything
#include <stdio.h>
#include <math.h>
// function declarations
void twoRoots (float a,float b,float delta);
void oneRoot (float a,float b,float delta);
int main (void)
{
//Local Declarations
float a;
float b;
float c;
float delta;
// float solution;
printf("Input coefficient a.\n");
scanf("%f", &a);
printf("Input coefficient b.\n");
scanf("%f", &b);
printf("Input coefficient c.\n");
scanf("%f", &c);
printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c);
delta = (float)(b*b) - (float)(4.0 * a * c);
printf("delta = %0.2f\n",delta);
if (delta > 0){
twoRoots(a,b,delta);
}else if (delta == 0) {
oneRoot(a,b,delta);
}else if (delta < 0.0){
printf("There are no real roots\n");
}
return 0;
}
void twoRoots (float a,float b,float delta)
{
float xOne;
float xTwo;
float deltaRoot;
printf("There are two distinct roots.\n");
deltaRoot = sqrt(delta);
xOne = (-b + deltaRoot) / (2*a);
xTwo = (-b - deltaRoot) / (2*a);
printf("%.2f", xOne);
printf("%.2f", xTwo);
}
void oneRoot(float a,float b,float delta)
{
float xOne;
// float xTwo;
// float deltaRoot;
printf("There is exactly one distinct root\n");
xOne = -b / (2*a);
printf("%.2f", xOne);
}
EDIT:
A slightly more optimized and better functioning code that I made from the above mentioned code:
http://pastebin.com/GS65PvH6
Edit2:
From your comments you try to do this:
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
This will fail if you input something like this: 121
Beacuse scanf will read the whole 121 into a and it will have nothing for b,c ( rather it will put \n(enter) into b and undefined into c )
So use the scanf the way I have used it in my code
OK - this is full of problems! I attempt to point them out, and show what "better" looks like. I hope this helps.
quad_equation(float a, float b, float c);
This is probably intended to be a "function prototype". A prototype tells the compiler "I am going to use this function later, and this is how it needs to be called, and the type it returns". You did not specify a return type; probably you want to use int to say whether you found roots or not, and print out the result in the function. Better would be to pass space for two return values as a parameter:
int quad_equation(float a, float b, float c, float* x1, float* x2);
Now we can use the main program to get input/output, and let the function solve the problem:
int main(void) {
{
float a, b, c, r1, r2;
int n;
// here you get the inputs; that seems OK
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f",&a,&b,&c);
// now you have to "call your function"
// note that I make sure to follow the prototype: I assign the return value to an int
// and I pass five parameters: the coefficients a, b, c and the address of two variables
// x1 and x2. These addresses will be where the function puts the roots
n = quad_equation(a, b, c, &r1, &r2);
// when the function returns, I can print the results:
printf("There are %d roots:\n", n);
// based on the value of n, I change what I want to print out:
if (n == 2) printf(" %f and ", r1); // when there are two roots I print "root 1 and"
if (n > 0) printf("%f\n", r2); // when there is at least one root, I print it
// note that if n == 0, I would skip both print statements
// and all you would have gotten was "There are 0 roots" in the output
}
int quad_equation(float a, float b, float c, float* x1, float* x2) {
// function that computes roots of quadratic equation
// and returns result in x1 and x2
// it returns the number of roots as the return value of the function
float determinant;
determinant=b*b-4*a*c;
if (determinant>0)
{
*x1 = (-b+sqrt(determinant))/(2*a);
*x2= (-b-sqrt(determinant))/(2*a);
return 2;
}
if (determinant==0) {
*x1 = *x2 = -b/(2*a);
return 1;
}
return 0;
}

C++ ( function, cosine) not giving the correct answer

I just started taking this C++ course like a month ago.
Now I'm assigned to write a program to calculate this. I don't know what I did wrong.
#include <stdio.h>
#include <math.h>
float gatherl1();
float gatherl2();
float gatheran();
void values(float,float,float);
float findlength(float,float,float);
float findan2(float,float,float);
float findan3(float,float,float);
void name(float,float,float);
int main(void)
{
float length1,length2;
float length3;
float angle1,angle2,angle3;
length1 = gatherl1();
length2 = gatherl2();
angle1 = gatheran();
values(length1,length2,angle1);
length3 = findlength(length1,length2,angle1);
angle2 = findan2(length1,length2,length3);
angle3 = findan3(length1,length2,length3);
name(angle1,angle2,angle3);
}
float gatherl1()
{
float l1;
printf("Enter the length of one of the sides of any triangle\n");
scanf("%f",&l1);
return l1;
}
float gatherl2()
{
float l2;
printf("Enter the length of the other side\n");
scanf("%f",&l2);
return l2;
}
float gatheran()
{
float angle;
printf("Enter the angle between them.\n");
scanf("%f",&angle);
return angle;
}
void values(float l1, float l2, float angle)
{
printf("\n The two sides are %f and %f. The angle between them is %f \n",l1,l2,angle);
}
float findlength(float l1, float l2, float angle)
{
float l3,pyt,boy;
if (angle==90)
{
pyt = pow(l1,2) + pow(l2,2);
l3 = sqrt(pyt);
}
else
{
boy = pow(l1,2) + pow(l2,2) - 2*l1*l2*cos(angle);
l3 = sqrt(boy);
}
printf("\nthe third side is = %f",l3);
return l3;
}
float findan2(float l1, float l2, float l3)
{
float cosangle2,angle2;
cosangle2 = (pow(l2,2) + pow(l3,2) - pow(l1,2)) / (2*l2*l3);
angle2 = acos(cosangle2);
return angle2;
}
float findan3(float l1, float l2, float l3)
{
float cosangle3,angle3;
cosangle3 = (pow(l1,2) + pow(l3,2) - pow(l2,2)) / (2*l1*l3);
angle3 = acos(cosangle3);
return angle3;
}
void name(angle,angle2,angle3)
{
printf("\n\n\n the other two angles are %f and %f",angle2,angle3);
printf("\n\n\n The angle you put is %f",angle);
if(angle == 90)
{
printf("\n The triangle is a right triangle\n");
}
else if(angle < 90)
{
printf("\n The triangle is a acute triangle\n");
}
else
{
printf("\n The triangle is a obtuse triangle\n");
}
}
I have never use cos and arccos function before so I'm not sure if that's the cause.
or the function because I'm new to function too. Please Help Me!! thank you.
Are the values that you pass to your functions in terms of radians? Because cos and arccos assume radians as an input.
The C trigonometry functions operate in radians, not degrees. You have to multiply by pi/180 to convert degrees to radians.

C++ Overloading Of Functions? Area of Circle

I am trying to get area of circle using my program. But area is not coming in decimals.
#include<iostream>
using namespace std;
float AreaOfCircle(float r);
int AreaOfCircle(int r);
int main()
{int rad;
cout<<"Enter the Radius of Crircle: ";
cin>>rad;
cout<<"The Are of the Cirlcle: "<<AreaOfCircle(rad);
}
float AreaOfCircle(float r)
{
int area=0;
area=2*3.1456*r*r;
return area;
}
int AreaOfCircle(int r)
{
int area=0;
area=2*3.1456*r*r;
return area;
}
But I need answer to some decimal point.
You're not calling the float version of the method.
Either declare your variable as float
float rad;
or cast it to float before you call the method.
AreaOfCircle((float)rad);
You also need to use float instead of int inside the overloaded method:
float AreaOfCircle(float r)
{
float area=0; // <--- float here
area=2*3.1456*r*r;
return area;
}
Also:
area = pi * r * r
length = 2 * pi * r
pi ~= 3.1415
In addition to answer by #Luchian, you need to change the returned value to a float:
float AreaOfCircle(float r)
{
int area=0; // <<----- float area = 0;
area=2*3.1456*r*r;
return area;
}
change to:
float AreaOfCircle(float r)
{
float area=0;
area=2*3.1456*r*r;
return area;
}
or just:
float AreaOfCircle(float r) { return 2*3.1456*r*r; }
The compiler will call the overload it feels is the best match to the types of parameters it is passed. Because you passed an int, it assumed you wanted the int version.
By casting to a float as Luchian suggested (or using a float in the first place) you are telling the compiler that you intend the parameter to be a float type - thus, it picks the float version.