84 lines
2.4 KiB
C
84 lines
2.4 KiB
C
/*
|
|
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;
|
|
} |