All Files here
This commit is contained in:
47
Q1.c
Normal file
47
Q1.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Question 1:
|
||||
// Function to find maximum
|
||||
int find_maximu m(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
64
Q2.c
Normal 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;
|
||||
}
|
||||
70
Q3.c
Normal file
70
Q3.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <stdio.h>
|
||||
/* I know that the program would be overall simpler if I printed the sums in the function as they are
|
||||
* calculated, but I have always been taught to avoid printing within functions, so I went with the
|
||||
* returning an array of the sums method. It has the benefit of being able to use the sums later in the program
|
||||
* if necessary
|
||||
*/
|
||||
|
||||
// Function to calculate sum of each row of matrix. Takes
|
||||
void row_sums(int width, int height, int matrix[width][height], int return_array[]) {
|
||||
int row_sum;
|
||||
for(int i = 0; i < height; i++) {
|
||||
row_sum = 0;
|
||||
for(int j = 0; j < width; j++) {
|
||||
row_sum+=matrix[i][j];
|
||||
}
|
||||
return_array[i] = row_sum;
|
||||
}
|
||||
}
|
||||
// Function to calculate sum of each column of matrix. Takes
|
||||
void column_sums(int width, int height, int matrix[width][height], int return_array[]) {
|
||||
int column_sum;
|
||||
for(int i = 0; i < width; i++) {
|
||||
column_sum = 0;
|
||||
for(int j = 0; j < height; j++) {
|
||||
column_sum+=matrix[i][j];
|
||||
}
|
||||
return_array[i] = column_sum;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int width, height;
|
||||
printf("Enter width then enter height:\n");
|
||||
scanf("%d", &width);
|
||||
scanf("%d", &height);
|
||||
|
||||
int matrix[width][height];
|
||||
|
||||
for(int i = 0; i < width; i++) {
|
||||
for(int j = 0; j < height; j++) {
|
||||
printf("Enter matrix value %d,%d: ", i, j);
|
||||
scanf("%d", &matrix[i][j]);
|
||||
}
|
||||
}
|
||||
// Print the matrix
|
||||
for(int i = 0; i < width; i++) {
|
||||
for(int j = 0; j < height; j++) {
|
||||
printf("%d ", matrix[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int row_sum_values[height];
|
||||
row_sums(width, height, matrix, row_sum_values);
|
||||
printf("Row sum values are: ");
|
||||
for(int i = 0; i < width; i++) {
|
||||
printf("%d, ", row_sum_values[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
int column_sum_values[height];
|
||||
column_sums(width, height, matrix, column_sum_values);
|
||||
printf("Column sum values are: ");
|
||||
for(int i = 0; i < width; i++) {
|
||||
printf("%d, ", column_sum_values[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
56
Q4.c
Normal file
56
Q4.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
//Question 4
|
||||
double find_average(const int arr[], int len) {
|
||||
double sum = 0.0;
|
||||
for(int i = 0; i < len; i++) {
|
||||
sum += arr[i];
|
||||
}
|
||||
return sum/len;
|
||||
}
|
||||
// Returns the INDEX of the highest value in array
|
||||
int find_index_highest(const double arr[], int len) {
|
||||
double highest = arr[0];
|
||||
int index_highest = 0;
|
||||
|
||||
for(int i = 0; i < len; i++) {
|
||||
if(arr[i] > highest) {
|
||||
highest = arr[i];
|
||||
index_highest = i;
|
||||
}
|
||||
}
|
||||
return index_highest;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int num_students, num_subjects;
|
||||
printf("Enter number of students: ");
|
||||
scanf("%d", &num_students);
|
||||
printf("Enter number of subjects: ");
|
||||
scanf("%d", &num_subjects);
|
||||
|
||||
int grade_matrix[num_students][num_subjects];
|
||||
for(int i = 0; i < num_students; i++) {
|
||||
printf("Enter student %d's grades: ", i+1);
|
||||
for(int j = 0; j < num_subjects; j++) {
|
||||
scanf("%d", &grade_matrix[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
double average_grades[num_students];
|
||||
printf("Average Grades:\n");
|
||||
for(int i = 0; i < num_students; i++) {
|
||||
average_grades[i] = find_average(grade_matrix[i], num_subjects);
|
||||
printf("Student %d: %0.02f\n", i+1, average_grades[i]);
|
||||
}
|
||||
|
||||
printf("Student with Highest Average: Student %d\n", (find_index_highest(average_grades, num_students) +1));
|
||||
|
||||
printf("Students with average >= 75:\n");
|
||||
for(int i = 0; i < num_students; i++) {
|
||||
if(average_grades[i] >= 75.0)
|
||||
printf("Student %d\n", i+1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user