This commit is contained in:
2023-04-02 15:52:26 +00:00
parent 3743b36625
commit bb134b96a6
5 changed files with 195 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
/*
HW8A
Nicholas Pease
Dot Matrix Operations
*/
#include <stdio.h>
#include <stdlib.h>
#include "matrix_print_functions.c"
int main() {
// input section
int firstR, firstC, secondR, secondC;
char useless;
printf("Enter number of rows and columns of the first matrix (format: r x c): ");
scanf("%d %c %d",&firstR, &useless, &firstC);
printf("Enter number of rows and columns of the second matrix (format: r x c): ");
scanf("%d %c %d",&secondR, &useless, &secondC);
// create matricies
int matrix1[firstR][firstC];
int matrix2[secondR][secondC];
// random values to matricies
for (int row = 0; row <= firstR; row++)
for (int column = 0; column <= firstC; column++)
matrix1[row][column] = 1 + rand() % 8;
for (int row = 0; row <= secondR; row++)
for (int column = 0; column <= secondC; column++)
matrix2[row][column] = 1 + rand() % 8;
// check condition before continuing
if (firstC == secondR) {
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]=matrix1[matrix1Row][matrix1Column] * matrix2[matrix1Column][matrix2Column];
}
}
}
matrix_print(firstR, firstC, matrix1, secondR, secondC, matrix2, firstR, secondC, matrix3);
} 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;
}
+54
View File
@@ -0,0 +1,54 @@
/*
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;
}
+84
View File
@@ -0,0 +1,84 @@
/*
HW8C
Nicholas Pease
Electricity Stats
*/
#include <stdio.h>
#include "electricity.h"
#include "matrix_print_functions.c"
/**
* @brief Calculate Average Usage for Each Calendar Month
* @retval float array of 12 items containing average of each month
*/
float* averageUsageMonthly() {
static float monthlyAverages[] = {0,0,0,0,0,0,0,0,0,0,0,0};
for(int row = 0; row < 80; row++) {
for (int column = 0; column < 12; column++) {
monthlyAverages[column] += electricity[row][column];
}
}
for (int i = 0; i < 12; i++) {
monthlyAverages[i] = monthlyAverages[i] / 80;
}
return monthlyAverages;
}
/**
* @brief Calculates months with lowest and highest average usage
* @param array of 12 items containing average consumption for all 12 calendar months
* @retval int array of 2 items; item 0 is month with min and item 1 is month with max
*/
int* consumptionMinMax(float electricityAverages[]) {
static int minmax[2];
minmax[0] = 0;
minmax[1] = 0;
float min = electricityAverages[0];
float max = electricityAverages[0];
for (int i = 0; i < 12; i++) {
if (electricityAverages[i] < min) {
minmax[0] = i;
} else if (electricityAverages[i] > max) {
minmax[1] = i;
}
}
return minmax;
}
/**
* @brief Calculates year with highest TOTAL consumption
* @retval int array of 2 items; item 0 is year with lowest and item 1 is year with most
*/
int* lowestHighestConsumption() {
static int lowestHighest[2];
lowestHighest[0] = 0;
lowestHighest[1] = 0;
float min = 0;
float max = 0;
for(int row = 0; row < 80; row++) {
int count = 0;
for (int column = 0; column < 12; column++) {
count+=electricity[row][column];
}
if (count < min) {
lowestHighest[0] = row;min = count;
}
if (count > max) {
lowestHighest[1] = row;max = count;
}
}
return lowestHighest;
}
int main() {
float* averages = averageUsageMonthly();
int* minmax = consumptionMinMax(averages);
int* lowhighyear = lowestHighestConsumption();
printf("Month with lowest average consumption: %d\n",minmax[0]);
printf("Month with highest average consumption: %d\n",minmax[1]);
printf("Year with highest consumption: %d\n",lowhighyear[0]);
printf("Year with lowest consumption: %d\n",lowhighyear[1]);
return 0;
}
Binary file not shown.
Executable
BIN
View File
Binary file not shown.