-
Notifications
You must be signed in to change notification settings - Fork 0
/
program_1.c
117 lines (96 loc) · 3.13 KB
/
program_1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#define NUM_MONTHS 12
void salesReport(double sales[]) {
printf("Monthly sales report for 2022:\n");
printf("Month Sales\n");
char *months[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
for (int i = 0; i < NUM_MONTHS; i++) {
printf("%-10s $%.2f\n", months[i], sales[i]);
}
}
void salesSummary(double sales[]) {
double minS = sales[0];
double maxS = sales[0];
double totalS = sales[0];
for (int i = 1; i < NUM_MONTHS; i++) {
if (sales[i] < minS) {
minS = sales[i];
}
if (sales[i] > maxS) {
maxS = sales[i];
}
totalS += sales[i];
}
double avgS = totalS / NUM_MONTHS;
printf("\nSales summary:\n");
printf("Minimum sales: $%.2f (January)\n", minS);
printf("Maximum sales: $%.2f (December)\n", maxS);
printf("Average sales: $%.2f\n", avgS);
}
void calcMovingAverages(double sales[]) {
printf("\nSix-Month Moving Average Report:\n");
char *months[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
for (int i = 0; i < NUM_MONTHS - 5; i++) {
double sum = 0;
for (int j = 0; j < 6; j++) {
sum += sales[i + j];
}
// Use a fixed width for month and range columns
printf("%-10s - %-10s $%.2f\n", months[i], months[i + 5], sum / 6);
}
}
void salesReportDescending(double sales[]) {
printf("\nSales Report (Highest to Lowest):\n");
printf("Month Sales\n");
char *months[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
// Create a copy of the sales array and its corresponding months for sorting
double sortedS[NUM_MONTHS];
char *sortedM[NUM_MONTHS];
for (int i = 0; i < NUM_MONTHS; i++) {
sortedS[i] = sales[i];
sortedM[i] = months[i];
}
// Sort the sales data and corresponding months in descending order
for (int i = 0; i < NUM_MONTHS - 1; i++) {
for (int j = i + 1; j < NUM_MONTHS; j++) {
if (sortedS[i] < sortedS[j]) {
// Swap sales
double tempS = sortedS[i];
sortedS[i] = sortedS[j];
sortedS[j] = tempS;
// Swap months
char *tempM = sortedM[i];
sortedM[i] = sortedM[j];
sortedM[j] = tempM;
}
}
}
// Print the sorted sales report
for (int i = 0; i < NUM_MONTHS; i++) {
printf("%-12s $%.2f\n", sortedM[i], sortedS[i]);
}
}
int main() {
double sales[] = {
23458.01,
40112.00,
56011.85,
37820.88,
37904.67,
60200.22,
72400.31,
56210.89,
67230.84,
68233.12,
80950.34,
95225.22
};
salesReport(sales);
salesSummary(sales);
calcMovingAverages(sales);
salesReportDescending(sales);
return 0;
}