Hello. My lab compiles with no problems, but the results are inconsistent. Using 2,2,2, as test data, the result is correct, perimeter of 6.000, and area of 1.732. However, when I try to use 8,5,12, the result is perimeter of 24 (should be 25), and area of 0.000 (should be 14.52
The lab specifies that we store the result of "s" and '"area" in reference parameters s and area.
Any input would be greatly appreciated.
Thank you guys! - #include <iostream>
-
#include <iomanip>
-
#include <cmath>
-
-
using namespace std;
-
-
//Define prototype for isValid Function
-
bool isValid (int tri1, int tri2, int tri3);
-
bool side;
-
-
//Define prototype for calc Function
-
void calc (int a, int b, int c, float &s, float &area);
-
-
int main ()
-
-
{
-
int side_a, side_b, side_c;
-
float s, area;
-
float refS = s; // declare ref variable here?
-
float refArea = area;
-
-
-
-
// Function prompting user for 3 numbers to be used for triangle sides
-
-
while (side != 1) //Establish loop here. If side not equal to 1, error encountered, try again.
-
{
-
cout << "Enter 3 lengths of the triangle sides: \n";
-
cout << " Side 1: "; cin >> side_a;
-
cout << " Side 2: "; cin >> side_b;
-
cout << " Side 3: "; cin >> side_c;
-
cout << endl;
-
cout << endl;
-
-
-
/*Call isValid Function to determine if the user inputs are valid.
-
isValid Function needs to loop until the correct values are entered */
-
-
bool side = isValid (side_a, side_b, side_c);
-
-
{
-
if (side = 0)
-
{
-
cout << "These edges do not form a triangle." << endl;
-
side = 0; // Populate side with 0. Error encountered. Give it another try.
-
}
-
else // All ok? populate valid with 1, end the loop.
-
{
-
side = 1; // If sides = triangle, end loop and continue.
-
}
-
}
-
} // end loop.
-
-
-
/*Call calc Function - this function will return the value of semiperimeter and Area.
-
(these values were stored in the reference parameters S and Area. Output should be perimeter
-
only (side a, b, c) and area. Set precision to 4 */
-
-
calc (side_a, side_b, side_c, refS, refArea);
-
-
cout << "Your triangle has a perimeter of " << fixed << setprecision (3)<< refS * 2 << " and an area of " << refArea << endl;
-
cout << endl;
-
-
-
system ("pause");
-
return 0;
-
-
}//end main
-
-
// isValid Function
-
-
bool isValid (int tri1, int tri2, int tri3)
-
{
-
if ((tri1 + tri2 > tri3) && (tri1 + tri3 > tri2) && (tri2 + tri3 > tri1))
-
side = true;
-
else
-
side = false;
-
-
return side;
-
-
} // end isValid
-
-
-
/* Write a Function 'calc'. Return type = void, 5 parameters (a, b, c, &s, &area.
-
Calculate s = semiperimeter and area. Results of s and area are to be stored in the
-
reference parameters s and area */
-
-
-
void calc(int a, int b, int c, float &s, float &area)
-
{
-
s = ((a + b + c)/2);
-
area = sqrt(s*(s-a)*(s-b)*(s-c));
-
-
-
} // end calc function
3 2345 -
//Define prototype for calc Function
-
void calc (int a, int b, int c, float &s, float &area);
-
-
int main ()
-
-
{
-
int side_a, side_b, side_c;
-
float s, area;
-
float refS = s; // declare ref variable here?
-
float refArea = area;
-
/*
-
...
-
*/ calc (side_a, side_b, side_c, refS, refArea);
-
-
cout << "Your triangle has a perimeter of " << fixed << setprecision (3)<< refS * 2 << " and an area of " << refArea << endl;
-
cout << endl;
-
Where are refS and refArea set?
Try doing a cout << refS << '\t' << refArea; right before you call calc, then do one right after you call calc.
The passing by reference is working. I tested random data and I don't understand why some works and others produces incorrect results.
Data tested:
5, 9, 7 = perimeter of 21 and area of 12.247 (correct)
5, 6, 9 = perimeter of 20 and area of 14.142 (correct)
9, 7, 3 = perimeter of 19 (this part is correct) and area of 0.000 (incorrect)
5, 8, 12 = perimeter of 25 (this part is correct) and area of 0.000 (incorrect)
Line 57 & 58: Compiler gives a warning of " converting to int from double. I don't understand this, since I'm sure I used the correct data types.
The output should be precision of 4 significant digits, but only the area is converting correctly, and not the perimeter.
I appreciate any input you guys may have. I checked my codes several times and I just can't seem to figure out the cause of the problems.
Thank you
My codes works great . No more errors.
I removed line 57 & 58. I thought that I had to do this to pass the value, but what I needed to do was just call my calc function. I changed the code to divide by 2.0 instead of interger 2 (I always forget to do double). Lastly, I created a new variable ‘perimeter’ to store the perimeter value. And result of ‘s’ is stored in ‘s’.
Thanks for the input.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: laredotornado |
last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to
have my Javascript function execute from the BODY's "onload" method,
but if there is already an onload method defined, I would...
|
by: Steve |
last post by:
Hi
I have decided it is time to learn Javascript (I know PHP fairly well, so
I thought it shouldn't be too difficult!!!)
I have a couple of question though, which I can highlight with the...
|
by: Razvan |
last post by:
Hi!
What is the purpose of such a function ?
int function(void)
|
by: Bern McCarty |
last post by:
I have run an experiment to try to learn some things about floating point
performance in managed C++. I am using Visual Studio
2003. I was hoping to get a feel for whether or not it would make...
|
by: Cuthbert |
last post by:
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the
'gets' function is dangerous and should not be...
|
by: simon |
last post by:
hi,
I would like to separate my javascript completely from my xhtml. in the
end there should be only
<script type="text/javascript" src="javalib.js"></script>
in the head-tag to my javascript....
|
by: Felix Kater |
last post by:
Hi,
I can compile and run this code (see below) which twice calls the
function f, first with too less, second with too much arguments.
But is it legal and free of memory leaks and other...
|
by: minseokoh |
last post by:
Hi,
Could someone explain why "const" is located after the function name
and what it means?
inline unsigned char *access(int off) const {
if (off < 0)
abort();
return (&bits_);
}
|
by: DiAvOl |
last post by:
Hello everyone,
Please take a look at the following code:
#include <stdio.h>
typedef struct person {
char name;
int age;
} Person;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |