Files
2023-04-02 15:52:26 +00:00

54 lines
2.1 KiB
C

/*
HW8B
Nicholas Pease
Dot Matrix Operations - Revised & Continued
*/
#include <stdio.h>
#include <stdlib.h>
#include "matrix_print_functions.c"
int main() {
int costInfo[1][3] = {3, 4, 5};
printf("Enter daily sales in the format: regular#special#premium\n");
int gasSales[3][3];
char useless;
// input and process directly into array
for (int i = 0; i < 3; i++) {
printf("Enter total gallons of gas sales on Day %d: ",i+1);
scanf("%d%c%d%c%d",&gasSales[i][0],&useless,&gasSales[i][1],&useless,&gasSales[i][2]);
}
int firstR = 1;int firstC = 3;int secondR = 3;int secondC = 3;
// check condition before continuing
if (firstC == secondR && useless == '#') {
int matrix3[firstR][secondC];
// each row of first matrix
// each column of second matrix
// each column of first matrix
// given that rows of second = columns of first
for (int matrix1Row = 0; matrix1Row < firstR; matrix1Row++) {
for (int matrix2Column = 0; matrix2Column < secondC; matrix2Column++) {
matrix3[matrix1Row][matrix2Column] = 0;
for (int matrix1Column = 0; matrix1Column < firstC; matrix1Column++) {
matrix3[matrix1Row][matrix2Column]+= costInfo[matrix1Row][matrix1Column] * gasSales[matrix1Column][matrix2Column];
}
}
}
matrix_print(firstR, firstC, costInfo, secondR, secondC, gasSales, firstR, secondC, matrix3);
int totalSales = 0;
for (int i = 0; i < 3; i++) {
printf("Total sales on Day %d: $%d\n",i+1,matrix3[0][i]);
totalSales+=matrix3[0][i];
}
printf("Total sales on weekend: $%d\n",totalSales);
} else if (useless != '#') {
printf("Error: please follow the correct input format\nPlease try again.\n")
} else {
// Unable to multiply
printf("Error: the %d x %d matrix can't be multiplied by the %d x %d matrix.\nReason: the number of columns of the 1st matrix must equal the number of rows of the 2nd matrix\n", firstR, firstC, secondR, secondC);
}
return 0;
}