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

Enter ticket prices in each of 3 cateogories: -- Enter group and fans in 3 categories
 Type a "." to finish entries:
        Concert   s1   s2   s3     Sales
              a    1    2    3    147.00
              b    3    3    1    127.00
              c    5    3    1    147.00
              d    3    3    5    251.00
              e    1    1    2     94.00
              f    9    4    3    271.00
              g    4    5    6    336.00

 --- Sorted ---
        Concert   s1   s2   s3     Sales
              e    1    1    2     94.00
              b    3    3    1    127.00
              c    5    3    1    147.00
              a    1    2    3    147.00
              d    3    3    5    251.00
              f    9    4    3    271.00
              g    4    5    6    336.00
... bye ...