I'm working on a program for class and was just wondering if anyone might be able to help me figure out why my perimeter values and two of the area values print incorrectly? All perimeters should be unique but instead end up being 12, and the two area calculations not belonging to the equilateral triangle come out as gibberish. I'm using the formulas my instructor told us to use, and we are also to use the functions as shown. My input file values are:
14 18 24
26 26 26
24 67 90
80 0 4 (0 being the sentinel value)
I'm just learning how to use functions and there must be something wrong with my logic. My source code is included below...if anyone can provide any insight it will be very greatly appreciated! :)
p = perimeterTriangle (side1, side2, side3);
area = sqrt ( p * ( p - side1 ) * ( p - side2 ) * ( p - side3 ) );
return area;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-31
>> Always take Cliffords advice, he is a damn smart cookie ;>p
Actually my advice would be to use a debugger. Unfortunately Dev-C++'s sucks. Insight is passable.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In your functions, when you return a value like that you have to make it static or it won't be around after the function finishes. Try static double p; instead of double p; and so forth.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
you declare your variables as doubles yet your values are integers
14 18 24
26 26 26
24 67 90
80 0 4 (0 being the sentinel value)
See Table Below and you should be able to fix your problem with a little thought.
(Hope your not using Vista/64bit)
Type Size Values
unsigned short int 2 bytes 0 to 65,535
short int 2 bytes -32,768 to 32,767
unsigned long int 4 bytes 0 to 4,294,967,295
long int 4 bytes -2,147,483,648 to 2,147,483,647
int (16 bit) 2 bytes -32,768 to 32,767
int (32 bit) 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int (16 bit) 2 bytes 0 to 65,535
unsigned int (32 bit) 2 bytes 0 to 4,294,967,295
char 1 byte 256 character values
float 4 bytes 1.2e-38 to 3.4e38
double 8 bytes 2.2e-308 to 1.8e308
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you both - I changed the proper values to ints (our instructor wants all numeric data formatted to one decimal place, she's VERY strict re: her instructions so I guess there's something to that) and made my return values static. I also rearranged my declared variables to get in the habit. :) Ran the program again and my output reads:
The sides of the triangle are 14, 18, and 24.
The area of the triangle is -1.$.
The perimeter of the triangle is 12.0.
The triangle is an equilateral triangle.
The sides of the triangle are 26, 26, and 26.
The area of the triangle is 84.9.
The perimeter of the triangle is 12.0.
The sides of the triangle are 24, 67, and 90.
The area of the triangle is -1.$.
The perimeter of the triangle is 12.0.
I apologize if there's something really simple that I'm overlooking...I think I've been staring at this too long! If my while loop reads the new set of ints each time it runs, why would the totals for the perimeter be the same on all three and the area the same on two of them? Is there something wrong with the way I've ordered things?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Take a look at your while loop and your variables and values.
When do you call your functions, do your variables change with each call of said function, do the values you pass to your functions change.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
A good way to see when you are calling functions since this is a simple console app is to place a line at the beginning of each function telling you when it has been called. Example below
// send text to screen saying you called the function
cout << "perimeterTriangle Function" << endl;
double p;
p = (side1, side2, side3) / 2.0;
return p;
}
////////////////////////////////////////////////////
Even better is to add to that the values you passed
////////////////////////////////////////////////////
// send text to screen saying you called the function with values passed to function
cout << "perimeterTriangle Function with values ";
cout << "side 1: " << side 1 << endl;
cout << "side 2: " << side 2 << endl;
cout << "side 3: " << side 3 << endl;
double p;
p = (side1, side2, side3) / 2.0;
return p;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-31
Interestingly while giving advice about debugging, you reproduced the error that Osito had pointed out 12 hours before!
I suggest a modification to your suggestion:
// send text to screen saying you called the function
cout << FUNCTION << endl;
The FUNCTION string is always equal to the function name in which it occurs.
Using sone funky pre-processor magic, you can do something even more elaborate:
showing the file name, line number and the function name as well as any values added in the DEBUG_printf call. And because of the conditional compilation, the debug code disappears on release builds.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-31
Forgive me for pointing out the obvious, but the perimeter of a triangle is the sum of the sides, not the sum of the sides divided by 2. You have calculated the "semi-perimeter", which is needed by the area calculation. You should remove the / 2.0 and have:
s = perimeterTriangle (side1, side2, side3) / 2.0 ;
area = sqrt ( s * ( s - side1 ) * ( s - side2 ) * ( s - side3 ) );
I only did a copy and paste from the original post, probably could have picked any of the functions...I just picked the one that was wrong ;)
Was just trying to get them to think about the functions and how they called them and how they could visually see when it was called what was sent ect....
Note***
Always take Cliffords advice, he is a damn smart cookie ;>p
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First off I can't thank you all enough. I don't know how I missed the problem with the basic perimeter calculation itself(commas instead of + signs) and the unnecessary division by 2.0 - glad you pointed that out since I've apparently forgotten everything I learned in high school math. But that's neither here nor there - by following your advice I FINALLY realized that I needed to be calling my functions from within the while loop instead of where I had them originally. I think having them outside the while loop meant that the calculations were only being performed on the first set of ints read from the input file and the 2nd and 3rd sets read went unnoticed by said functions. Argh and I spent all this time worrying about something I should have seen right away! With my future courses I hope I can become as knowledgable as you guys one of these days - I am truly grateful for your attention to my problem and your suggestions - this is a great forum! Thanks again!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm working on a program for class and was just wondering if anyone might be able to help me figure out why my perimeter values and two of the area values print incorrectly? All perimeters should be unique but instead end up being 12, and the two area calculations not belonging to the equilateral triangle come out as gibberish. I'm using the formulas my instructor told us to use, and we are also to use the functions as shown. My input file values are:
14 18 24
26 26 26
24 67 90
80 0 4 (0 being the sentinel value)
I'm just learning how to use functions and there must be something wrong with my logic. My source code is included below...if anyone can provide any insight it will be very greatly appreciated! :)
include <iostream>
include <fstream>
include <iomanip>
include <cmath>
using namespace std;
double areaEquilateral(double, double, double);
double perimeterTriangle(double, double, double);
double areaTriangle(double, double, double);
int main()
{
double side1,
side2,
side3,
eq_area,
area,
p;
}
double perimeterTriangle(double side1, double side2, double side3)
{
double p;
}
double areaEquilateral(double side1, double side2, double side3)
{
double eq_area;
}
double areaTriangle(double side1, double side2, double side3)
{
double area,
p;
}
>> Always take Cliffords advice, he is a damn smart cookie ;>p
Actually my advice would be to use a debugger. Unfortunately Dev-C++'s sucks. Insight is passable.
In your functions, when you return a value like that you have to make it static or it won't be around after the function finishes. Try static double p; instead of double p; and so forth.
well First it is good habit to declare your variables on a seperate line for each one
double side1;
double side2;
double side3;
double eq_area;
double area;
double p;
you declare your variables as doubles yet your values are integers
14 18 24
26 26 26
24 67 90
80 0 4 (0 being the sentinel value)
See Table Below and you should be able to fix your problem with a little thought.
(Hope your not using Vista/64bit)
Type Size Values
unsigned short int 2 bytes 0 to 65,535
short int 2 bytes -32,768 to 32,767
unsigned long int 4 bytes 0 to 4,294,967,295
long int 4 bytes -2,147,483,648 to 2,147,483,647
int (16 bit) 2 bytes -32,768 to 32,767
int (32 bit) 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int (16 bit) 2 bytes 0 to 65,535
unsigned int (32 bit) 2 bytes 0 to 4,294,967,295
char 1 byte 256 character values
float 4 bytes 1.2e-38 to 3.4e38
double 8 bytes 2.2e-308 to 1.8e308
Thank you both - I changed the proper values to ints (our instructor wants all numeric data formatted to one decimal place, she's VERY strict re: her instructions so I guess there's something to that) and made my return values static. I also rearranged my declared variables to get in the habit. :) Ran the program again and my output reads:
The sides of the triangle are 14, 18, and 24.
The area of the triangle is -1.$.
The perimeter of the triangle is 12.0.
The triangle is an equilateral triangle.
The sides of the triangle are 26, 26, and 26.
The area of the triangle is 84.9.
The perimeter of the triangle is 12.0.
The sides of the triangle are 24, 67, and 90.
The area of the triangle is -1.$.
The perimeter of the triangle is 12.0.
I apologize if there's something really simple that I'm overlooking...I think I've been staring at this too long! If my while loop reads the new set of ints each time it runs, why would the totals for the perimeter be the same on all three and the area the same on two of them? Is there something wrong with the way I've ordered things?
Your perimeter calculation doesn't make sense.
p = (side1, side2, side3) / 2.0;
I'm not sure how that's interpreted in C++, but the perimeter of a triangle should be p = side1 + side2 + side3;
That probably explains the answer always being 12.
Take a look at your while loop and your variables and values.
When do you call your functions, do your variables change with each call of said function, do the values you pass to your functions change.
Also
A good way to see when you are calling functions since this is a simple console app is to place a line at the beginning of each function telling you when it has been called. Example below
double perimeterTriangle(double side1, double side2, double side3)
{
// send text to screen saying you called the function
cout << "perimeterTriangle Function" << endl;
double p;
p = (side1, side2, side3) / 2.0;
return p;
}
////////////////////////////////////////////////////
Even better is to add to that the values you passed
////////////////////////////////////////////////////
double perimeterTriangle(double side1, double side2, double side3)
{
// send text to screen saying you called the function with values passed to function
cout << "perimeterTriangle Function with values ";
cout << "side 1: " << side 1 << endl;
cout << "side 2: " << side 2 << endl;
cout << "side 3: " << side 3 << endl;
double p;
p = (side1, side2, side3) / 2.0;
return p;
}
Interestingly while giving advice about debugging, you reproduced the error that Osito had pointed out 12 hours before!
I suggest a modification to your suggestion:
// send text to screen saying you called the function
cout << FUNCTION << endl;
The FUNCTION string is always equal to the function name in which it occurs.
Using sone funky pre-processor magic, you can do something even more elaborate:
if !defined NDEBUG
include <stdio.h>
define DEBUG_printf( format, ... ) printf( FILE, LINE, FUNCTION, "%s[%d]:%s: "format, VA_ARGS )
else
define DEBUG_printf( format, ... )
endif
double perimeterTriangle(double side1, double side2, double side3)
{
double p;
p = (side1 + side2 + side3) / 2.0;
DEBUG_printf( "side1:%f side2:%f side3:%f p:%f", side1, side2, side3, p ) ;
return p;
}
Which when called thus:
perimeterTriangle( 12.0, 13.0, 15.0 ) ;
will output:
main.cpp[13]:perimeterTriangle: side1:12.000000 side2:13.000000 side3:15.000000 p:20.000000
showing the file name, line number and the function name as well as any values added in the DEBUG_printf call. And because of the conditional compilation, the debug code disappears on release builds.
Clifford
Forgive me for pointing out the obvious, but the perimeter of a triangle is the sum of the sides, not the sum of the sides divided by 2. You have calculated the "semi-perimeter", which is needed by the area calculation. You should remove the / 2.0 and have:
s = perimeterTriangle (side1, side2, side3) / 2.0 ;
area = sqrt ( s * ( s - side1 ) * ( s - side2 ) * ( s - side3 ) );
in the area calculation.
See: http://www.btinternet.com/~se16/hgb/triangle.htm
I only did a copy and paste from the original post, probably could have picked any of the functions...I just picked the one that was wrong ;)
Was just trying to get them to think about the functions and how they called them and how they could visually see when it was called what was sent ect....
Note***
Always take Cliffords advice, he is a damn smart cookie ;>p
First off I can't thank you all enough. I don't know how I missed the problem with the basic perimeter calculation itself(commas instead of + signs) and the unnecessary division by 2.0 - glad you pointed that out since I've apparently forgotten everything I learned in high school math. But that's neither here nor there - by following your advice I FINALLY realized that I needed to be calling my functions from within the while loop instead of where I had them originally. I think having them outside the while loop meant that the calculations were only being performed on the first set of ints read from the input file and the 2nd and 3rd sets read went unnoticed by said functions. Argh and I spent all this time worrying about something I should have seen right away! With my future courses I hope I can become as knowledgable as you guys one of these days - I am truly grateful for your attention to my problem and your suggestions - this is a great forum! Thanks again!