fork(1) 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 4 // max categories
  6.  
  7.  
  8. char group [MAXG][MAXN];
  9. int fans [MAXG][MAXC];
  10. float prices [MAXC];
  11. float sales [MAXG];
  12. float total_sales = 0;
  13. int count = 0;
  14.  
  15. void printArray () {
  16.  
  17. printf ("%15s%5s%5s%5s%5s%10s\n",
  18. "Concert", "s1", "s2", "s3", "s4", "Sales");
  19.  
  20.  
  21. for (int i = 0; i < count; i++) {
  22. printf ("%15s", group [i]);
  23. for (int j = 0; j < MAXC; j++) {
  24. printf ("%5d", fans[i][j]);
  25. } // end for each category
  26. printf ("%10.2f\n", sales [i]);
  27. } // end for each group
  28. } // end function printArray
  29.  
  30. void computeSales () {
  31. for (int i = 0; i < count; i++) {
  32. sales [i] = 0;
  33. for (int j = 0; j < MAXC; j++) {
  34. sales [i] += prices [j] * fans [i][j];
  35. } // end for each category
  36. } // end for each group
  37. } // end function computeSales
  38.  
  39. void computeTotalSales () {
  40. for (int i = 0; i < count; i++) {
  41. total_sales+=sales[i];
  42. }//end for each group
  43.  
  44. }
  45.  
  46. void switchRows (int m, int n) {
  47. char tc;
  48. int ti;
  49. float v;
  50. // printf ("Switching %d with %d\n", m, n);
  51. for (int i = 0; i < MAXN; i++) {
  52. tc = group [m][i];
  53. group [m][i] = group [n][i];
  54. group [n][i] = tc;
  55. } // end for each character in a group name
  56. for (int i = 0; i < MAXC; i++) {
  57. ti = fans [m][i];
  58. fans [m][i] = fans [n][i];
  59. fans [n][i] = ti;
  60. } // end for each fan category
  61. v = sales [m];
  62. sales [m] = sales [n];
  63. sales [n] = v;
  64. } // end switch
  65.  
  66. int findMinSales (int m) {
  67. float min = sales [m];
  68. int target = m;
  69. for (int i = m+1; i < count; i++)
  70. if (sales [i] < min) {
  71. min = sales [i];
  72. target = i;
  73. } // end new max found
  74. return target;
  75. } // end function findMinSales
  76.  
  77. void sortBySales () {
  78. int target;
  79. for (int i = 0; i < count; i++) {
  80. target = findMinSales (i);
  81. if (target > i)
  82. switchRows (i, target);
  83. } // for each concert
  84. } // end function sortBySales
  85.  
  86. void getData () {
  87. // for (int i = 0; i < MAXG; i++) sales [i] = 0;
  88. printf ("Enter ticket prices in each of %d cateogories: ", MAXC);
  89. for (int i = 0; i < MAXC; i++)
  90. scanf ("%f", &prices [i]);
  91. printf ("-- Enter group and fans in %d categories\n", MAXC);
  92. printf (" . to finish entries:\n");
  93. for (int i = 0; i < MAXG; i++) {
  94. scanf ("%s", &group[i]);
  95. if (group [i][0] == '.')
  96. break;
  97. count++;
  98. for (int j = 0; j < MAXC; j++)
  99. scanf ("%d", &fans[i][j]);
  100. } // end for each group
  101. } // end function getData
  102.  
  103. int main(void) {
  104. getData ();
  105. computeSales ();
  106. printArray ();
  107. printf ("\n --- Sorted ---\n");
  108. sortBySales ();
  109. printArray ();
  110.  
  111. //********* calling function to compute total sales ,
  112. // **********then display the total sales *******************
  113. computeTotalSales ();
  114. printf("Total sales: %10.2f\n",total_sales);
  115. // *************************************************************
  116.  
  117. printf("... bye ...\n");
  118. return 0;
  119. }
Success #stdin #stdout 0s 4376KB
stdin
7 5 3 1
a 1 5 2 1
b 1 1 1 2
c 6 5 4 3
d 2 2 2 2
e 3 6 9 5
f 5 2 1 4
g 1 1 1 1
stdout
Enter ticket prices in each of 4 cateogories: -- Enter group and fans in 4 categories
 . to finish entries:
        Concert   s1   s2   s3   s4     Sales
              a    1    5    2    1     39.00
              b    1    1    1    2     17.00
              c    6    5    4    3     82.00
              d    2    2    2    2     32.00
              e    3    6    9    5     83.00
              f    5    2    1    4     52.00
              g    1    1    1    1     16.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00

 --- Sorted ---
        Concert   s1   s2   s3   s4     Sales
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
                   0    0    0    0      0.00
              g    1    1    1    1     16.00
              b    1    1    1    2     17.00
              d    2    2    2    2     32.00
              a    1    5    2    1     39.00
              f    5    2    1    4     52.00
              c    6    5    4    3     82.00
              e    3    6    9    5     83.00
Total sales:     321.00
... bye ...