# Probably a trivial C question

Status
Not open for further replies.

#### tanky321

##### Member level 1
Im trying to right a program that is given 5 integers, it then sorts the integers in numerical order. Catch is it has to use if else statements or loops.

Its for a class im in, and im at a complete loss. Heres what I have so far, only for the first two numbers, but it doesnt work. Any tips or help would be appreciated.

void order(int a, int b, int c, int d, int e)
{ int num_1=0;
int num_2=0;

if(a<b && a<c && a<d && a<e)
{num_1=a;
a=0;}
else if(b<a && b<c && b<d && b<e)
{num_1=b;
b=0;}
else if(c<a && c<b && c<d && c<e)
{num_1=c;
c=0;}
else if(d<a && d<b && d<c && d<e)
{num_1=d;
d=0;}
else
{num_1=e;
e=0;}
if (a<b && a<c && a<d && a<e && a!=num_1 && a!=0)
{num_2=a;
a=0;}
else if(b<a && b<c && b<d && b<e && b!=num_1 && b!=0)
{num_2=b;
b=0;}
else if(c<a && c<b && c<d && c<e && c!=num_1 && c!=0)
{num_2=c;
c=0;}
else if(d<a && d<b && d<c && d<e && d!=num_1 && d!=0)
{num_2=d;
d=0;}
else if(e<a && e<b && e<c && e<d && e!=num_1 && e!=0)
{num_2=e;
e=0;}

printf("%d %d %d\n\n\n", num_1,num_2);

}

#### sunderwood

##### Member level 4
there are many ways of doing this. Much easier to read/write when using a loop.
An easy way to do it is to find the min of all five, then store it in "num_1", then find the min of the remaining four, store, and repeat until done.

#### echo47

Instead of using five separate integer variables, it would be easier to put the input data into a five-element array, and then process it with a loop.

Try searching Google for "sorting algorithms", and you will find descriptions of various techniques. For example, the "bubble sort" is probably the simplest of all, although it's relatively inefficient:
https://en.wikipedia.org/wiki/Sorting_algorithm

#### tanky321

##### Member level 1
The sorting and arrays sound great, but I cant use them. Im pretty much restricted to Loops and if else statements.

I got the program to working using a for loop, but it wont work if I have duplicate entries. Il post up my program in a second, just need to open my laptop.

Thanks guys.

#### artem

use state variable (bitmap) to store whether vars are printed or not . Start to find highest one , once found - print it and mark its state bitmap variable as processed then start again in loop and skip those vars whose states are marked as sorted. Then no array will be needed.

#### tanky321

##### Member level 1
This is what I have, i use 1000000 so it will kind of exclude that variable. But it ends up screwing me in the long run. I would love to use that bitmap function, but we haven't covered that, and he wont let us use it.

void order(int a, int b, int c, int d, int e)
{ int num_1=0;
int count;

for(count=0;count<5;count++)
{
if(a<b && a<c && a<d && a<e)
{num_1=a;
a=1000000;}
else if(b<a && b<c && b<d && b<e)
{num_1=b;
b=1000000;}
else if(c<a && c<b && c<d && c<e)
{num_1=c;
c=1000000;}
else if(d<a && d<b && d<c && d<e)
{num_1=d;
d=1000000;}
else if(e<a && e<b && e<c && e<d)
{num_1=e;
e=1000000;}

printf(" %d ",num_1);
}
}

#### jhbbunch

##### Full Member level 4
Code:
// this is a quick and dirty implementation of a bubble sort. It sorts in ascending
// order. I used devcpp

#include <cstdlib>
#include <stdio.h>
#include <conio.h>

using namespace std;
void SortIt(int *);

int main(int argc, char *argv[])
{
int a[] = {1123, 675, 435, 33, 456};  ;array of numbers to be sorted
int i;

for (i = 0; i <= 4; i++)                     ;output unsorted numbers
printf("%d  ", a[i]);

SortIt(a);

printf("\n\n");

for (i = 0; i <= 4; i++)                    ;output sorted numbers
printf("%d  ", a[i]);

getch();
system("PAUSE");
return EXIT_SUCCESS;
}

void SortIt(int *a)
{
int  i, flag, temp, count;

flag = 1;   // 0 will indicate no swaps were made, sorting is done
count = 0;  // just used for debugging, it will
// end an infinite loop condition.

while (flag !=  0 & count < 2000)
{
flag = 0;
count++;
for (i = 0; i <= 3; i++)
{
if (a[i] > a[i+1] )     //test for greater than condition
{
temp = a[i];        // and swap if necessary
a[i] = a[i+1];
a[i+1] = temp;
flag = 1;             //set flag to indicate still some sorting to do
} // end of it
}  // end of for
} // end of while
} // end of SortIt

This is what the output of the program will look like:
1123 675 435 33 456

33 435 456 675 1123

#### echo47

Your professor's rules don't leave you much to work with.
Code:
void order(int a, int b, int c, int d, int e)
{
if (a > b)
order(b, a, c, d, e);
else if (b > c)
order(a, c, b, d, e);
else if (c > d)
order(a, b, d, c, e);
else if (d > e)
order(a, b, c, e, d);
else
printf("%d %d %d %d %d\n", a, b, c, d, e);
}

#### tanky321

##### Member level 1
echo47 said:
Your professor's rules don't leave you much to work with.
Code:
void order(int a, int b, int c, int d, int e)
{
if (a > b)
order(b, a, c, d, e);
else if (b > c)
order(a, c, b, d, e);
else if (c > d)
order(a, b, d, c, e);
else if (d > e)
order(a, b, c, e, d);
else
printf("%d %d %d %d %d\n", a, b, c, d, e);
}

That looks good, but the only thing is that we havent used the Order function yet. I actually did something to my for loop program and it worked. I changed all of the "<" to "<=" and it works like a charm. I know its not the right way to do it, but it will work!

I really thank all of you for your help, Its opened my eyes up!

Andrew

#### echo47

Look again! 'order' isn't some library function. The 'order' function calls itself.

Oops, earlier I said "reentrant ". The correct term is "recursive".

#### tanky321

##### Member level 1
echo47 said:
Look again! 'order' isn't some library function. This function calls itself.

Oops, earlier I said "reentrant ". The correct term is "recursive".

oops i didnt even notice that! Thats pretty ingenious, but he would know I didnt come up with that! I dont have the experience to come up with that.

Thanks alot! I really appreciate it!!

#### mmelek1

##### Member level 4
const int size =5;
int temp;
int x[size]={2, 6, 1, 7, 9};
for(int i=0;i<size-1;i++)
for(int j=i+1;j<size;j++)
if(x<x[j]){temp=x;x=x[j];x[j]=temp;}
for(i=0;i<5;i++)cout<<x;

Status
Not open for further replies.