Added all previous files
This commit is contained in:
parent
4466ee8a4c
commit
f04fb2189a
59 changed files with 64921 additions and 0 deletions
61
lab4/grades.c
Normal file
61
lab4/grades.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundementals of Computing
|
||||
// Assignment: Lab 4, Part 1
|
||||
// Due: 29 September 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
float getAverage(int size, int grades[]);
|
||||
float getStDev(int size, float average, int grades[]);
|
||||
|
||||
int main() {
|
||||
float average;
|
||||
float stdev;
|
||||
int size;
|
||||
|
||||
int grades[] =
|
||||
{ 96,73,62,87,80,63,93,79,71,99,
|
||||
82,83,80,97,89,82,93,92,95,89,
|
||||
71,97,91,95,63,81,76,98,64,86,
|
||||
74,79,98,82,77,68,87,70,75,97,
|
||||
71,94,68,87,79 };
|
||||
|
||||
// Compute size, average, and standard deviation
|
||||
size = sizeof(grades) / sizeof(grades[0]);
|
||||
average = getAverage(size, grades);
|
||||
stdev = getStDev(size, average, grades);
|
||||
|
||||
// Print result
|
||||
printf("Number of grades: %d\n", size);
|
||||
printf("Average grade: %.2lf\n", average);
|
||||
printf("Standard deviation: %.2lf\n", stdev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
float getAverage(int size, int grades[]) {
|
||||
float average;
|
||||
float sum = 0;
|
||||
|
||||
// Loop through array to create sum
|
||||
for (int i = 0; i < size; i++)
|
||||
sum = sum + grades[i];
|
||||
|
||||
average = sum / size;
|
||||
return average;
|
||||
}
|
||||
|
||||
float getStDev(int size, float average, int grades[]) {
|
||||
float stdev;
|
||||
float sum = 0;
|
||||
|
||||
// Loop through array, create sum of squares of difference between grades and the mean
|
||||
for (int i = 0; i < size; i++) {
|
||||
sum = sum + pow(grades[i] - average, 2);
|
||||
}
|
||||
|
||||
stdev = sqrt(sum / (size - 1));
|
||||
|
||||
return stdev;
|
||||
}
|
||||
144
lab4/ndfootball.c
Normal file
144
lab4/ndfootball.c
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundamentals of Computing
|
||||
// Assignment: Lab 4, Part 3
|
||||
// Due: 1 October 2024
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int printMenu();;
|
||||
void statDisplay(int wins[], int losses[]);
|
||||
void winPercent(int size, int wins[], int losses[]);
|
||||
void graphWins(int wins[]);
|
||||
|
||||
int main() {
|
||||
int size, selection;
|
||||
|
||||
int wins[] =
|
||||
{ 6, 8, 6, 8, 5, 5, 6, 6, 8, 7, 4, 6,
|
||||
7, 7, 6, 7, 8, 6, 3, 9, 9, 10, 8, 9,
|
||||
10, 7, 9, 7, 5, 9, 10, 6, 6, 3, 6, 7,
|
||||
6, 6, 8, 7, 7, 8, 7, 9, 8, 7, 8, 9,
|
||||
9, 10, 4, 7, 7, 9, 9, 8, 2, 7, 6, 5,
|
||||
2, 5, 5, 2, 9, 7, 9, 8, 7, 8, 10, 8,
|
||||
8, 11, 10, 8, 9, 11, 9, 7, 9, 5, 6, 7,
|
||||
7, 5, 5, 8, 12, 12, 9, 10, 10, 11, 6, 9,
|
||||
8, 7, 9, 5, 9, 5, 10, 5, 6, 9, 10, 3,
|
||||
7, 6, 8, 8, 12, 9, 8, 10, 4, 10, 12, 11,
|
||||
10, 11, 9, 10 };
|
||||
|
||||
int losses[] =
|
||||
{ 3, 1, 2, 0, 3, 4, 1, 0, 1, 0, 1, 0,
|
||||
0, 0, 2, 1, 1, 1, 1, 0, 0, 1, 1, 1,
|
||||
0, 2, 1, 1, 4, 0, 0, 2, 2, 5, 3, 1,
|
||||
2, 2, 1, 2, 2, 0, 2, 1, 2, 2, 0, 0,
|
||||
0, 0, 4, 2, 2, 0, 1, 2, 8, 3, 4, 5,
|
||||
8, 5, 5, 7, 1, 2, 0, 2, 2, 2, 1, 2,
|
||||
3, 0, 2, 3, 3, 1, 3, 4, 2, 6, 4, 5,
|
||||
5, 6, 6, 4, 0, 1, 3, 3, 1, 1, 5, 3,
|
||||
3, 6, 3, 7, 3, 6, 3, 7, 6, 3, 3, 9,
|
||||
6, 6, 5, 5, 1, 4, 5, 3, 8, 3, 1, 2,
|
||||
2, 2, 4, 3 };
|
||||
|
||||
size = sizeof(wins) / sizeof(wins[0]);
|
||||
|
||||
// Print menu and get selection by calling function
|
||||
selection = printMenu();
|
||||
|
||||
// Call functions based on selection
|
||||
while (selection != 0) {
|
||||
switch (selection) {
|
||||
case 1:
|
||||
statDisplay(wins, losses);
|
||||
break;
|
||||
case 2:
|
||||
winPercent(size, wins, losses);
|
||||
break;
|
||||
case 3:
|
||||
graphWins(wins);
|
||||
break;
|
||||
default:
|
||||
printf("Invalid input. Try again.\n\n");
|
||||
}
|
||||
|
||||
selection = printMenu();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int printMenu() {
|
||||
int selection;
|
||||
|
||||
// Print options and get input
|
||||
printf("1) Print stats for a specific year\n");
|
||||
printf("2) Display years with at least n% wins\n");
|
||||
printf("3) Graph wins for range of years\n");
|
||||
printf("0) Exit\n");
|
||||
printf("Make selection: ");
|
||||
scanf("%d", &selection);
|
||||
|
||||
printf("\n");
|
||||
|
||||
return selection;
|
||||
}
|
||||
|
||||
void statDisplay(int wins[], int losses[]) {
|
||||
int year, index;
|
||||
float winPer, lossPer;
|
||||
printf("Enter year (1900-2023): ");
|
||||
scanf("%d", &year);
|
||||
|
||||
index = year - 1900;
|
||||
|
||||
// If no losses, wins = 100%, losses = 0%; otherwise, calculate percentages
|
||||
if (losses[index] == 0) {
|
||||
winPer = 100.00;
|
||||
lossPer = 0.00;
|
||||
}
|
||||
else {
|
||||
winPer = (float)wins[index] / (wins[index] + losses[index]) * 100;
|
||||
lossPer = 100 - winPer;
|
||||
}
|
||||
|
||||
// Print wins and losses with percentages
|
||||
printf("Wins: %d (%.2f%)\n", wins[index], winPer);
|
||||
printf("Losses: %d (%.2f%)\n\n", losses[index], lossPer);
|
||||
}
|
||||
|
||||
void winPercent(int size, int wins[], int losses[]) {
|
||||
float percent, tempPer;
|
||||
printf("Enter minimum win %: ");
|
||||
scanf("%f", &percent);
|
||||
|
||||
// Calculate win percentage for each year, display if over threshold
|
||||
for (int i = 0; i < size; i++) {
|
||||
tempPer = (float)wins[i] / (wins[i] + losses[i]) * 100;
|
||||
if (tempPer >= percent)
|
||||
printf("%d ", 1900 + i);
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
void graphWins(int wins[]) {
|
||||
int start, end;
|
||||
printf("Enter start year: ");
|
||||
scanf("%d", &start);
|
||||
printf("Enter end year: ");
|
||||
scanf("%d", &end);
|
||||
|
||||
// COnvert years to indexes
|
||||
start = start - 1900;
|
||||
end = end - 1900;
|
||||
|
||||
// Graph wins in user-specified range
|
||||
for (int i = start; i <= end; i++) {
|
||||
printf("%d ", 1900 + i);
|
||||
for (int j = 0; j < wins[i]; j++) {
|
||||
printf("#");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
59
lab4/primes.c
Normal file
59
lab4/primes.c
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundementals of Computing
|
||||
// Assignment: Lab 4, Part 2
|
||||
// Due: 29 September 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void findPrimes(int max, bool* isPrime);
|
||||
void printPrimes(int max, bool* isPrime);
|
||||
|
||||
int main() {
|
||||
int max = 1000;
|
||||
bool isPrime[max];
|
||||
|
||||
// Fill array with 1s
|
||||
for (int i = 0; i < 1000; i++)
|
||||
isPrime[i] = true;
|
||||
|
||||
// Set 0 and 1 to 0
|
||||
isPrime[0] = isPrime[1] = false;
|
||||
|
||||
// Call functions to find and print primes
|
||||
findPrimes(max, isPrime);
|
||||
printPrimes(max, isPrime);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void findPrimes(int max, bool* isPrime) {
|
||||
for (int i = 2; i <= max; i++) {
|
||||
// If number is prime, test all higher numbers
|
||||
if (isPrime[i] == true) {
|
||||
for (int j = i + 1; j <= max; j++) {
|
||||
// If i is factor of j, j is not prime
|
||||
if (j % i == 0)
|
||||
isPrime[j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printPrimes(int max, bool* isPrime) {
|
||||
int rowPrinted = 0;
|
||||
for (int i = 2; i <= max; i++) {
|
||||
if (isPrime[i] == true) {
|
||||
printf("%d ", i);
|
||||
rowPrinted++;
|
||||
}
|
||||
|
||||
// If the number of printed values is 10, start a new line
|
||||
if (rowPrinted == 10){
|
||||
printf("\n");
|
||||
rowPrinted = 0;
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
22
lab4/report.txt
Normal file
22
lab4/report.txt
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Author: Owen Dorweiler
|
||||
Class: Fundementals of Computing
|
||||
Assignment: Lab 4, Lab Report
|
||||
Due: 1 October 2024
|
||||
|
||||
NOTE: in lab 5, I was told that my choices are complicated enough that I can get full credit with the three instead of five.
|
||||
|
||||
From a user perspective, the program displays a menu of three options and asks the user to select one. If the user selects the first option, the program asks the user to input a year, and it displays the win, win percent, loss, and loss percent for that year. If the user selects option 2, the program asks for a win percentage theshold, and outputs only the years that meet or surpass that threshold. If the user selects option 3, the program asks for a start and end date, and prints a bar graph of wins for that range. After completing an option, the program prints the menu again and repeats until the user enters 0.
|
||||
|
||||
|
||||
Internally, the program calls the printMenu() function to print the menu and get the first user input. If the input is not 0, the program enters a while loop and calls various functions using a switch statement based on the input. If the input is invalid, it displays an error message.
|
||||
|
||||
- The first function called statDisplay() takes a year input and calculates and prints the wins, win percentage, loss, and loss percentage.
|
||||
|
||||
- The second function called winPercent() takes a threshold input and loops through the whole array, only printing the years that meet or surpass that threshold.
|
||||
|
||||
- The third function called graphWins() takes a start and end year input, and iterates through that range, printing a # for every win for that year.
|
||||
|
||||
At the end of the while loop, the program calls the printMenu() function again and repeats if the input is not 0.
|
||||
|
||||
|
||||
I verified the output of the program by manually comparing the printed values with what the arrays show, and by testing a variety of inputs for each selection. Although the program does not account for errors for every input, it prompts the user to enter a correct input, and the correct inputs yield accurate results.
|
||||
Loading…
Reference in a new issue