6. Try using different inputs: a. What changes would you suggest
6. Try using different inputs: a. What changes would you suggest to
handle larger, more realistic numbers? b. What happens if any of the numbers, such as the ticket prices, are negative? c. What are your recommendations concerning negative input values?
// C code
// This code will compute the values of the sales ticket sales for concerts
// and sort the entries by those values
// Developer: XX
// Date: 3/28/3019
#include <stdio.h>
#define MAXN 100 // max characters in a group/concert name
#define MAXG 50 // max concerts/groups
#define MAXC 4 // max categories
char group [MAXG][MAXN];
int fans [MAXG][MAXC];
float prices [MAXC];
float sales [MAXG];
int count;
// outputs information on concert sales
void printArray () {
printf (“%25s%5s%5s%5s%5s%10s\n”,
“Concert”, “s1”, “s2”, “s3”, “s4″,”Sales”);
for (int i = 0; i < count; i++) {
printf (“%25s”, group [i]);
for (int j = 0; j < MAXC; j++) {
printf (“%5d”, fans[i][j]);
} // end for each category
printf (“%10.2f\n”, sales [i]);
} // end for each group
} // end function printArray
// calculates sales for each concert and stores the results
// in the sales array
void computeSales () {
for (int i = 0; i < count; i++) {
sales [i] = 0;
for (int j = 0; j < MAXC; j++) {
sales [i] += prices [j] * fans [i][j];
} // end for each category
} // end for each group
} // end function computeSales
// switches rows in the group, fans, and sales arrays. Used after the minimum
// in the sales array has been found
void switchRows (int m, int n) {
char tc;
int ti;
float v;
// printf (“Switching %d with %d\n”, m, n);
for (int i = 0; i < MAXN; i++) {
tc = group [m][i];
group [m][i] = group [n][i];
group [n][i] = tc;
} // end for each character in a group name
for (int i = 0; i < MAXC; i++) {
ti = fans [m][i];
fans [m][i] = fans [n][i];
fans [n][i] = ti;
} // end for each fan category
v = sales [m];
sales [m] = sales [n];
sales [n] = v;
} // end switch
// finds and returns the index of the minimum value in the portion of the
// sales array between the element with the index of the
// passed argument (m) and the end of the array
int findMinSales (int m) {
float min = sales [m];
int target = m;
for (int i = m+1; i < count; i++)
if (sales [i] < min) {
min = sales [i];
target = i;
} // end new max found
return target;
} // end function findMinSales
// sorts the sales array using a selection sort, then rearranges the group,
// fans, and sales arrays into order based on the sorted sales
//
// uses functions findMinSales and switchRows
void sortBySales () {
int target;
for (int i = 0; i < count; i++) {
target = findMinSales (i);
if (target > i)
switchRows (i, target);
} // for each concert
} // end function sortBySales
// gets data and fills the prices and group arrays.
// Also determines value of count
void getData () {
printf (“Enter ticket prices in each of %d cateogories: “, MAXC);
for (int i = 0; i < MAXC; i++)
scanf (“%f”, &prices [i]);
printf (“– Enter group and fans in %d categories\n”, MAXC);
printf (” . to finish entries:\n”);
count = 0;
scanf (“%s”, group[count]);
while ( ( count + 1 ) < MAXG && ( group [count][0] != ‘.’) ) {
for (int j = 0; j < MAXC; j++)
scanf (“%d”, &fans[count][j]);
count ++;
scanf (“%s”, group[count]);
} // end while for each group
} // end function getData
void welcome() {
printf(“Welcome! My name is Minerva Song.\n”);
printf(“This project is called ‘Week 8 – Concerts’ for CMIS 102 in Spring 2019.\n”);
}
void total_sum() {
int total_sales = 0;
for (int i = 0; i < count; i++) {
sales [i] = 0;
for (int j = 0; j < MAXC; j++) {
sales [i] += prices [j] * fans [i][j];
} // end for each category
total_sales = total_sales + sales[i];
}
printf(“The total sales for all the concerts is: %d\n”,total_sales);
}// end function total_sum
int main(void) {
welcome();
getData ();
computeSales ();
printArray ();
printf (“\n — Sorted —\n”);
sortBySales ();
printArray ();
total_sum();
printf(“… bye …\n”);
return 0;
} // main