c++ functions to compare numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

c++ functions to compare numbers

 
0
  #1
Oct 11th, 2007
  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <string>
  5. #include<string>
  6. #include<iostream>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. int num1 = 0;
  13. int num2 = 0;
  14. char again = 'y';
  15. while (again=='y')
  16. {
  17. cout<<"Please enter two numbers to compare "<<endl;
  18. cin>>num1>>num2;
  19. cout<<"The two numbers entered in order were "<<num1<<" and "<<num2<<endl;
  20. if (num1<num2)
  21. {
  22. cout<<num2<<" is the larger of "<<num1<< " and "<<num2<<endl;
  23. }
  24. else if (num1>num2)
  25. {
  26. cout<<num1<<" is the larger of "<<num1<< " and "<<num2<<endl;
  27. }
  28. cout<<"\n Go again?(y/n): ";
  29. cin>>again;
  30. }
  31.  
  32. return 0;
  33. }
Good day. My assignment is as follows: Write a c++ program using functions, that will accept 2 numbers from the keyboard and then determine which is
the larger and smaller of the two. There should be two functions:
1. getnumbers

Output within the 1st function should display
The two numbers entered in order were XXX and YYY

2. findbig
Output within the 2nd function should display
XXX is the larger of XXX and YYY
Put the main function within an "again" loop that will continue until the user no longer wants to.(skip a line between each sets output).
Could you please explain to me how i'd go about creating two such functions? I had a bit of problem grasping the concept of functions....thanks. The above program does work, but it doesn't have the two distinct functions as is requested.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: c++ functions to compare numbers

 
0
  #2
Oct 11th, 2007
If getnumber() doen't ask for values but just shows them, it would look like this
  1.  
  2. int findbig(int a, int b)
  3. {
  4. // will return -1, if a is greater
  5. // 0 if both are equal
  6. // 1 if b is greater
  7. }
  8. void getnumbers(int a, int b)
  9. {
  10. // your output
  11.  
  12. int x = findbig(a, b);
  13.  
  14. // check the values of x
  15. // decide which is greater
  16. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ functions to compare numbers

 
0
  #3
Oct 11th, 2007
HHHmmmmm. I didn't quite get it. I still got problems. Below is my revised code:

  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <string>
  5. #include<string>
  6. #include<iostream>
  7.  
  8. using namespace std;
  9.  
  10. int num1, num2;
  11.  
  12. int findbig(int num1, int num2);
  13.  
  14. int main()
  15. {
  16. char again = 'y';
  17.  
  18. while (again=='y')
  19. {
  20. cout<<"Please enter two numbers to compare "<<endl;
  21. cin>>num1>>num2;
  22. cout<<"The two numbers entered in order were "<<num1<<" and "<<num2<<endl;
  23. }
  24. void getnumbers(int num1, int num2);
  25.  
  26. if (num1<num2)
  27. int x = findbig(num1, num2);
  28. cout<<num2<<" is the larger of "<<num1<< " and "<<num2<<endl;
  29.  
  30. else if (num1>num2)
  31. {
  32. cout<<num1<<" is the larger of "<<num1<< " and "<<num2<<endl;
  33. }
  34. cout<<"\n Go again?(y/n): ";
  35. cin>>again;
  36.  
  37. return 0;
  38. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,663
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 725
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: c++ functions to compare numbers

 
1
  #4
Oct 11th, 2007
You're overcomplicating things, I think. Here's a sample without the "again" crap that should give you an idea of what's expected when functions get involved:
  1. #include <iostream>
  2.  
  3. int findbig ( int num1, int num2 )
  4. {
  5. return num1 > num2 ? num1 : num2;
  6. }
  7.  
  8. void getnumbers ( int& num1, int& num2 )
  9. {
  10. std::cout<<"Please enter two numbers to compare: ";
  11. std::cin>> num1 >> num2;
  12. }
  13.  
  14. int main()
  15. {
  16. int num1, num2;
  17.  
  18. getnumbers ( num1, num2 );
  19. std::cout<< findbig ( num1, num2 ) <<" is larger\n";
  20. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

Re: c++ functions to compare numbers

 
0
  #5
Oct 11th, 2007
widenning for many cases that is there are many nums to compare to each others, how can you solve? we use this whether when the comparisors are and maybe more than two a little but this algorithm is not brief. you can use another algorithm. Use an indirect varia.
Say It Like It Is.
Có sao nói vậy.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ functions to compare numbers

 
0
  #6
Oct 11th, 2007
Thx for your prompt reply Narue, we're almost there, however, if I compile using your suggested format...i get the following error:

  1. 1>------ Build started: Project: bigone, Configuration: Debug Win32 ------
  2. 1>Compiling...
  3. 1>bigone.cpp
  4. 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(16) : error C2447: '{' : missing function header (old-style formal list?)
  5. 1>Build log was saved at "file://c:\Users\Documents\Visual Studio 2005\Projects\bigone\bigone\Debug\BuildLog.htm"
  6. 1>bigone - 1 error(s), 0 warning(s)
  7. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

Re: c++ functions to compare numbers

 
0
  #7
Oct 11th, 2007
there is an mistake, this is, function Void not using return to back due to the recurs. U must change it into INT type.
Last edited by vincent551987vn; Oct 11th, 2007 at 2:40 pm. Reason: more disscuss
Say It Like It Is.
Có sao nói vậy.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ functions to compare numbers

 
0
  #8
Oct 11th, 2007
I'm thinking perhaps because we're using different compilers. Also, just a side-question. I'm aware that C++ is used to create programs like these and other tools that would be very repetitive for a human to do, such as certain calcualtions, can we create c++ programs to do other internal stuff? By that i mean, can you create a c++ program to shutdown your computer or to lauch internet explorer?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,663
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 725
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: c++ functions to compare numbers

 
0
  #9
Oct 11th, 2007
>if I compile using your suggested format...i get the following error:
You made a change that broke the code. Post what you're compiling so I can see where the error is.

>widenning for many cases that is there are many nums to compare to each others, how can you solve?
Load the numbers into a suitable data structure (I'd suggest a set), and then search for the smallest. For example:
  1. #include <iostream>
  2. #include <set>
  3.  
  4. int findbig ( std::set<int>& numbers )
  5. {
  6. return *numbers.rbegin();
  7. }
  8.  
  9. void getnumbers ( std::set<int>& numbers )
  10. {
  11. int num;
  12.  
  13. std::cout<<"Please enter a list of numbers to compare: ";
  14.  
  15. while ( std::cin>> num )
  16. numbers.insert ( num );
  17. }
  18.  
  19. int main()
  20. {
  21. std::set<int> numbers;
  22.  
  23. getnumbers ( numbers );
  24. std::cout<< findbig ( numbers ) <<" is larger\n";
  25. }
>I'm thinking perhaps because we're using different compilers.
Nope, it looks like we're using the same one.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

Re: c++ functions to compare numbers

 
0
  #10
Oct 11th, 2007
I have no imagination bout ur speech. but there is a thing i want to say, i have had both of compilers. .
Say It Like It Is.
Có sao nói vậy.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC