By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Very Lost and Very need of help

P: 35
I'm suppose to edit the function called swap_values so that it receives three numbers (not two nubmers) called first , second, and third. These numbers will not be in any special order (for example enter 20.0, 3.1 and 9.3). The function swap-values will need to be edited to use more "if" statements to place the smallest vlaue in the variable named first, the second smallest number in the variable named second, and the largest number in the variable named third.
You are also to edit the cout statements to print all three nubmers in the correct order (the variable first will be printed first and have in it the smallest number, the variable second will be pritned next and will have the middle number in it and the variable third will be prited last and have the largest number in it). Than take the cout statements (that print the three numbers in order) and create another another function called print_inorder. This new function will print the numbers in order from smallest to largest.
Expand|Select|Wrap|Line Numbers
  1. # include <iostream.h> 
  2. // recall function definitions can be written either before or after the main 
  3. void swap_values (float &a, float &b) 
  4. float temp; 
  5.  
  6. temp = a; 
  7. a = b; 
  8. b = temp; 
  9.  
  10. void main (void) 
  11.  
  12. void swap_values (float &, float &); //prototype 
  13.  
  14. float first = 10000.0; 
  15. float second = 0.00001; 
  16.  
  17. swap_values(first, secondl); // function call statement 
  18.  
  19. cout << “Big contains “ << big << endl; 
  20. cout << “Small contains “ << small << endl; 
  21. } //end of code to swap two numbers 
  22.  
At the top of the program insert comments that contain the assignment number, question number, and your name
Edit the addThree function and change its name to useTwo.
Edit the useTwo function accept only two float numbers
Within the useTwo function add additional code to
calculate the sum, product, quotient and subtraction value of the two floats entered.
Return the 4 math answers calculated in the useTwo function to the main along with the two floats used to get the answers.
Edit the printResults to receive 6 floats, the two numbers entered in the useTwo function the the 4 math answers calcualted in the useTwo fucntion. The printResults function should be edited to include the code to print all 6 numbers, formatted, and clearly labeled.
Expand|Select|Wrap|Line Numbers
  1. # include <iostream.h> 
  2. // recall function definitions can be written either before or after the main 
  3. void addThree (float &a, float &b, float &c, float &s) 
  4. cout<<”enter number 1: “; 
  5. cin>>a; 
  6. cout<<”enter number 2: “; 
  7. cin>>b; 
  8. cout<<”enter number 3: “; 
  9. cin>>c; 
  10. s = a + b + c; 
  11.  
  12. void printResults (float a, float b, float c, float s) 
  13. cout<<”number 1 = “<<a<<endl; 
  14. cout<<”number 2 = “<<b<<endl; 
  15. cout<<”number 3 = “<<c<<endl; 
  16. cout<<”sum = “<<s<<endl; 
  17.  
  18. void main (void) 
  19.  
  20. void addThree (float &a, float &b, float &c, float &s) ; // prototype 
  21. void printResults (float a, float b, float c, float s); // prototype 
  22.  
  23. float num1, num2, num3, sum; 
  24.  
  25. addThree (num1, num2, num3, sum); // call statement 
  26. printResults(num1, num2, num3, sum);// call statement 
  27. } //End of Code 
  28.  
Nov 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: 35
Any help please
Nov 16 '06 #2

sivadhas2006
100+
P: 142
Hi,

You want to create print_inorder function to print the values from smaller to bigger right?

Regards,
M.Sivadhas.
Nov 17 '06 #3

P: 5
can u be more specific as to what you want to do?
Nov 17 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
I'm suppose to edit the function called swap_values so that it receives three numbers (not two nubmers) called first , second, and third. These numbers will not be in any special order (for example enter 20.0, 3.1 and 9.3). The function swap-values will need to be edited to use more "if" statements to place the smallest vlaue in the variable named first, the second smallest number in the variable named second, and the largest number in the variable named third.
So there are basically 2 things to do

1. Alter the prototype of swap_values. Can you do this re-wrte swap_values prototype to accept 3 parameters?

2. Change the logic that it implements.

As an initial step to altering the logic it may be worth writing down (in English) the new logic that this function will implement. Once you you have an idea of the logic it needs to implement then you can actually have an attempt at implementing it.
Nov 17 '06 #5

P: 60
For sure this is what you want
#include <iostream>
using namespace std;

void swap_values(float & first, float & second, float &third)
{
float first_holder,second_holder,third_holder;
if(first > second && first > third)
{
third_holder = third;
first_holder = first;
third = first;
if(third_holder < second)
first = third_holder;
else
{
first = second;
second = third_holder;
}
}
else if(second > first && second > third)
{
third_holder = third;
second_holder = second;
third = second;
if(first < third_holder)
second = third_holder;
else
{
second = first;
first = third_holder;
}
}
else
{
if(first > second)
{
first_holder = first;
second_holder = second;
first = second_holder;
second = first_holder;
}
}
}
void print_inorder(float first, float second, float third)
{
cout<<"The first number is "<<first<<endl;
cout<<"The second number is "<<second<<endl;
cout<<"The third number is "<<third<<endl;
}
int main()
{
float first,second,third;
cout<<"Enter the first number ";
cin>>first;
cout<<"Enter the second number ";
cin>>second;
cout<<"Enter the third number ";
cin>>third;
swap_values(first,second,third);
print_inorder(first,second,third);
return 0 ;
}
Nov 17 '06 #6

P: 35
For sure this is what you want
#include <iostream>
using namespace std;

void swap_values(float & first, float & second, float &third)
{
float first_holder,second_holder,third_holder;
if(first > second && first > third)
{
third_holder = third;
first_holder = first;
third = first;
if(third_holder < second)
first = third_holder;
else
{
first = second;
second = third_holder;
}
}
else if(second > first && second > third)
{
third_holder = third;
second_holder = second;
third = second;
if(first < third_holder)
second = third_holder;
else
{
second = first;
first = third_holder;
}
}
else
{
if(first > second)
{
first_holder = first;
second_holder = second;
first = second_holder;
second = first_holder;
}
}
}
void print_inorder(float first, float second, float third)
{
cout<<"The first number is "<<first<<endl;
cout<<"The second number is "<<second<<endl;
cout<<"The third number is "<<third<<endl;
}
int main()
{
float first,second,third;
cout<<"Enter the first number ";
cin>>first;
cout<<"Enter the second number ";
cin>>second;
cout<<"Enter the third number ";
cin>>third;
swap_values(first,second,third);
print_inorder(first,second,third);
return 0 ;
}
There should be two different codes
Nov 17 '06 #7

Post your reply

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