First commit

This commit is contained in:
2024-09-16 23:58:32 -05:00
commit 239d6a3459
2 changed files with 111 additions and 0 deletions

47
Q1.c Normal file
View File

@@ -0,0 +1,47 @@
#include <stdio.h>
// Question 1:
// Function to find maximum
int find_maximum(const int arr[], int len) {
int max = arr[0];
for(int i = 0; i < len; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
return max;
}
// Function to find minimum
int find_minimum(const int arr[], int len) {
int min = arr[0];
for(int i = 0; i < len; i++) {
if(arr[i] < min) {
min = arr[i];
}
}
return min;
}
// Function to find amount temperatures above given threshold
int above_threshold(const int arr[], int len, int threshold) {
int count = 0;
for(int i = 0; i < len; i++) {
if(arr[i] > threshold)
count++;
}
return count;
}
// Passed arrays are const because they shouldn't be modified in the functions
int main() {
// The array of temps, and threshold value to be used to test
int temps[7] = {32,35,30,28,40,33,29};
int threshold = 32;
printf("Maximum Temperature: %d\n", find_maximum(temps, 7)); // Max test
printf("Minimum Temperature: %d\n", find_minimum(temps, 7)); // Min test
printf("Days above Threshold (%d): %d", threshold, above_threshold(temps, 7, threshold)); // Threshold test
return 0;
}

64
Q2.c Normal file
View File

@@ -0,0 +1,64 @@
#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;
}