Commit 3a9fb83f by otsall

Replace homework2.c

parent d1b49423
Showing with 26 additions and 12 deletions
#include <stdio.h> #include <stdio.h>
//constant for the maximum number of carriers/destinations
#define MAXSIZE 15 #define MAXSIZE 15
//function prototypes
void fillMatrix(int flights[][MAXSIZE], int dest, int carrier); void fillMatrix(int flights[][MAXSIZE], int dest, int carrier);
void printMatrix(int flights[][MAXSIZE], int dest, int carrier); void printMatrix(int flights[][MAXSIZE], int dest, int carrier);
void totalFlights(int flights[][MAXSIZE], int sum[], int dest, int carrier); void totalFlights(int flights[][MAXSIZE], int sum[], int dest, int carrier);
void busiestCarrier(int sum[], int carrier); void busiestCarrier(int sum[], int carrier);
int main() { int main() {
int dest, int dest,
carrier; carrier;
//this is here because exciting the fillMatrix function otherwise changes the value of dest or carrier
int cartonBuffer[MAXSIZE];
//user inputs or file contains the number of destinations and carriers, which can only be positive and nonzero
puts("please input the number of destinations"); puts("please input the number of destinations");
scanf("%d", &dest); scanf("%d", &dest);
while(dest > MAXSIZE || dest < 1){ while(dest > MAXSIZE || dest < 1){
...@@ -24,9 +26,12 @@ int main() { ...@@ -24,9 +26,12 @@ int main() {
printf("the number of carriers has to be between 1 and %d, try again\n", MAXSIZE); printf("the number of carriers has to be between 1 and %d, try again\n", MAXSIZE);
scanf("%d", &carrier); scanf("%d", &carrier);
} }
//an empty array called flightPlan is created in addition to an array called sum which will be used later
int flightPlan[dest][carrier], int flightPlan[dest][carrier],
sum[carrier]; sum[carrier];
fillMatrix(flightPlan, dest, carrier); fillMatrix(flightPlan, dest, carrier);
printMatrix(flightPlan, dest, carrier); printMatrix(flightPlan, dest, carrier);
...@@ -38,6 +43,10 @@ int main() { ...@@ -38,6 +43,10 @@ int main() {
return 0; return 0;
} }
/* function takes in the empty array of flights and fills it with flights
* using a nested for loop based on user input or file.
* If the number of flights is negative, it asks for new input
*/
void fillMatrix(int flights[][MAXSIZE], int dest, int carrier){ void fillMatrix(int flights[][MAXSIZE], int dest, int carrier){
int i, int i,
j; j;
...@@ -51,31 +60,33 @@ void fillMatrix(int flights[][MAXSIZE], int dest, int carrier){ ...@@ -51,31 +60,33 @@ void fillMatrix(int flights[][MAXSIZE], int dest, int carrier){
scanf("%d", &flights[i][j]); scanf("%d", &flights[i][j]);
} }
} }
} }
return; return;
} }
/*function uses the filled array of flights and the number of
* destinations and carriers to print out the array in full
*/
void printMatrix(int flights[][MAXSIZE], int dest, int carrier){ void printMatrix(int flights[][MAXSIZE], int dest, int carrier){
int i, int i,
j; j;
puts("Flights : "); puts("Flights : ");
printf(" "); printf(" ");
for(i = 0; i < carrier; i++){ for(i = 0; i < carrier; i++){
printf("C%d, ", i+1); printf("C%-3d", i+1);
} }
printf("\n"); printf("\n");
for(i = 0; i < dest; i++){ for(i = 0; i < dest; i++){
printf("DESTINATION%d ", i+1); printf("DESTINATION%d ", i+1);
for(j = 0; j < carrier; j++){ for(j = 0; j < carrier; j++){
printf("%d ", flights[i][j]); printf("%-3d ", flights[i][j]);
} }
printf("\n"); printf("\n");
} }
return; return;
} }
/*the function takes the flightplan array and the numbers of destinations and carriers to
* fill the array "sum" with the sums of flights for each carrier and prints them out.
*/
void totalFlights(int flights[][MAXSIZE],int sum[], int dest, int carrier){ void totalFlights(int flights[][MAXSIZE],int sum[], int dest, int carrier){
int i, int i,
j, j,
...@@ -89,11 +100,14 @@ void totalFlights(int flights[][MAXSIZE],int sum[], int dest, int carrier){ ...@@ -89,11 +100,14 @@ void totalFlights(int flights[][MAXSIZE],int sum[], int dest, int carrier){
} }
printf(" TOTAL "); printf(" TOTAL ");
for(i = 0; i < carrier; i++){ for(i = 0; i < carrier; i++){
printf("%d ", sum[i]); printf("%-4d", sum[i]);
} }
return; return;
} }
/*the function uses the array of sums of flights and the carrier number to find the largest value
* in the array - the busiest carrier. This value is used to recheck the array for the same value to print out
* as the busiest carriers.
*/
void busiestCarrier(int sum[], int carrier){ void busiestCarrier(int sum[], int carrier){
int i, int i,
j = 0, j = 0,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment