943,762 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 13690
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 11th, 2007
0

c++ functions to compare numbers

Expand Post »
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

If getnumber() doen't ask for values but just shows them, it would look like this
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Oct 11th, 2007
0

Re: c++ functions to compare numbers

HHHmmmmm. I didn't quite get it. I still got problems. Below is my revised code:

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
1

Re: c++ functions to compare numbers

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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 11th, 2007
0

Re: c++ functions to compare numbers

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vincent551987vn is offline Offline
14 posts
since Oct 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

Thx for your prompt reply Narue, we're almost there, however, if I compile using your suggested format...i get the following error:

C++ Syntax (Toggle Plain Text)
  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 ==========
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vincent551987vn is offline Offline
14 posts
since Oct 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

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?
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 11th, 2007
0

Re: c++ functions to compare numbers

I have no imagination bout ur speech. but there is a thing i want to say, i have had both of compilers. .
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vincent551987vn is offline Offline
14 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Solution for Big Integer used Linked - List.
Next Thread in C++ Forum Timeline: Participating in projects





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC