473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function call - results not consistent

18
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!


Expand|Select|Wrap|Line Numbers
  1.  #include <iostream> 
  2. #include <iomanip> 
  3. #include <cmath> 
  4.  
  5. using namespace std; 
  6.  
  7.       //Define prototype for isValid Function 
  8.          bool isValid (int tri1, int tri2, int tri3); 
  9.          bool side; 
  10.  
  11.      //Define prototype for calc Function 
  12.          void calc (int a, int b, int c, float &s, float &area); 
  13.  
  14. int main () 
  15.  
  16.         int side_a, side_b, side_c;
  17.         float s, area; 
  18.         float refS = s; // declare ref variable here? 
  19.         float refArea = area;
  20.  
  21.  
  22.  
  23.     // Function prompting user for 3 numbers to be used for triangle sides 
  24.  
  25.     while (side != 1) //Establish loop here. If side not equal to 1, error encountered, try again.
  26.     {
  27.        cout << "Enter 3 lengths of the triangle sides: \n"; 
  28.        cout << " Side 1:  ";     cin >> side_a; 
  29.        cout << " Side 2:  ";     cin >> side_b; 
  30.        cout << " Side 3:  ";     cin >> side_c; 
  31.        cout << endl; 
  32.        cout << endl; 
  33.  
  34.  
  35.     /*Call isValid Function to determine if the user inputs are valid. 
  36.       isValid Function needs to loop until the correct values are entered */ 
  37.  
  38.        bool side = isValid (side_a, side_b, side_c); 
  39.  
  40.       { 
  41.         if (side = 0) 
  42.         {
  43.          cout << "These edges do not form a triangle."  << endl; 
  44.          side = 0; // Populate side with 0. Error encountered. Give it another try.
  45.         }    
  46.         else // All ok? populate valid with 1, end the loop. 
  47.         {
  48.           side = 1; // If sides = triangle, end loop and continue. 
  49.         }           
  50.        } 
  51.    }  // end  loop.         
  52.  
  53.  
  54.  /*Call calc Function - this function will return the value of semiperimeter and Area. 
  55.  (these values were stored in the reference parameters S and Area.  Output should be perimeter 
  56.  only (side a, b, c) and area.   Set precision to 4 */ 
  57.  
  58.         calc (side_a, side_b, side_c, refS, refArea); 
  59.  
  60.         cout << "Your triangle has a perimeter of  " << fixed << setprecision (3)<< refS * 2 << " and an area of " << refArea << endl; 
  61.         cout << endl; 
  62.  
  63.  
  64.     system ("pause"); 
  65.     return 0; 
  66.  
  67. }//end main 
  68.  
  69.     // isValid Function 
  70.  
  71.        bool isValid (int tri1, int tri2, int tri3) 
  72.     {   
  73.        if ((tri1 + tri2 > tri3) && (tri1 + tri3 > tri2) && (tri2 + tri3 > tri1)) 
  74.        side = true; 
  75.        else 
  76.        side = false; 
  77.  
  78.        return side; 
  79.  
  80.     } // end isValid 
  81.  
  82.  
  83.      /* Write a Function 'calc'.   Return type = void, 5 parameters (a, b, c, &s, &area. 
  84.     Calculate s = semiperimeter and area.   Results of s and area are to be stored in the 
  85.     reference parameters s and area */ 
  86.  
  87.  
  88.       void calc(int a, int b, int c, float &s, float &area) 
  89.     { 
  90.       s = ((a + b + c)/2); 
  91.       area = sqrt(s*(s-a)*(s-b)*(s-c)); 
  92.  
  93.  
  94.     } // end calc function 
Oct 18 '07 #1
3 2345
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1.      //Define prototype for calc Function 
  2.          void calc (int a, int b, int c, float &s, float &area); 
  3.  
  4. int main () 
  5.  
  6.         int side_a, side_b, side_c;
  7.         float s, area; 
  8.         float refS = s; // declare ref variable here? 
  9.         float refArea = area;
  10. /* 
  11. ...
  12. */      calc (side_a, side_b, side_c, refS, refArea); 
  13.  
  14.         cout << "Your triangle has a perimeter of  " << fixed << setprecision (3)<< refS * 2 << " and an area of " << refArea << endl; 
  15.         cout << endl; 
  16.  
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.
Oct 18 '07 #2
pyramid
18
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
Oct 19 '07 #3
pyramid
18
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.
Oct 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
4
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...
18
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
13
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...
89
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...
4
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....
11
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...
8
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_); }
160
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;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.