Monday, May 24, 2010

Currently have the following for my code, but im not sure how to sort. (4) please help write in C language?

write the following in C language:





1. dynamically create an array of 1000 doubles


2. read unspecified number of doubles from a binary file


3. reallocate the array to amount of items actually read


4. sort all values in descending order (high to low)





#include %26lt;stdio.h%26gt;





int main(int argc, char** argv)


{


FILE *fp;


double *ptr, *reptr;


int length;


ptr = (double *) malloc(1000* sizeof(double));


fp =fopen("yourfilepathandname","rb");





length = fread(ptrr, sizeof(double), 1000, fp);


close(fp);


(double *) realloc(ptr, length * sizeof(double);











sort(ptr);


return 0;


}

Currently have the following for my code, but im not sure how to sort. (4) please help write in C language?
hey geekguy, bad news dude you have a corrupt system pal because everything in your system is on C:\ driver so if you know how to debug and format your hard drive it will fix these problems. you might try hitting start,programs,accessaries and system tools and restore to before it became corrupted but chances are it won't help pc repair can help just tell him that your C:\ driver is become corrupt and you want him to do a debug and format your hard drive and he will think that you know a lot about pc repair lol sorry man. But what your files is doing is repeating themselves or buy a new hard drive but they are expensive so do as the geeks all do is give good advice tell him what your pc is doing and it need a debug and format good luck and keep computing.
Reply:So you want to sort an array. There are lots of algorithms to do that. They are different in speed, memory usage and ... . Some famous ones are: Bubble sort, QuickSort, ...


The most simple one is bubble sort. You get an array and start with the first number. You compare it with the next one and if it is smaller, you change their places and so on. Then the second item and so on to the last item in array.


The pseudo-code will be like this:





procedure bubbleSort( A : list of sortable items ) defined as:


do


swapped := false


for each i in 0 to length( A ) - 1 do:


if A[ i ] %26gt; A[ i + 1 ] then


swap( A[ i ], A[ i + 1 ] )


swapped := true


end if


end for


while swapped


end procedure





You can find the other algorithms with a little search!





good luck.

community survey

No comments:

Post a Comment