65 lines
1.9 KiB
C
65 lines
1.9 KiB
C
#include <stdio.h>
|
|
|
|
/* For the functions that return a product id, and its sales value, this struct will be a data type that holds both
|
|
* It is unnecessary, I could just print the product sales from the printf using sales[id-1]
|
|
* But I wanted some practice with structs so I did this
|
|
*/
|
|
struct sales_data {
|
|
int id; // The ID of the product
|
|
int sales; // Amount of sales of the product
|
|
};
|
|
|
|
//Question 2:
|
|
// Returns sum of all sales
|
|
int find_total_sales(const int sales[], int len) {
|
|
int total = 0;
|
|
for(int i = 0; i < len; i++) {
|
|
total += sales[i];
|
|
}
|
|
return total;
|
|
}
|
|
|
|
// Returns id and sales of product with most sales
|
|
struct sales_data find_highest(const int sales[], int len) {
|
|
// Initialize our return values with the first item in the array
|
|
struct sales_data result;
|
|
result.id = 1;
|
|
result.sales = sales[0];
|
|
// Loop and if something was more, it is our new return value
|
|
for(int i = 0; i < len; i++) {
|
|
if(sales[i] > result.sales) {
|
|
result.id = i+1;
|
|
result.sales = sales[i];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
// Returns id and sales of product with the least sales
|
|
struct sales_data find_lowest(const int sales[], int len) {
|
|
// Initialize our return values with the first item in the array
|
|
struct sales_data result;
|
|
result.id = 1;
|
|
result.sales = sales[0];
|
|
// Loop and if something was less, it is our new return value
|
|
for(int i = 0; i < len; i++) {
|
|
if(sales[i] < result.sales) {
|
|
result.id = i+1;
|
|
result.sales = sales[i];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int main() {
|
|
int sales[5] = {20,15,30,10,25};
|
|
|
|
printf("Total Sales: %d\n", find_total_sales(sales, 5));
|
|
struct sales_data highest_sales = find_highest(sales, 5);
|
|
printf("Product with Highest Sales: Product %d with %d units\n", highest_sales.id, highest_sales.sales);
|
|
struct sales_data lowest_sales = find_lowest(sales, 5);
|
|
printf("Product with Lowest Sales: Product %d with %d units\n", lowest_sales.id, lowest_sales.sales);
|
|
|
|
|
|
return 0;
|
|
}
|