Listing Integers in Numerical Order

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 6
Reputation: JayseR is an unknown quantity at this point 
Solved Threads: 0
JayseR JayseR is offline Offline
Newbie Poster

Listing Integers in Numerical Order

 
0
  #1
Mar 14th, 2005
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 >>
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Listing Integers in Numerical Order

 
0
  #2
Mar 14th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 6
Reputation: JayseR is an unknown quantity at this point 
Solved Threads: 0
JayseR JayseR is offline Offline
Newbie Poster

Re: Listing Integers in Numerical Order

 
0
  #3
Mar 14th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,450
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Listing Integers in Numerical Order

 
0
  #4
Mar 14th, 2005
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;
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 13
Reputation: wbk is an unknown quantity at this point 
Solved Threads: 0
wbk wbk is offline Offline
Newbie Poster

Re: Listing Integers in Numerical Order

 
0
  #5
Mar 16th, 2005
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,450
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Listing Integers in Numerical Order

 
0
  #6
Mar 16th, 2005
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*);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 13
Reputation: wbk is an unknown quantity at this point 
Solved Threads: 0
wbk wbk is offline Offline
Newbie Poster

Re: Listing Integers in Numerical Order

 
0
  #7
Mar 16th, 2005
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.
  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.''
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 13
Reputation: wbk is an unknown quantity at this point 
Solved Threads: 0
wbk wbk is offline Offline
Newbie Poster

Re: Listing Integers in Numerical Order

 
0
  #8
Mar 16th, 2005
actually, you're right. suggesting qsort was idiotic.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 36
Reputation: Raven11 is an unknown quantity at this point 
Solved Threads: 1
Raven11 Raven11 is offline Offline
Light Poster

Re: Listing Integers in Numerical Order

 
0
  #9
Mar 16th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 6
Reputation: JayseR is an unknown quantity at this point 
Solved Threads: 0
JayseR JayseR is offline Offline
Newbie Poster

Re: Listing Integers in Numerical Order

 
0
  #10
Mar 17th, 2005
I'm sure in due time I will learn these fundamentals such as qsort, but thanks for the help in the meantime.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC