fork download
  1. #include <stdio.h>
  2.  
  3. #define MAXN 100 // max characters in a group/concert name
  4. #define MAXG 50 // max concerts/groups
  5. #define MAXC 3 // max categories
  6.  
  7. char group [MAXG][MAXN];
  8. int fans [MAXG][MAXC];
  9. float prices [MAXC];
  10. float sales [MAXG];
  11. int count = 0;
  12.  
  13. void printArray () {
  14. printf ("%15s%5s%5s%5s%10s\n",
  15. "Concert", "s1", "s2", "s3", "Sales");
  16. for (int i = 0; i < count; i++) {
  17. printf ("%15s", group [i]);
  18. for (int j = 0; j < MAXC; j++) {
  19. printf ("%5d", fans[i][j]);
  20. } // end for each category
  21. printf ("%10.2f\n", sales [i]);
  22. } // end for each group
  23. } // end function printArray
  24.  
  25. void computeSales () {
  26. for (int i = 0; i < count; i++) {
  27. sales [i] = 0;
  28. for (int j = 0; j < MAXC; j++) {
  29. sales [i] += prices [j] * fans [i][j];
  30. } // end for each category
  31. } // end for each group
  32. } // end function computeSales
  33.  
  34. void switchRows (int m, int n) {
  35. char tc;
  36. int ti;
  37. float v;
  38. // printf ("Switching %d with %d\n", m, n);
  39. for (int i = 0; i < MAXN; i++) {
  40. tc = group [m][i];
  41. group [m][i] = group [n][i];
  42. group [n][i] = tc;
  43. } // end for each character in a group name
  44. for (int i = 0; i < MAXC; i++) {
  45. ti = fans [m][i];
  46. fans [m][i] = fans [n][i];
  47. fans [n][i] = ti;
  48. } // end for each fan category
  49. v = sales [m];
  50. sales [m] = sales [n];
  51. sales [n] = v;
  52. } // end switch
  53.  
  54. int findMinSales (int m) {
  55. float min = sales [m];
  56. int target = m;
  57. for (int i = m+1; i < count; i++)
  58. if (sales [i] < min) {
  59. min = sales [i];
  60. target = i;
  61. } // end new max found
  62. return target;
  63. } // end function findMinSales
  64.  
  65. void sortBySales () {
  66. int target;
  67. for (int i = 0; i < count; i++) {
  68. target = findMinSales (i);
  69. if (target > i)
  70. switchRows (i, target);
  71. } // for each concert
  72. } // end function sortBySales
  73.  
  74. void getData () {
  75. // for (int i = 0; i < MAXG; i++) sales [i] = 0;
  76. printf ("Enter ticket prices in each of %d cateogories: ", MAXC);
  77. for (int i = 0; i < MAXC; i++)
  78. scanf ("%f", &prices [i]);
  79. printf ("-- Enter group and fans in %d categories\n", MAXC);
  80. printf (" . to finish entries:\n");
  81. for (int i = 0; i < MAXG; i++) {
  82. scanf ("%s", group[i]);
  83. if (group [i][0] == '.')
  84. break;
  85. count++;
  86. for (int j = 0; j < MAXC; j++)
  87. scanf ("%d", &fans[i][j]);
  88. } // end for each group
  89. } // end function getData
  90.  
  91. int main(void) {
  92. getData ();
  93. computeSales ();
  94. printArray ();
  95. printf ("\n --- Sorted ---\n");
  96. sortBySales ();
  97. printArray ();
  98. printf("... bye ...\n");
  99. return 0;
  100. }
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
Enter ticket prices in each of 3 cateogories: -- Enter group and fans in 3 categories
   . to finish entries:
        Concert   s1   s2   s3     Sales
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00

 --- Sorted ---
        Concert   s1   s2   s3     Sales
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
                   0    0    0      0.00
... bye ...