User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,556 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,441 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1382 | Replies: 3 | Solved
Reply
Join Date: Oct 2007
Posts: 7
Reputation: pyramid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pyramid pyramid is offline Offline
Newbie Poster

Pass by reference in Function call results not correct

  #1  
Oct 18th, 2007
Hello -
I am working on one of my labs, but I am having difficulty with the results being inconsistent.

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.


#include <iostream> 
#include <iomanip> 
#include <cmath> 

using namespace std; 

        bool isValid (int tri1, int tri2, int tri3); 
        bool side; 
          
         void calc(int a, int b, int c, double& s, double& area);
  
int main () 

{ 
        int side_a, side_b, side_c;
        double s, 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; 
    
        
    /*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 perimeter 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, s, area); 
  
     return s * 2;   // Line 57, warning converting to int from double
      return area;  // Line 58, warning converting to int from double
             
      cout << fixed << setprecision (4);
      cout << "Your triangle has a perimeter of  " << s << " and an area of " << area << 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 = 1; 
       else 
       side = 0; 
       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, double& s, double& area) 
    { 
       s = ((a + b + c)/2);         
       area =(s-a)*(s-b)*(s-c); 
       area = area * s;
       area = sqrt (area);
      
      return;
         
      
    } // end calc function 
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Location: Honduras
Posts: 1,407
Reputation: Nichito is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 26
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Pass by reference in Function call results not correct

  #2  
Oct 18th, 2007
one thing i don't understand is why in line 57 and 58 you have two returns if your main function is not over...

the errors you get are because main is declared as an int function, and the values you are returning are invalid conversions from double to int...

in your void calc() you should just use a, b, and c to calculate your perimeter and your area, because those operations there are messing your whole operations... maybe you should want to try this:

  1. void calc(int a, int b, int c, double &s, double &area){
  2. s=((a+b+c)/2);
  3. area=sqrt (((s-a)*(s-b)*(s-c))*s);
  4. }

try it and let me know...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

Re: Pass by reference in Function call results not correct

  #3  
Oct 19th, 2007
Nichito is right about lines 57 & 58 - they have no place in this program, they do nothing useful other than cause it to end before displaying your results. His revision to the calc function body does not change anything, just compresses three lines into one. Saves a couple assignment operations, that's all.

As to the erroneous area calculations, step through the function using the problem values (9,7,3). You should always solve by hand using the algorithm you implement to verify it. In this case, you'll find that your 's' value is 9. What's the value, then, of s-a? What happens when you use that result in the multiplications?

Your code is not giving the correct perimeter. It's giving 1/2 of the perimeter. You need to store the perimeter separately from the 's' value you use in calculations.

And lastly, the value you calculate for 's' is not correct, in most cases. Remember that dividing integers gives only an integer result, so 9/2 is the same as 8/2. You're storing to a double, so do the arithmetic as a double, divide by 2.0.
I am in mourning for my country.
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: pyramid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pyramid pyramid is offline Offline
Newbie Poster

Re: Pass by reference in Function call results not correct

  #4  
Oct 19th, 2007
My codes works great . No more errors. Thank you guys for all the suggestions.

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 agree with the revision to the calc body function. The original equation given to the class was: area = sqrt (s*(s-a)*(s-b)*(s-c)); I originally had this as one line, but changed it later. I agree that one line is better than three lines and much neater too.

I couldn’t agree more about solving the problems by hand first. I had actually done this for test data 5, 8, 12 (see below)

5 + 8+ 12 = 25.00 perimeter
25 /2.0 = 12.50
12.50 – 5 = 7.50
12.50 – 8 = 4.50
12.50 – 12 =.50
7.50 * 4.50 * .50 = 16.88
12.50 * 16.88 = 211
sqrt (211) = 14.525839 area

I changed the code to divide by 2.0 (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’.

I continuously learn from you guys – keep up the great job you’re doing. It’s awesome that people are so willing to help each other….

Thanks again.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC