Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do i Put numbers in arrays in order?? 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How Do i Put numbers in arrays in order??

for example




#include <stdio.h>

int array[] = { 12, 3, 100, 23, 11, 15, 200, 33, 199, 198, 22}

void main()
{

}

 
Use the qsort function available in <stdlib.h> or, from the various algorithms used, write a function of your own.

Bye.
Ankan.
Please do correct me if I am wrong. :)
 
I had this same problem about two days ago. qsort is what you need. This program works:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

float data[10];
float a, b;


int compare(const void *, const void *);
int compare(const void *a, const void *b)
{
float x, y;
x = *(float *)a;
y = *(float *)b;
if (x == y) return 0;
else return (x>y)? -1: 1; (Note colon between -1 and 1.)
}
main()
{
data[0] = 8.0;
data[1] = 7.4;
data[2] = 8.6;
data[3] = 6.4;
data[4] = 20.0;
data[5] = 3.4;
data[6] = 2.3;
data[7] = 13.5;
data[8] = 7.7;
data[9] = 9.2;

qsort((void *)data, 10, sizeof(data[0]), compare);


(void)printf(&quot;The largest number is %f\n&quot;, data[0]);

return(0);
}
Hope this helps.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top