fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void sortArray(float a[], float b[], char c[6][4], int size){
  6. int i, swap;
  7. float temp1, temp2;
  8. char temp3[4];
  9. int k = 0;
  10. do{
  11. swap = 0;
  12. for (i = 0; i < size - 1; i++){//basic sorting for loop
  13. if (a[i]>a[i + 1]){
  14. swap = 1;
  15. temp1 = a[i]; //temporarily stores value of array cell
  16. temp2 = b[i];
  17. for ( k=0; k < 4 ; k++ ) { //Copying the c[i] to temp
  18. temp3[k] = c[i][k];
  19. }
  20. a[i] = a[i + 1]; //swaps the cells
  21. b[i] = b[i + 1];
  22. for ( k=0; k < 4 ; k++ ) { //Copying the c[i+1] to c[i]
  23. c[i][k] = c[i+1][k];
  24. }
  25. a[i + 1] = temp1;//stores value in swapped cell
  26. b[i + 1] = temp2;
  27. for ( k=0; k< 4 ; k++ ) { //Copying the temp to c[i+1]
  28. c[i+1][k] = (char)temp3[k];
  29. }
  30. }
  31. }
  32. } while (swap);
  33.  
  34. }
  35.  
  36. int main()
  37. {
  38. float arr1[6] = { 4534, 4554, 4574, 3934, 4054, 4174 };
  39. float arr2[6] = { 97.5, 97.4, 97.6, 97.1, 97.2, 97.3 };
  40. char arr3[6][4] = { "m4w", "m5w", "m6w", "m1w", "m2w", "m3w" };
  41. int i;
  42. printf("Arrays before sorting:\n");
  43. for (i = 0; i != 6; i++)
  44. {
  45. printf("%f ", arr1[i]);
  46. printf("%f ", arr2[i]);
  47. printf("%s\n", arr3[i]);
  48. }
  49.  
  50. sortArray(arr1, arr2, &arr3, 6); ///this is where the sorting function is used
  51.  
  52. printf("\n\nArrays after sorting:\n");
  53.  
  54. for (i = 0; i != 6; i++)
  55. {
  56. printf("%f ", arr1[i]);
  57. printf("%f ", arr2[i]);
  58. printf("%s\n", arr3[i]);
  59. }
  60.  
  61.  
  62. system("pause");
  63. return 0;
  64. }
Success #stdin #stdout #stderr 0s 2292KB
stdin
Standard input is empty
stdout
Arrays before sorting:
4534.000000 97.500000 m4w
4554.000000 97.400002 m5w
4574.000000 97.599998 m6w
3934.000000 97.099998 m1w
4054.000000 97.199997 m2w
4174.000000 97.300003 m3w


Arrays after sorting:
3934.000000 97.099998 m1w
4054.000000 97.199997 m2w
4174.000000 97.300003 m3w
4534.000000 97.500000 m4w
4554.000000 97.400002 m5w
4574.000000 97.599998 m6w
stderr
sh: pause: not found