944,126 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3315
  • C RSS
Mar 14th, 2005
0

Listing Integers in Numerical Order

Expand Post »
I'm new to this whole Visual Basic idea, and my questions may be newb for all of you, but I'd like to learn. Here is my question. At school I was given an assignment , which is:

Write a program which uses functions to accept as input from the key board 3 integers and print them out in order from smallest to largest.
the program should use 3 functions
1. getdata -- will read and echo the input
2. sort -- will put the numbers in order
3. print -- will print the numbers to the screen

I want to know how to sort the three integers from user input. Would I just use a series of "if" statements?

This is what I have so far:

  1. // Excercise 2 no.2.cpp : Defines the entry point for the console application.
  2. //
  3. // ***** Includes *****
  4. #include "stdafx.h"
  5. using namespace std;
  6.  
  7. // ***** Function Prototypes *****
  8. void getData();
  9. void sortData();
  10. void printScreen();
  11. void printClosing();
  12.  
  13. int input1, input2, input3;
  14.  
  15. void main()
  16. { // startmain
  17.  
  18. // ***** Input integers from keyboard *****
  19. getData();
  20.  
  21. // ***** Sort data inputted from keyboard *****
  22. sortData();
  23.  
  24. // ***** Print results to screen*****
  25. printScreen();
  26.  
  27. // ***** Closing message *****
  28. printClosing();
  29.  
  30. } // end main
  31.  
  32. void getData()
  33. { // start getData
  34.  
  35. cout << "Please enter three integer values:\n";
  36. cin >> input1;
  37. cin >> input2;
  38. cin >> input3;
  39.  
  40. } // end getData
  41.  
  42. void sortData()
  43. { // start sortData
  44.  
  45. } // end sortData
  46.  
  47. void printScreen()
  48. { // start printScreen
  49.  
  50.  
  51.  
  52. } // end printScreen
  53.  
  54. void printClosing()
  55. { // start printClosing
  56. int wait;
  57.  
  58. cout << "\nPlease enter any key to exit.\n";
  59. cin >> wait;
  60. cout << endl;
  61.  
  62. } // end printClosing

I know it's short and I have next to nothing, but please help me with this.


<< moderator edit: added [code][/code] tags >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JayseR is offline Offline
6 posts
since Mar 2005
Mar 14th, 2005
0

Re: Listing Integers in Numerical Order

You've got the right idea, now it's just a matter of deciding what sort algorithym you want to use. Conditionals "if" would work, especially as there are only three values but you might not want to limit your application to this.

Consider declaring you inputs as an array of int's rather than three different variables. This will make it easier to code your sort routine, eventhough the way you've declared them they would be contiguous in the sort procedures frame.

Look through code snippets and I'm sure you'll find something to suite your needs.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Mar 14th, 2005
0

Re: Listing Integers in Numerical Order

OK, now I put some if statements in, ran the program, the problem now is that it takes all the "if" statements as true and outputs them all to the console.


  1. // Excercise 2 no.2.cpp : Defines the entry point for the console application.
  2. //
  3. // ***** Includes *****
  4. #include "stdafx.h"
  5. using namespace std;
  6.  
  7. // ***** Function Prototypes *****
  8. void main();
  9. void getData(int&, int&, int&);
  10. void sortData(int&, int&, int&);
  11. void printScreen();
  12. void printClosing();
  13.  
  14. int input1 = 0;
  15. int input2 = 0;
  16. int input3 = 0;
  17.  
  18. void main()
  19. { // startmain
  20.  
  21. // ***** Input integers from keyboard *****
  22. getData(input1, input2, input3);
  23.  
  24. // ***** Sort data inputted from keyboard *****
  25. sortData(input1, input2, input3);
  26.  
  27. // ***** Print results to screen*****
  28. printScreen();
  29.  
  30. // ***** Closing message *****
  31. printClosing();
  32.  
  33. } // end main
  34.  
  35. void getData(int& input1, int& input2, int& input3)
  36. { // start getData
  37.  
  38. cout << "Please enter three integer values:\n";
  39. cin >> input1 >> input2 >> input3;
  40.  
  41. } // end getData
  42.  
  43. void sortData(int& input1, int& input2, int& input3)
  44. { // start sortData
  45.  
  46. cout << "\nData entered will now be sorted numerically.\n";
  47.  
  48. if(input1 > input2 && input2 > input3)
  49. cout << endl;
  50. cout << input1 << endl;
  51. cout << input2 << endl;
  52. cout << input3 << endl;
  53.  
  54. if(input2 > input3 && input3 > input1)
  55. cout << endl;
  56. cout << input2 << endl;
  57. cout << input3 << endl;
  58. cout << input1 << endl;
  59.  
  60. if(input3 > input1 && input1 > input2)
  61. cout << endl;
  62. cout << input3 << endl;
  63. cout << input1 << endl;
  64. cout << input2 << endl; // Errors?
  65. } // end sortData
  66.  
  67.  
  68. void printScreen()
  69. { // start printScreen
  70.  
  71. } // end printScreen
  72.  
  73. void printClosing()
  74. { // start printClosing
  75.  
  76. int wait;
  77.  
  78. cout << "\nPlease enter any key to exit.\n";
  79. cin >> wait;
  80. cout << endl;
  81.  
  82. } // end printClosing
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JayseR is offline Offline
6 posts
since Mar 2005
Mar 14th, 2005
0

Re: Listing Integers in Numerical Order

Enclose blocks of code corresponding to an if statement within braces { }.

This
  1. if(input1 > input2 && input2 > input3)
  2. cout << endl;
  3. cout << input1 << endl;
  4. cout << input2 << endl;
  5. cout << input3 << endl;
means this
  1. if(input1 > input2 && input2 > input3)
  2. {
  3. cout << endl;
  4. }
  5. cout << input1 << endl;
  6. cout << input2 << endl;
  7. cout << input3 << endl;
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 16th, 2005
0

Re: Listing Integers in Numerical Order

you aren't allowed to use qsort?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. #define NINTS 3
  7.  
  8. int compar(int *a, int *b)
  9. {
  10. return (*a>*b);
  11. }
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. int i,ints[NINTS];
  16. char buf[BUFSIZ];
  17.  
  18. printf("enter %d integer values:\n",NINTS);
  19. for (i=0;i<NINTS;++i) {
  20. memset(buf,0,BUFSIZ);
  21. fgets(buf,BUFSIZ,stdin);
  22. ints[i]=strtol(buf, (char **)NULL, 10);
  23. if (errno==ERANGE) {
  24. perror("strtol");
  25. exit(1);
  26. }
  27. }
  28.  
  29. qsort(&ints,NINTS,sizeof(int),compar);
  30.  
  31. for (i=0;i<NINTS;++i)
  32. printf("%d\n",ints[i]);
  33.  
  34. return 0;
  35. }
wbk
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wbk is offline Offline
13 posts
since Mar 2005
Mar 16th, 2005
0

Re: Listing Integers in Numerical Order

Quote originally posted by wbk ...
you aren't allowed to use qsort?
JayseR seemed to make it clear that s/he is new. And doesn't qsort seem like extreme overkill for three values?

  1. int compar(int *a, int *b)
  2. {
  3. return (*a>*b);
  4. }
The comparison function for qsort should be declared like this.
int compar(const void*, const void*);
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 16th, 2005
0

Re: Listing Integers in Numerical Order

Quote originally posted by Dave Sinkula ...
JayseR seemed to make it clear that s/he is new. And doesn't qsort seem like extreme overkill for three values?
qsort is a systems programming fundamental. it might as well be learned for a sorting assignment.
Quote ...
  1. int compar(int *a, int *b)
  2. {
  3. return (*a>*b);
  4. }
The comparison function for qsort should be declared like this.
int compar(const void*, const void*);
of course. like knuth said, "beware of bugs in the above code; i've only proved it correct, not tried it.''
wbk
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wbk is offline Offline
13 posts
since Mar 2005
Mar 16th, 2005
0

Re: Listing Integers in Numerical Order

actually, you're right. suggesting qsort was idiotic.
wbk
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wbk is offline Offline
13 posts
since Mar 2005
Mar 16th, 2005
0

Re: Listing Integers in Numerical Order

If they are all returning true try using else statements...

  1. if(input1 > input2 && input2 > input3)
  2. {
  3. cout << endl;
  4. cout << input1 << endl;
  5. cout << input2 << endl;
  6. cout << input3 << endl;
  7. }
  8.  
  9. else if(input2 > input3 && input3 > input1)
  10. {
  11. cout << endl;
  12. cout << input2 << endl;
  13. cout << input3 << endl;
  14. cout << input1 << endl;
  15. }

I'm new to, so maybe it would be the same thing without else?

Also, dont forget to check if the numbers are equal to eachother as well.
Reputation Points: 11
Solved Threads: 1
Light Poster
Raven11 is offline Offline
36 posts
since Mar 2005
Mar 17th, 2005
0

Re: Listing Integers in Numerical Order

I'm sure in due time I will learn these fundamentals such as qsort, but thanks for the help in the meantime.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JayseR is offline Offline
6 posts
since Mar 2005

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: arrays
Next Thread in C Forum Timeline: Unhijacked Thread: Coding * in a circle / Oval





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


Follow us on Twitter


© 2011 DaniWeb® LLC