I'm having trouble getting my functions to work right in this program. I am getting the following error when I try to compile:
/tmp/cc7oQapH.o: In function `main':polarcoord.cpp:(.text+0x7b): undefined reference to `degrees2radians(double)'
polarcoord.cpp:(.text+0x99): undefined reference to `compute_coord(double, double, double, double)'
collect2: ld returned 1 exit status
I can't figure out how to fix this despite reading a lot of different things online. Any help would be appreciated!
#include <iostream>
#include <cmath>
using namespace std;
// FUNCTION PROTOTYPE FOR degrees2radians
double degrees2radians( double) ; //variable for the function degrees2radians
// FUNCTION PROTOTYPE FOR compute_coord
void compute_coord( double, double, double, double) ; //variable for the
function compute_coord
// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
double angle_degrees(0.0), angle_radians(0.0), radius(0.0);
double coord_x(0.0), coord_y(0.0);
// Read in polar coordinates
cout << "Enter radius: ";
cin >> radius;
cout << "Enter polar angle (degrees): ";
cin >> angle_degrees;
// Convert degrees to radians
angle_radians = degrees2radians(angle_degrees);
// Compute Cartesian (x,y) coordinates
compute_coord(radius, angle_radians, coord_x, coord_y);
// Output Cartesian coordinates
cout << "Cartesian coordinates: ";
cout << "(" << coord_x << "," << coord_y << ")" << endl;
return 0;
}
// DEFINE FUNCTION degrees2radians here:
double degrees2radians( double angle_degrees, double & angle_radians)
{
angle_radians = angle_degrees * (3.14 / 180);
//return (angle_radians);
}
// DEFINE FUNCTION compute_coord here:
double compute_coord( double radius, double angle_radians, double & coord_x,
double & coord_y)
{
coord_x = radius * cos(angle_radians);
coord_y = radius * sin(angle_radians);
return (coord_x, coord_y);
}
#include <iostream>
#include <cmath>
using namespace std;
double degrees2radians( double) ;
void compute_coord( double, double, double&, double&) ;
int main()
{
double angle_degrees(0.0), angle_radians(0.0), radius(0.0);
double coord_x(0.0), coord_y(0.0);
cout << "Enter radius: ";
cin >> radius;
cout << "Enter polar angle (degrees): ";
cin >> angle_degrees;
angle_radians = degrees2radians(angle_degrees);
compute_coord(radius, angle_radians, coord_x, coord_y);
cout << "Cartesian coordinates: ";
cout << "(" << coord_x << "," << coord_y << ")" << endl;
return 0;
}
double degrees2radians( double angle_degrees)
{
return angle_degrees * (3.14 / 180);
}
void compute_coord( double radius, double angle_radians, double & coord_x, double & coord_y)
{
coord_x = radius * cos(angle_radians);
coord_y = radius * sin(angle_radians);
}
your function's definition not matched with declaration
maybe this is right code
Related
I have included the code I wrote below. I have created a function that calculates the volume of a cone based on the user's input. This is working as intended.
# include <iostream>
# include <string.h>
# include <string>
using namespace std;
// ConeVolume prototype
float ConeVolume(float radius, float height);
int main()
{
// Establish variables
float radius1;
float height2;
float volumeCone = ConeVolume(radius1, height2);
// User input to define the varibales
cout << "Radius: ";
cin >> radius1;
cout << "Height: ";
cin >> height2;
// Return variable using the ConeVolume function
cout << endl << "Cone Volume: " << volumeCone;
return 0;
}
// Function that calculates the volume of a Cone
float ConeVolume(float radius, float height)
{
float pi = 3.14;
float volume = (pi/3)*(radius * radius) * (height);
return volume;
}
My question... if I were to call the function by outputting the variable "float ConeVolume" as below, why does the program return '0'? Can I not set the value of a variable equal to a function?
// Return variable using the volumeCone float variable
cout << endl << "Cone Volume: " << volumeCone;
You've simply made a silly mistake. You've called the 'ConeVolume' function before taking user input. So, only garbage values are being passed to the function.
# include <iostream>
# include <string.h>
# include <string>
using namespace std;
// ConeVolume prototype
float ConeVolume(float radius, float height);
int main()
{
// Establish variables
float radius1;
float height2;
//wrong code here
// you've called the function before taking input of radius1 and height2
//float volumeCone = ConeVolume(radius1, height2);
// User input to define the varibales
cout << "Radius: ";
cin >> radius1;
cout << "Height: ";
cin >> height2;
// Correct code:
// Call the function after taking input
float volumeCone = ConeVolume(radius1, height2);
// Return variable using the ConeVolume function
cout << endl << "Cone Volume: " << volumeCone;
return 0;
}
// Function that calculates the volume of a Cone
float ConeVolume(float radius, float height)
{
float pi = 3.14;
float volume = (pi/3)*(radius * radius) * (height);
return volume;
}
Hope this helped.
The program return 0 because the value volumeCone is not being updated after you changed the values of radius1 and height2.
You have to call the function coneVolume() again, or better yet just call it after you define radius1 and height2.
int main()
{
// Establish variables
float radius1;
float height2;
// User input to define the varibales
cout << "Radius: ";
cin >> radius1;
cout << "Height: ";
cin >> height2;
float volumeCone = ConeVolume(radius1, height2);
// Return variable using the ConeVolume function
cout << endl << "Cone Volume: " << volumeCone;
return 0;
}
Insert the line
volumeCone = ConeVolume(radius1, height2);
after the line
cin >> height2;
And change line
float volumeCone = ConeVolume(radius1, height2);
to
float volumeCone;
I am a newbie in C++ and I am quite confused in programmer - defined functions.
It shows these errors
40 [Error] cannot convert 'double' to 'double(double, double)' in assignment
40 [Error] assignment of function 'double total_area(double, double)'
I cannot enter total_area = cross_area + side_area;
If I try to remove the double, it results to more errors
I can't find any information in youtube or in google that seems helpful.
// P33_2.cpp This program illustrates the local and global variables and call-by-value.
// This program computes the side area and the cross section area of a cylinder
#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14159; // This variable is defined globally, known to all functions in this program as PI
double cross_area(double r); // Function prototype for function cross_area
double side_area(double r, double h); // Function prototype for function Side_area
double total_area(double cross_area, double side_area);
int main(void)
{
double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder in Cm <Enter> ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I want to let you know that \n";
cout << "you have entered r = " << r << " and h = " << h << "." << endl;
cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is " << cross_area(r) << " inch-sqr endl\n";
cout << "The side area of the cylinder is " << side_area(r,h) << " inch-sqr \n\n";
cout << "The total surface area is "<< total_area << "inch-sqr \n \n";
return 0;
}
double cross_area(double r)
{
//Cross secion area includes the disks at the bottom and the top
r = r * 0.3937; // converting r to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to Side_area function
h = h * 0.3937; // converting h to inch
area = 2*PI*r*h;
return area;
}
double total_area(double cross_area, double side_area)`enter code here`
{
total_area = cross_area + side_area;
return 0;
}
See improved working code below
in your code, you have not passed any argument to total_area function. How do you think you will calculate area without passing arguments.
cout << "The total surface area is "<< total_area << "inch-sqr \n \n";
#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14159; // This variable is defined globally, known to all functions in this program as PI
double cross_area(double r); // Function prototype for function cross_area
double side_area(double r, double h); // Function prototype for function Side_area
double total_area(double cross_area, double side_area);
int main(void)
{
double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder in Cm <Enter> ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I want to let you know that \n";
cout << "you have entered r = " << r << " and h = " << h << "." << endl;
double dcross_area = cross_area(r);
double dside_area= side_area(r,h);
cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is " << cross_area(r) << " inch-sqr endl\n";
cout << "The side area of the cylinder is " << side_area(r,h) << " inch-sqr \n\n";
cout << "The total surface area is "<< total_area(dcross_area, dside_area) << "inch-sqr \n \n";
return 0;
}
double cross_area(double r)
{
//Cross secion area includes the disks at the bottom and the top
r = r * 0.3937; // converting r to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to Side_area function
h = h * 0.3937; // converting h to inch
area = 2*PI*r*h;
return area;
}
double total_area(double cross_area, double side_area)
{
return cross_area + side_area;
}
Here I have my code to calculate the distance between the user inputted points, and the calculateDistance function should take in two pointers, I feel like I had it setup right, but when I run the code I get this error: bash: line 12: 25372 Segmentation fault $file.o $args
Code:
struct Point{
float x;
float y;
};
float calculateDistance(struct Point *p1, struct Point *p2){
float *fx, *fy;
*fx = (*p1).x - (*p2).x;
*fy = (*p1).y - (*p2).y;
return sqrt((*fx * *fx) + (*fy * *fy));
}
int main()
{
struct Point *p1, *p2, q, w;
p1 = &q;
p2 = &w;
//float distance;
cout << "Enter coordinate for p1x: " << endl;
cin >> (*p1).x;
cout << "Enter coordinate for p1y: " << endl;
cin >> (*p1).y;
cout << "Enter coordinate for p2x: " << endl;
cin >> (*p2).x;
cout << "Enter coordinate for p2y: " << endl;
cin >> (*p2).y;
//distance = calculateDistance(*p1, *p2);
cout << "Distance between points: " << calculateDistance(p1, p2) << endl;
return 0;
}
One fault is in function calculateDistance. Please change it as
float calculateDistance(struct Point *p1, struct Point *p2){
float fx, fy;
fx = (*p1).x - (*p2).x;
fy = (*p1).y - (*p2).y;
return sqrt((fx * fx) + (fy * fy));
}
I have to create a sphere class with the following attributes:
Write a class Sphere with the following properties:
Private attributes: (1) X, Y, Z coordinates of the center (2)
Radius
Accessor and mutator methods to
• Set and get the X, Y, and Z
coordinates
• Set and get the radius
• Get the volume and surface
area if a sphere.
For a sphere,
Volume = 4πr3/3
Surface Area = 4πr2
Write a main
program to test the sphere class.
I have never worked with classes before. I think I did this correctly. However, my output for my Volume and my Surface Area comes out really strange. Below is my program and my output.
PROGRAM
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
using namespace std;
class Sphere {
private:
float X;
float Y;
float Z;
float R;
float Volume;
float SurfaceArea;
public:
float DefineCoordinates(float x, float y, float z);
void DefineRadius(float radius);
double GetVolume()
{
return (((4 * M_PI*pow(R, 3))) / 3);
}
double GetSurfaceArea()
{
return (4 * M_PI*pow(R, 2));
}
float GetX();
float GetY();
float GetZ();
};
float Sphere::GetX() {
return X;
}
float Sphere::GetY() {
return Y;
}
float Sphere::GetZ() {
return Z;
}
float Sphere::DefineCoordinates(float x, float y, float z) {
X = x;
Y = y;
Z = z;
return 0;
}
void Sphere::DefineRadius(float radius) {
R = radius;
}
int main() {
float inputr, radius, x, y, z;
Sphere sphere;
double Volume = sphere.GetVolume();
double SurfaceArea = sphere.GetSurfaceArea();
char open = '(';
char close = ')';
char comma = ',';
cout << "Please input the center of the sphere in the fashion (X,Y,Z) and press enter: ";
cin >> open >> x >> comma >> y >> comma >> z >> close;
cout << "Please define the radius of the sphere: ";
cin >> inputr;
sphere.DefineCoordinates(x, y, z);
sphere.DefineRadius(inputr);
cout << "This sphere has a center of (" << sphere.GetX() << ", " << sphere.GetY() << ", " << sphere.GetZ() << ")." << endl;
cout << "This sphere has a radius of " << inputr << "." << endl;
cout << "This computes to a volume of " << Volume << " units cubed, and a surface area of " << SurfaceArea << "." << endl;
}
OUTPUT
No matter what I input as radius, I will get:
This computes to a volume of -5.18547e+24 units cubed, and a surface
area of 1.4488e+17.
What am I doing wrong?? Also, any other advice to clean up my class would be helpful!
You call the GetVolume() method too early. Call it after actually taking the radius from the user.
int main() {
float inputr, radius, x, y, z;
Sphere sphere;
double SurfaceArea = sphere.GetSurfaceArea();
char open = '(';
char close = ')';
char comma = ',';
cout << "Please input the center of the sphere in the fashion (X,Y,Z) and press enter: ";
cin >> open >> x >> comma >> y >> comma >> z >> close;
cout << "Please define the radius of the sphere: ";
cin >> inputr;
sphere.DefineCoordinates(x, y, z);
sphere.DefineRadius(inputr);
double Volume = sphere.GetVolume();
cout << "This sphere has a center of (" << sphere.GetX() << ", " << sphere.GetY() << ", " << sphere.GetZ() << ")." << endl;
cout << "This sphere has a radius of " << inputr << "." << endl;
cout << "This computes to a volume of " << Volume << " units cubed, and a surface area of " << SurfaceArea << "." << endl;
}
Like the above.
Simple,
You read the volume and the surface before defining the radius.
Your problem is, that you are calling the GetVolume() and GetSurfaceArea() functions before you are reading the input. Your calculations are based on uninitialized values.
You should move the functions calls after the calls of DefineCoordinates()and DefineRadius()
Your main should look similar to this:
int main() {
float inputr, radius, x, y, z;
Sphere sphere;
char open = '(';
char close = ')';
char comma = ',';
cout << "Please input the center of the sphere in the fashion (X,Y,Z) and press enter: ";
cin >> open >> x >> comma >> y >> comma >> z >> close;
cout << "Please define the radius of the sphere: ";
cin >> inputr;
sphere.DefineCoordinates(x, y, z);
sphere.DefineRadius(inputr);
double Volume = sphere.GetVolume();
double SurfaceArea = sphere.GetSurfaceArea();
cout << "This sphere has a center of (" << sphere.GetX() << ", " << sphere.GetY() << ", " << sphere.GetZ() << ")." << endl;
cout << "This sphere has a radius of " << inputr << "." << endl;
cout << "This computes to a volume of " << Volume << " units cubed, and a surface area of " << SurfaceArea << "." << endl;
}
Sphere has a default constructor, which does not initialize the X, Y and Z members. Directly after construction, you try to calculate the volume and surface from these indefinite values.
In other words,
double Volume = sphere.GetVolume();
does not define a mathematical relation (it seems you think it does), it's an imperative. The volume will be calculated once (used for initialization of Volume) and its value will not change with you changing sphere's attributes. Do this calculation after you've called Sphere::DefineCoordinates and Sphere::DefineRadius.
Or, make it impossible to construct such an invalid Sphere object. Learn how to use constructors, and use some const qualifiers from time to time.
For completeness, what are Sphere::Volume and Spehre::SurfaceArea for? Why does Sphere::DefineCoordinates return a float (always 0) and why is my answer not at the top?
I need help with fixing the program. It is not running. I keep getting the warning control reaches end of non void function. I dont know how to fix it. Please help me. The program is suppose to find the volume or the surface area of a sphere. I get the warning on the last 2 }
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std;
char s = '\0';
const char SENTINEL = 's';
float radius, answer;
void get_radius (float&);
float surface_area (float);
float volume (float);
float cross_section (float);
const float PI = 3.14;
int main()
{
cout << "This program will let you input the radius of a sphere to find its volume or surface area." << endl << endl;
cout << "Enter 'v' for volume or 'a' for surface area of a sphere" << endl;
cout << "'s' to stop" << endl;
cin >> s;
while (s != SENTINEL)
{
get_radius (radius);
if(s == 'V')
{
volume (radius);
}
else if(s == 'A')
{
surface_area (radius);
}
cout << "Enter 'v' for volume or 'a' for surface area of a sphere" << endl;
cout << "'s' to stop" << endl;
cin >> s;
}
system("PAUSE");
return 0;
}
void get_radius (float& radius)
{
cout << "Please enter the radius of the sphere: " << endl;
cin >> radius;
}
float volume (float radius){
float answer;
answer = 4.0/3.0 * PI * pow (radius, 3);
cout << "The volume is: " << answer << endl;
}
float surface_area (float radius){
float answer;
answer = 4.0 * PI * pow(radius, 2);
cout << "The surface area is: " << answer << endl;
}
your function declaration must match what you are returning. You have to make sure you are returning values from functions that are declared that they are returning something.
volume() and surface_area() are printing things with cout but are not returning anything.
float volume (float radius){
float answer;
answer = 4.0/3.0 * PI * pow (radius, 3);
cout << "The volume is: " << answer << endl;
return answer;
}
float surface_area (float radius){
float answer;
answer = 4.0 * PI * pow(radius, 2);
cout << "The surface area is: " << answer << endl;
return answer;
}
When you declare the type of a function, you need to return a value of that type. For example, your function:
float volume (float radius) {}
Needs a return statement returns a value of type float.
If you don't need the function to actually return something, then declare it void to let the compiler know that. In this case:
void volume (float radius)
Just be careful, since void functions must NOT return a value (they can use a bare return statement, though).
Note also that potential paths which skips the return statement can trigger this error. For example, I could have this function:
int veryBadFunction(int flag)
{
if (flag == 1) {
return 1;
}
}
In this case, even though there IS a return statement in the function, it gets skipped any time the value of flag is something other than '1'. This is why the error message is worded control reaches...