| | |
Listing Integers in Numerical Order
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 6
Reputation:
Solved Threads: 0
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:
I know it's short and I have next to nothing, but please help me with this.
<< moderator edit: added [code][/code] tags >>
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:
C Syntax (Toggle Plain Text)
// Excercise 2 no.2.cpp : Defines the entry point for the console application. // // ***** Includes ***** #include "stdafx.h" using namespace std; // ***** Function Prototypes ***** void getData(); void sortData(); void printScreen(); void printClosing(); int input1, input2, input3; void main() { // startmain // ***** Input integers from keyboard ***** getData(); // ***** Sort data inputted from keyboard ***** sortData(); // ***** Print results to screen***** printScreen(); // ***** Closing message ***** printClosing(); } // end main void getData() { // start getData cout << "Please enter three integer values:\n"; cin >> input1; cin >> input2; cin >> input3; } // end getData void sortData() { // start sortData } // end sortData void printScreen() { // start printScreen } // end printScreen void printClosing() { // start printClosing int wait; cout << "\nPlease enter any key to exit.\n"; cin >> wait; cout << endl; } // 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 >>
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.
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.
•
•
Join Date: Mar 2005
Posts: 6
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
// Excercise 2 no.2.cpp : Defines the entry point for the console application. // // ***** Includes ***** #include "stdafx.h" using namespace std; // ***** Function Prototypes ***** void main(); void getData(int&, int&, int&); void sortData(int&, int&, int&); void printScreen(); void printClosing(); int input1 = 0; int input2 = 0; int input3 = 0; void main() { // startmain // ***** Input integers from keyboard ***** getData(input1, input2, input3); // ***** Sort data inputted from keyboard ***** sortData(input1, input2, input3); // ***** Print results to screen***** printScreen(); // ***** Closing message ***** printClosing(); } // end main void getData(int& input1, int& input2, int& input3) { // start getData cout << "Please enter three integer values:\n"; cin >> input1 >> input2 >> input3; } // end getData void sortData(int& input1, int& input2, int& input3) { // start sortData cout << "\nData entered will now be sorted numerically.\n"; if(input1 > input2 && input2 > input3) cout << endl; cout << input1 << endl; cout << input2 << endl; cout << input3 << endl; if(input2 > input3 && input3 > input1) cout << endl; cout << input2 << endl; cout << input3 << endl; cout << input1 << endl; if(input3 > input1 && input1 > input2) cout << endl; cout << input3 << endl; cout << input1 << endl; cout << input2 << endl; // Errors? } // end sortData void printScreen() { // start printScreen } // end printScreen void printClosing() { // start printClosing int wait; cout << "\nPlease enter any key to exit.\n"; cin >> wait; cout << endl; } // end printClosing
Enclose blocks of code corresponding to an if statement within braces { }.
This means this
This
C Syntax (Toggle Plain Text)
if(input1 > input2 && input2 > input3) cout << endl; cout << input1 << endl; cout << input2 << endl; cout << input3 << endl;
C Syntax (Toggle Plain Text)
if(input1 > input2 && input2 > input3) { cout << endl; } cout << input1 << endl; cout << input2 << endl; 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
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Solved Threads: 0
you aren't allowed to use qsort?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define NINTS 3 int compar(int *a, int *b) { return (*a>*b); } int main(int argc, char *argv[]) { int i,ints[NINTS]; char buf[BUFSIZ]; printf("enter %d integer values:\n",NINTS); for (i=0;i<NINTS;++i) { memset(buf,0,BUFSIZ); fgets(buf,BUFSIZ,stdin); ints[i]=strtol(buf, (char **)NULL, 10); if (errno==ERANGE) { perror("strtol"); exit(1); } } qsort(&ints,NINTS,sizeof(int),compar); for (i=0;i<NINTS;++i) printf("%d\n",ints[i]); return 0; }
•
•
•
•
Originally Posted by wbk
you aren't allowed to use qsort?
C Syntax (Toggle Plain Text)
int compar(int *a, int *b) { return (*a>*b); }
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
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
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?
•
•
•
•
The comparison function for qsort should be declared like this.C Syntax (Toggle Plain Text)
int compar(int *a, int *b) { return (*a>*b); }int compar(const void*, const void*);
•
•
Join Date: Mar 2005
Posts: 36
Reputation:
Solved Threads: 1
If they are all returning true try using else statements...
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.
C Syntax (Toggle Plain Text)
if(input1 > input2 && input2 > input3) { cout << endl; cout << input1 << endl; cout << input2 << endl; cout << input3 << endl; } else if(input2 > input3 && input3 > input1) { cout << endl; cout << input2 << endl; cout << input3 << endl; cout << input1 << endl; }
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.
![]() |
Other Threads in the C Forum
- Previous Thread: arrays
- Next Thread: Unhijacked Thread: Coding * in a circle / Oval
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch centimeter char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling highest homework i/o ide infiniteloop initialization interest intmain() keyboard kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32api windows.h






