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:

// 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.

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.

[IMG]http://i2.photobucket.com/albums/y45/cynixx/console001.jpg[/IMG]

// 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

if(input1 > input2 && input2 > input3)
		cout << endl;
		cout << input1 << endl;
		cout << input2 << endl;
		cout << input3 << endl;

means this

if(input1 > input2 && input2 > input3)
	{
		cout << endl;
	}
	cout << input1 << endl;
	cout << input2 << endl;
	cout << input3 << endl;

you aren't allowed to use qsort?

#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;
}

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?

int compar(int *a, int *b) 
{
        return (*a>*b);
}

The comparison function for qsort should be declared like this.

int compar(const void*, const void*);

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.

int compar(int *a, int *b) 
{
        return (*a>*b);
}

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.''

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

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.

I'm sure in due time I will learn these fundamentals such as qsort, but thanks for the help in the meantime.

This article has been dead for over six months. Start a new discussion instead.