100k views
4 votes
5. Create a variety of test cases focusing on the sorting algorithm, such as the final element is the smallest, the entire set is already sorted, etc. Explain the purpose of each test case, and check your code against each of those cases

User Guelfey
by
5.3k points

1 Answer

6 votes

Answer:

// C code

// This code will compute the values of the sales ticket sales for concerts

#include <stdio.h>

#define MAXN 100 // max characters in a group/concert name

#define MAXG 50 // max concerts/groups

#define MAXC 3 //max categories

char group [MAXG][MAXN];

int fans [MAXG][MAXC];

float prices [MAXC];

float sales [MAXG];

int count = 0;

void printArray () {

printf ("%15s%5s%5s%5s%10s\\",

"Concert", "s1", "s2", "s3","Sales");

for (int i = 0; i < count; i++) {

printf ("%15s", group [i]);

for (int j = 0; j < MAXC; j++) {

printf ("%5d", fans[i][j]);

} // end for each category

printf ("%10.2f\\", sales [i]);

} // end for each group

} // end function printArray

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

// ***** totalSales ****

void totalSales()

{

float tsales;

for(int i = 0; i <= MAXC; i++){

tsales += sales [i];

}

printf("\\\t\tThe total sales are: $%.2f\\",tsales);

}

// ***********************************

void switchRows (int m, int n) {

char tc;

int ti;

float v;

// printf ("Switching %d with %d\\", 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

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

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

//**********sort by fans*************

int minFans (int b) {

float min = fans [b][0];

int target = b;

for (int i = b; i < count; i++)

if (fans [i][0] < min) {

min = fans [i][0];

target = i;

} // end new max found

return target;

}

void sortByFans () {

int target = 0;

//int result;

for (int i = 0; i < count; i++) {

target = minFans(i);

if (target > i)

//result = target;

switchRows (i,target);

}

}

//********************************

void getData () {

// for (int i = 0; i < MAXG; i++) sales [i] = 0;

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\\", MAXC);

printf (" . to finish entries:\\");

for (int i = 0; i < MAXG; i++) {

scanf ("%s", group[i]);

if (group [i][0] == '.')

break;

count++;

for (int j = 0; j < MAXC; j++)

scanf ("%d", &fans[i][j]);

} // end for each group

} // end function getData

int main(void) {

getData ();

computeSales ();

printArray ();

printf ("\\ --- Sorted sales ---\\");

sortBySales ();

printArray ();

totalSales ();

printf("--- sorted by fans ---\\");

sortByFans ();

printArray ();

totalSales ();

printf("\t\t ... Bye ...\\");

return 0;

}

User GiorgosK
by
5.3k points