fork download
  1. /* C code
  2. This code will compute the values of the sales ticket sales for concerts
  3. and sort the entries by those values
  4. Developer: Sergio Arispe CMIS102
  5. Date: 10/13/2019 */
  6.  
  7. #include <stdio.h>
  8. #define MAXN 100 // max characters in a group/concert name
  9. #define MAXG 50 // max concerts/groups
  10. #define MAXC 4 // max categories
  11.  
  12. char group [MAXG][MAXN];
  13. int fans [MAXG][MAXC];
  14. float prices [MAXC];
  15. float sales [MAXG];
  16. int count = 0;
  17.  
  18. void printArray () {
  19. printf ("%15s%5s%5s%5s%5s%10s\n","Concert", "s1", "s2", "s3", "s4", "Sales");
  20. for (int i = 0; i < count; i++) {
  21. printf ("%15s", group [i]);
  22. for (int j = 0; j < MAXC; j++) {
  23. printf ("%5d", fans[i][j]);
  24. } // end for each category
  25. printf ("%10.2f\n", sales [i]);
  26. } // end for each group
  27. } // end function printArray
  28.  
  29. void computeSales () {
  30. for (int i = 0; i < count; i++) {
  31. sales [i] = 0;
  32. for (int j = 0; j < MAXC; j++) {
  33. sales [i] += prices [j] * fans [i][j];
  34. } // end for each category
  35. } // end for each group
  36. } // end function computeSales
  37.  
  38. void switchRows (int m, int n) {
  39. char tc;
  40. int ti;
  41. float v;
  42. // printf ("Switching %d with %d\n", m, n);
  43. for (int i = 0; i < MAXN; i++) {
  44. tc = group [m][i];
  45. group [m][i] = group [n][i];
  46. group [n][i] = tc;
  47. } // end for each character in a group name
  48. for (int i = 0; i < MAXC; i++) {
  49. ti = fans [m][i];
  50. fans [m][i] = fans [n][i];
  51. fans [n][i] = ti;
  52. } // end for each fan category
  53. v = sales [m];
  54. sales [m] = sales [n];
  55. sales [n] = v;
  56. } // end switch
  57.  
  58. int findMinSales (int m) {
  59. float min = fans [m][0];
  60. int target = m;
  61. for (int i = m+1; i < count; i++)
  62. if (fans [i][0] < min) {
  63. min = fans [i][0];
  64. target = i;
  65. } // end new max found
  66. return target;
  67. } // end function findMinSales
  68.  
  69. void sortBySales () {
  70. int target;
  71. for (int i = 0; i < count; i++) {
  72. target = findMinSales (i);
  73. if (target > i)
  74. switchRows (i, target);
  75. } // for each concert
  76. } // end function sortBySales
  77.  
  78. void getData () {
  79. // for (int i = 0; i < MAXG; i++) sales [i] = 0;
  80. printf ("Enter ticket prices in each of %d cateogories: ", MAXC);
  81. for (int i = 0; i < MAXC; i++)
  82. scanf ("%f", &prices [i]);
  83. printf ("-- Enter group and fans in %d categories\n", MAXC);
  84. printf (" . to finish entries:\n");
  85. for (int i = 0; i < MAXG; i++) {
  86. scanf ("%s", &group[i]);
  87. if (group [i][0] == '.')
  88. break;
  89. count++;
  90. for (int j = 0; j < MAXC; j++)
  91. scanf ("%d", &fans[i][j]);
  92. } // end for each group
  93. } // end function getData
  94.  
  95. void printWelcome(){
  96. printf("Hello there!\nMy name is Sergio Arispe\nAnd this program will compute the concert data you input shortly...\n");
  97. }
  98. void totalSales(){
  99. double totalSales =0;
  100. for(int i=0;i<count;i++){
  101. totalSales += sales[i];
  102. }
  103. printf("The total sales made were: %f\n",totalSales);
  104. }
  105.  
  106. int main(void) {
  107. printWelcome();
  108. getData ();
  109. computeSales ();
  110. printArray ();
  111. printf ("\n --- Sorted ---\n");
  112. sortBySales ();
  113. printArray ();
  114. totalSales();
  115. printf("... bye ...\n");
  116. return 0;
  117. }
Success #stdin #stdout 0s 4484KB
stdin
1 1 1 1
Maroon_5 7 30 30 30
Gorillaz 2 20 20 20 
Daft_punk 5 10 10 10.

stdout
Hello there!
My name is Sergio Arispe
And this program will compute the concert data you input shortly...
Enter ticket prices in each of 4 cateogories: -- Enter group and fans in 4 categories
 . to finish entries:
        Concert   s1   s2   s3   s4     Sales
       Maroon_5    7   30   30   30     97.00
       Gorillaz    2   20   20   20     62.00
      Daft_punk    5   10   10   10     35.00

 --- Sorted ---
        Concert   s1   s2   s3   s4     Sales
       Gorillaz    2   20   20   20     62.00
      Daft_punk    5   10   10   10     35.00
       Maroon_5    7   30   30   30     97.00
The total sales made were: 194.000000
... bye ...