Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // finding the index of the biggest number;
- int index_of_max(float a[ ], int n)
- {
- int i, i_max = 0;
- for (i = 1; i < n; i++)
- {
- if (a[i] > a[i_max])
- {
- i_max = i;
- }
- }
- return i_max;
- }
- // swapping position of the current biggest number, by pointers;
- void swap(float *p1, float *p2)
- {
- float p3 = *p1;
- *p1 = *p2;
- *p2 = p3;
- }
- // calculates the final and future location of the current biggest number;
- void max_sort(float a[], int n)
- {
- int length;
- for (length = n ; length > 1; length--)
- {
- int i_max = index_of_max(a, length);
- swap(&a[length-1], &a[i_max]);
- }
- }
- // main function;
- int main()
- {
- FILE *fin;
- int num, i;
- float fnum[num-1];
- fin = fopen("read_it.txt", "rt");
- fflush(stdin);
- fscanf(fin, "%d", &num);
- // reading the numbers from a file;
- for (i = 0; i < num; i++)
- {
- fflush(stdin);
- fscanf(fin, "%f", &fnum[i]);
- printf("%d. %.2lf\n", i+1, fnum[i]);
- }
- // calling for the function, which will sort the received numbers;
- max_sort(fnum, num);
- putchar('\n');
- // reading the numbers on the screen, after they have been sorted;
- for (i = 0; i < num; i++)
- {
- printf("%d. %.2lf\n", i+1, fnum[i]);
- }
- fclose(fin);
- fflush(stdin);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement