473,811 Members | 3,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function call - results not consistent

18 New Member
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 2392
sicarie
4,677 Recognized Expert Moderator Specialist
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 New Member
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 New Member
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
7682
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this behavior? <!-- main page --> <html> <head> <script type="text/javascript">
4
5417
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 following code... --- function mysortfn(a,b) {
18
2105
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
13
4156
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 sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain amount of floating point work was approximately. To attempt to do this I made a program that...
89
6091
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 used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
4
2387
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. Because I want to use some ajax-requests and other javascript-functions on my xhtml, I need to dynamically add event handlers to any possible dom-elements. All solutions I found so fare are for specific, pre-known
11
2747
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 problems? Of course, I presume that inside f I don't access i in case it was called via g. int f(int i){ /* ... */ return 0; }
8
4581
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
5926
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
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10394
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10127
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.