Computing.Net > Forums > Programming > C programming

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C programming

Reply to Message Icon

Name: Death-Knight
Date: March 10, 2003 at 18:43:28 Pacific
OS: WinXP
CPU/Ram: 1.9Ghz/1.0Gigz
Comment:

Hi, HOw do i program a program to sort numbers? It will sort from the 10 numbers i enter. Please help thanks



Sponsored Link
Ads by Google

Response Number 1
Name: borelli34
Date: March 10, 2003 at 19:53:03 Pacific
Reply:


================================================================

I'm using [ and ] in place of the greater-than and less-than signs.

void swap(int i1,i2){
int i3;
i3=i1;
i1=i2;
i2=i3;
}

int nums[10];

void main(){
int x,y;
for (x=0;x[11;x++){
for (y=x;y[11;y++){
if x]y then swap(x,y);
}
}

You will have to modify this slightly to mix it into your program but the method is clearly displayed here.

borelli34


0

Response Number 2
Name: Death-Knight
Date: March 10, 2003 at 21:14:44 Pacific
Reply:

This is C++ Program? I didn't learn that in C tho. I want it to be in C program. Thanks


0

Response Number 3
Name: Lightning
Date: March 10, 2003 at 23:49:00 Pacific
Reply:

No, that is C, not C++.

Just be sure to substitute the [ and ] for the appropriate less than (instead of [), greater than (instead of ]) signs


0

Response Number 4
Name: Deaht Knight
Date: March 11, 2003 at 07:55:33 Pacific
Reply:

I got the idea bout your program.... but can't get it to work. Damn.


0

Response Number 5
Name: elric
Date: March 12, 2003 at 19:05:57 Pacific
Reply:

G'day,

These two examples work with TurboC 2.0. The first one is a straight copy of the basic bubble sort algorithm adapted for the C programming language. The second is a version using a function swap()- ( acknowlegement Borelli34) and involves pointers.

/***************************/
/***** Bubble sort Example,12/3/03 */
/************ Author: Traditional * /


#include
#include

/* Main Function */

void main(void){

int table[10]={23,45,1,5,65,24,8,7,10,55};
int top, search, hold,index;

clrscr();

for(top=0;top table[top]){
hold=table[top];
table[top]=table[search];
table[search]=hold;
}
}
printf("The array is as follows : \n");
for (index=0;index
#include

/********Function Prototype********/
void swap(int *x, int *y);

/**********Test Array********************/
int table[10]={23,45,1,5,65,24,8,7,10,55};

/* Main Function */
void main(void){

int top, search,index;

clrscr();
/*********Bubble Sort Algorithm***********/
for(top=0;top table[top]){
swap(&table[top],&table[search]);
}
}
/********** Print Results *************/
printf("The array is as follows : \n");
for (index=0;index 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);

}/* End Main*/

/*********Function Swap****/
void swap(int *x ,int *y){
int temp;

temp=*x;
*x=*y;
*y=temp;
}

You might like to code these into your machine (probably the first one) and set a few watches and and then single step (F8) thru' the code to see how the algorithm works.
Hope this helps.
regards,
Elric



0

Related Posts

See More



Response Number 6
Name: elric
Date: March 12, 2003 at 19:14:21 Pacific
Reply:

G'day,

I have absolutely no idea what happend to that formatting!!!
I hope these get through better...

/****************** */
/*******Bubble sort Example 12/3/03 */
/*******Author: Traditional * /


#include
#include

/* Main Function */

void main(void){

int table[10]={23,45,1,5,65,24,8,7,10,55};
int top, search, hold,index;

clrscr();

for(top=0;top table[top]){
hold=table[top];
table[top]=table[search];
table[search]=hold;
}
}

printf("The array is as follows : \n");
for (index=0;index
#include

/********Function Prototype********/
void swap(int *x, int *y);

/**********Test Array********************/
int table[10]={23,45,1,5,65,24,8,7,10,55};

/* Main Function */
void main(void){

int top, search,index;

clrscr();
/*********Bubble Sort Algorithm***********/
for(top=0;top table[top]){
swap(&table[top],&table[search]);
}
}
/********** Print Results *************/
printf("The array is as follows : \n");
for (index=0;index 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);

}/* End Main*/

/*********Function Swap****/
void swap(int *x ,int *y){
int temp;

temp=*x;
*x=*y;
*y=temp;
}

I hope these get thru' OK
regards,
Elric


0

Response Number 7
Name: elric
Date: March 12, 2003 at 19:22:17 Pacific
Reply:

G'day,

Heck!! This is longer than Ben Hur!!
I can't work out why these will not format, so I am going to send them individually and see if that works...fingers crossed!!

#include
#include

/* Main Function */

void main(void){

int table[10]={23,45,1,5,65,24,8,7,10,55};
int top, search, hold,index;

clrscr();

for(top=0;top table[top]){
hold=table[top];
table[top]=table[search];
table[search]=hold;
}
}

printf("The array is as follows : \n");
for (index=0;index 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);

}

This looks fine in my editor...
regards,
Elric


0

Response Number 8
Name: elric
Date: March 12, 2003 at 19:36:32 Pacific
Reply:

G'day,

Still won't work. It's those greater than and less than keys (what's up ??????????)
I'll have to modify my programme and use [ and ] instead (now I know why Borelli34 used them...)

#include [stdio.h]
#include [conio.h]

/* Main Function */

void main(void){

int table[10]={23,45,1,5,65,24,8,7,10,55};
int top, search, hold,index;

clrscr();

for(top=0;top[9;top++)
for(search=top+1;search[10;search++){
if(table[search] ] table[top]){
hold=table[top];
table[top]=table[search];
table[search]=hold;
}
}

printf("The array is as follows : \n");
for (index=0;index[ 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);

}


/************ ************/
/************ Bubble sort Example 12/3/03 */

#include [stdio.h]
#include [conio.h]

/********Function Prototype********/
void swap(int *x, int *y);

/**********Test Array********************/
int table[10]={23,45,1,5,65,24,8,7,10,55};

/* Main Function */
void main(void){

int top, search,index;

clrscr();
/*********Bubble Sort Algorithm***********/
for(top=0;top[9;top++)
for(search=top+1;search[10;search++){
if(table[search] ] table[top]){
swap(&table[top],&table[search]);
}
}
/********** Print Results *************/
printf("The array is as follows : \n");
for (index=0;index[ 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);

}/* End Main*/

/*********Function Swap****/
void swap(int *x ,int *y){
int temp;

temp=*x;
*x=*y;
*y=temp;
}

That seems to have worked. Note only replace [ (less than) and ] (greater than) in the appropiate places ie I also use them for array indexes (should only be replaced in the if and for next loops).
That was a lot more complicated than it should have been, but at least I have learned something too...
regards,
Elric


0

Response Number 9
Name: pissedatlop
Date: March 22, 2003 at 13:30:29 Pacific
Reply:

I am typing this straight from my textbook. Hope it helps


//sorting
for (out=0; outlist[in])
{
temp=list[in];
list[in]=list[out];
list[out]=temp;
}
}
}
printf("The sorted array in ascending order is.....\n");


0

Response Number 10
Name: pissedatlop
Date: March 22, 2003 at 13:33:05 Pacific
Reply:

sorry, this IS the correct one..
I am typing this straight from my textbook. Hope it helps


//sorting
for (out=0; outlist[in])
{
temp=list[in];
list[in]=list[out];
list[out]=temp;
}
}
}
printf("The sorted array in ascending order is.....\n");


0

Response Number 11
Name: pissedatlop
Date: March 22, 2003 at 13:37:10 Pacific
Reply:

for (out=0; out[size-1; out++)
{
for (in=out+1; in[size; in++)
{
if (list[out] ] list[in])
{
temp=list[in];
list[in]=list[out];
list[out]=temp;
}
}
}
printf("The sorted array in ascending order is.....\n");



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C programming

c++ programming for novell www.computing.net/answers/programming/c-programming-for-novell/2525.html

Bike Trader C program www.computing.net/answers/programming/bike-trader-c-program/6557.html

C++ Programming Website www.computing.net/answers/programming/c-programming-website/4752.html