7 Replies - 290 Views - Last Post: 22 November 2012 - 01:54 AM Rate Topic: -----

#1 jain87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-November 12

problem using a function for a task

Posted 21 November 2012 - 09:15 AM

hello, i have been given a task to write a code using a function but i have no idea how to connect it to the main function. i would love an explanation
#include <iostream>
using namespace std;


void Biggest (double & num1 , double & num2, double & num3);

void main ()

{
	double dig1, dig2, dig3;
	double biggest;
	

	cout<<"Please enter 3 numbers"<<endl;
	cin>>dig1>>dig2>>dig3;

	cout<<"The biggest number is: "<<biggest<<endl; // I know this is not good

}

void Biggest (double & num1 , double & num2, double & num3)

{
	double b_num;
	
	if ((num1>num2) && (num1>num3))
		b_num=num1;
	else if ((num2>num1) && (num2>num3))
		b_num=num2;
	else
		b_num=num3;

}


thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: problem using a function for a task

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: problem using a function for a task

Posted 21 November 2012 - 09:20 AM

I suggest you study the links provided in my signature, they should help explain functions.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: problem using a function for a task

Posted 21 November 2012 - 10:09 AM

You may want to make the Biggest() function return a double variable that will be the "biggest" of the 3 arguments that are passed in to the function. You don't need to do this, I'm just making a suggestion.
Was This Post Helpful? 1
  • +
  • -

#4 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: problem using a function for a task

Posted 21 November 2012 - 10:23 AM

First, you need to adjust the type of your function.

double Biggest (double & num1 , double & num2, double & num3);
.
.
.
double Biggest (double & num1 , double & num2, double & num3)  
{  
   double b_num;  
   if ((num1>num2) && (num1>num3))  
       b_num=num1;  
   else if ((num2>num1) && (num2>num3))  
       b_num=num2;  
   else 
       b_num=num3;
   return b_num;  
} 



I changed the function from void to double. This means whenever the function is used, the result will be treated as a double. Every function needs to return a value that has the same type as the function, so I added "return b_num;" at the end of it. Voids don't have to have a return, everything else needs a return. With these modifications, You can now use this function anywhere you would use a double. Here are some examples of your function in action.


int main()
{
     double e1 = biggest(1.0, 2.0, 3.0);
     double e2 = biggest(2.5, 3.0, 3.5);
     double e3 = biggest(3.4, 3.3, 3.2);

     cout<<e1<<endl;
     cout<<e2<<endl;
     cout<<e3<<endl;

     cout<<biggest(e1, e3, e2)<<endl;

     return 0;  //because this main has a non-void type
}



The output of this code would be

3.0
3.5
3.4
3.5
Was This Post Helpful? -1
  • +
  • -

#5 jain87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-November 12

Re: problem using a function for a task

Posted 22 November 2012 - 01:30 AM

View Postjimblumberg, on 21 November 2012 - 09:20 AM, said:

I suggest you study the links provided in my signature, they should help explain functions.


Jim


thank you Jim, that was very helpfull, I have another question for you, in the same code I have to use one more function that will work with the second one, that will give me the smallest number, and again I'm having trouble to use both of them in the same code, how can I connect 3 of them?
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: problem using a function for a task

Posted 22 November 2012 - 01:43 AM

View Postjain87, on 22 November 2012 - 01:30 AM, said:

View Postjimblumberg, on 21 November 2012 - 09:20 AM, said:

I suggest you study the links provided in my signature, they should help explain functions.


Jim


thank you Jim, that was very helpfull, I have another question for you, in the same code I have to use one more function that will work with the second one, that will give me the smallest number, and again I'm having trouble to use both of them in the same code, how can I connect 3 of them?


The Smallest function can be exactly the same as the Biggest function, except for turning the comparison around, so the smallest number is returned and printed. Same parameters, same return type, same comparisons - just change the logic to switch from biggest number to the smallest number, and you're done.
Was This Post Helpful? 0
  • +
  • -

#7 jain87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-November 12

Re: problem using a function for a task

Posted 22 November 2012 - 01:49 AM

View PostAdak, on 22 November 2012 - 01:43 AM, said:

View Postjain87, on 22 November 2012 - 01:30 AM, said:

View Postjimblumberg, on 21 November 2012 - 09:20 AM, said:

I suggest you study the links provided in my signature, they should help explain functions.


Jim


thank you Jim, that was very helpfull, I have another question for you, in the same code I have to use one more function that will work with the second one, that will give me the smallest number, and again I'm having trouble to use both of them in the same code, how can I connect 3 of them?


The Smallest function can be exactly the same as the Biggest function, except for turning the comparison around, so the smallest number is returned and printed. Same parameters, same return type, same comparisons - just change the logic to switch from biggest number to the smallest number, and you're done.



I wish! My task is to use the biggest function, and witout making any more comparison find out the smallest number, by using another function.
Was This Post Helpful? 0
  • +
  • -

#8 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: problem using a function for a task

Posted 22 November 2012 - 01:54 AM

/* A possibility */

double biggest(double &a, double &b, double &c)
{
   if(a > b )
     swap(a ,b )
   if(a > c) 
     swap(a, c)
}

void swap(double &a, double &b )
{
   a = b;
}


This post has been edited by IngeniousHax: 22 November 2012 - 01:55 AM

Was This Post Helpful? -1
  • +
  • -

Page 1 of 1