Old 1st October 2004, 18:46   #1
Commasterharry
Senior Member
 
Commasterharry's Avatar
 
Join Date: Feb 2001
Posts: 183
c++ help!

i need an algorithm that takes in 3 numbers and outputs them in ascending order, using nothing more complicated than a while loop. i'm st00pid
Commasterharry is offline   Reply With Quote
Old 1st October 2004, 19:00   #2
will
Nullsoft Newbie (Moderator)
 
will's Avatar
 
Join Date: Mar 2001
Location: Sheffield, England
Posts: 5,569
code:
static int sortFunc(const void *elem1, const void *elem2)
{
int *a=(int *)*(void **)elem1;
int *b=(int *)*(void **)elem2;

return *a - *b;
}

void main()
{
int list[3] = {2,3,1};
qsort(&list,3,sizeof(int),sortFunc);
for(int i=0; i<3; i++)
printf("%d\n", list[i]);
}

Or something. You better check that.

DO NOT PM ME WITH TECH SUPPORT QUESTIONS
will is offline   Reply With Quote
Old 1st October 2004, 21:06   #3
xzxzzx
Forum King
 
xzxzzx's Avatar
 
Join Date: Aug 2002
Posts: 7,254
Very stupid/simple bubble sort:
code:
#include <stdio.h>;

void main()
{
int list[3] = {2,3,1};
for(int i = 1; i < 3; i++){
if(list[i-1] > list[i]){
list[i-1] ^= list[i] ^= list[i-1] ^= list[i];
i = 0;
}
}
printf("first: %d, second: %d, third: %d", list[0], list[1], list[2]);
}


Freedom of speech is the basic freedom of humanity. When you've lost that, you've lost everything.
1\/\/4y 34|<$p4y 1gp4y 33714y, 0d4y 0uy4y? | Roses are #FF0000; Violets are #0000FF; chown -R ${YOU} ~/base
The DMCA. It really is that bad. : Count for your life.
xzxzzx is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Community Center > General Discussions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump