Q2 is done

This commit is contained in:
2024-09-22 12:44:33 -05:00
parent da1644e7c8
commit 2976e1a77b

12
Q2.c
View File

@@ -1,15 +1,16 @@
#include <stdio.h> #include <stdio.h>
// Question 2
/* For the functions that return a product id, and its sales value, this struct will be a data type that holds both /* 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] * 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 * But I wanted some practice with structs, so I did this
*/ */
struct sales_data { struct sales_data { // For returning a product id and its sales
int id; // The ID of the product int id; // The ID of the product
int sales; // Amount of sales of the product int sales; // Amount of sales of the product
}; };
//Question 2:
// Returns sum of all sales // Returns sum of all sales
int find_total_sales(const int sales[], int len) { int find_total_sales(const int sales[], int len) {
int total = 0; int total = 0;
@@ -23,6 +24,7 @@ int find_total_sales(const int sales[], int len) {
struct sales_data find_highest(const int sales[], int len) { struct sales_data find_highest(const int sales[], int len) {
// Initialize our return values with the first item in the array // Initialize our return values with the first item in the array
struct sales_data result; struct sales_data result;
// Default to values of first in array
result.id = 1; result.id = 1;
result.sales = sales[0]; result.sales = sales[0];
// Loop and if something was more, it is our new return value // Loop and if something was more, it is our new return value
@@ -34,6 +36,7 @@ struct sales_data find_highest(const int sales[], int len) {
} }
return result; return result;
} }
// Returns id and sales of product with the least sales // Returns id and sales of product with the least sales
struct sales_data find_lowest(const int sales[], int len) { struct sales_data find_lowest(const int sales[], int len) {
// Initialize our return values with the first item in the array // Initialize our return values with the first item in the array
@@ -52,13 +55,12 @@ struct sales_data find_lowest(const int sales[], int len) {
int main() { int main() {
int sales[5] = {20,15,30,10,25}; int sales[5] = {20,15,30,10,25};
// Test functions
printf("Total Sales: %d\n", find_total_sales(sales, 5)); printf("Total Sales: %d\n", find_total_sales(sales, 5));
struct sales_data highest_sales = find_highest(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); 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); 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); printf("Product with Lowest Sales: Product %d with %d units\n", lowest_sales.id, lowest_sales.sales);
return 0; return 0;
} }