Final Answer for volume of a spherical tank not valid - c++

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

Related

Circle's area using lenght of the side of the square ascribed in the circle / C++

I've written this C++ code which is supposed to compute circles' areas using the length of the side of the square inscribed in the circle. I get no errors but the result isn't right. Any advice?
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, const char * argv[]) {
float l, ac, r, area;
cout << "Square's side length: ";
cin >> l;
ac = l * sqrt(2);
r = ac/2;
area = M_PI * pow(2,r);
cout << "Circle's area: "<<area<< endl;
return 0;
}
In computing the area you have to write area = M_PI * pow(r,2);: you just inverted the arguments of the pow function.
The diameter of the circle would be equal to the hypotenuse of the diagonal of the square, so assuming l is side length of the square
float diam = std::sqrt(2.0f * std::pow(l, 2));
float radius = diam / 2.0f;
float area = M_PI * std::pow(radius, 2.0f);

Area of a circle Using points from a cartesian plane

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;
}

program is not printing at right points although the coordinates are correct

I want to build a program that stimulate the behavior of a projectile motion. I have checked the coordinates (x, y) which are correct but the compiler is not printing at right coordinates resulting in an inverted projectile motion(swing like motion)
the file "myconsole.h"is working correctly I have checked it separately.
here is my code:
#include <iostream>
#include <cmath>
#include <windows.h>
#include "myconsole.h"
using namespace std;
int coordinate(int theta, float v, float g, float initialheight, float x) //function to calculate y coordinate. it accepts x and produces y
{
float y = ((initialheight + (x * tan(theta))) - (( g * (x * x))/(2 * (powf((v * cos(theta)), 2)))));//formula
return y;
}
int main()
{
float initialheight = 0;
int y = 0;
int x = 0;
float v = 0;
int theta =0;
float g = 9.81;
cout<<"Enter the following Information"<<endl;
cout<<"Angle (theta) = ";
cin>>theta;
cout<<"Initial Velocity (V) = ";
cin>>v;
cout<<"Initial height = ";
cin>>initialheight;
float sq = 2*g*initialheight;//just for simplification
float sqroot = powf(v * (sin(theta)), 2);//just for simplification
float d = ((v * (cos(theta))/g)*((v * (sin(theta))+ sqrt(sqroot + sq))));
/*equation to calculate total distance covered
by the projectile. I started x from 0 and add 1 to it till it reaches d.for every value of x we get the
corisponding value of y from function coordinate.*/
ClearScreen();
while(x <= d)//loop to increment x
{
y = coordinate(theta, v, g, initialheight, x);
PlaceCursor(x, y); //using function from console.h and placing the cursor at (x, y) position
cout<<"*";
/*although the coordinates are stimulating the correct behavior of projectile but "* " are printing
an inverted projectile*/
x++;
}
system("pause");
return 0 ;
}
I believe the coordinate system for a console window starts at 0,0 in the top left and y increases down the window. Your code is correct, the graph itself is inverted because that's how the coords work.
I changed your code like this to get the desired result:
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo (hStdout, &csbiInfo);
int winheight = csbiInfo.dwSize.Y ;
system("cls");
while(x <= d)//loop to increment x
{
y = coordinate(theta, v, g, initialheight, x);
COORD pos;
pos.X = x;
pos.Y = winheight -y;
SetConsoleCursorPosition(hStdout, pos);
cout<<"*";
Without coding it up, this should fix it, change the value of gravity to a negative value in your equation as it's pulling in the opposite direction to your projectile.

Use of Undeclared Identifier "angle"?

I am making a program that converts rectangular coordinates into polar coordinates and whenever I go to run the program it tells me that the "angle" is undeclared even though I am sure I have declared it. As well I know that the program isn't returning anything, I just want to be able to run it for now.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
double random_float(double min, double max);
void rect_to_polar(double x, double y, double &distance, double &angle);
int main() {
double x, y;
x = random_float(-1, 1);
y = random_float(-1, 1);
rect_to_polar(x, y, distance, angle);
}
double random_float(double min, double max) {
unsigned int n = 2;
srand(n);
return ((double(rand()) / double(RAND_MAX)) * (max - min)) + min;
}
void rect_to_polar(double x, double y, double &distance, double &angle) {
const double toDegrees = 180.0/3.141593;
distance = sqrt(x*x + y*y);
angle = atan(y/x) * toDegrees;
}
You did not declare anything called angle in your main(), but still used the name angle there. Thus the error.
You might want to read up on scopes.
You should declare distance and angle in your main.
int main() {
double x, y, angle, distance;
x = random_float(-1, 1);
y = random_float(-1, 1);
rect_to_polar(x, y, distance, angle);
}

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.