Classes/csci218/Labs/Lab8/lab8.c

259 lines
4.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Name: Garrett Haldrup
lab8.c
Problem: Has programs that convert C° to F°, and 7 different patterns and calculating mean and sd of users grades.
Certification of Authenticity:
I certify that this assignment is entirely my own work.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void temperatureChart();
void apporxSqrt();
void patternA(int n);
void patternB(int n);
void patternC(int n);
void patternD(int n);
void patternE(int n);
void patternF(int n);
void patternG(int n);
void hwMean();
void hwStats();
int main() {
temperatureChart();
//apporxSqrt();
patternA(8);
patternB(8);
patternC(8);
patternD(8);
patternE(8);
patternF(8);
patternG(8);
//hwMean();
hwStats();
return 0;
}
void temperatureChart() {
int c;
float f;
printf("This program will convert from °C to °F from 0 to 200 every 10°C\n");
for (c = 0; c <= 200; c += 10) {
f = (c * 1.8) + 32;
printf("%d°C\t= %.1f°F\n", c, f);
}
}
void apporxSqrt() {
int n, num;
float guess;
printf("This will estimate the square root of a number using newtons method\n");
printf("Enter number: ");
scanf("%d", &num);
printf("Enter n: ");
scanf("%d", &n);
guess = num / 2.0;
for (int i = 0; i < n; i++) {
guess = (guess + (num / guess)) / 2.0;
}
printf("%.1f\n", guess);
}
void patternA(int n) {
int num = 0;
int sum = 0;
printf("\nThis will show n values and sum of the pattern 2n\n");
num += 2;
sum += num;
printf("%d", num);
for (int i = 0; i < n; i++) {
num += 2;
sum += num;
printf(" + %d", num);
}
printf(" = %d\n", sum);
}
void patternB(int n) {
int num = 0;
printf("\nThis will show the values for the pattern (-1)^n*(2 + 4n)\n");
num = -6;
printf("%d", num);
for (int i = 1; i < n; i++) {
num = abs(num) + 4;
if (!(i % 2)) {
num *= -1;
}
printf(", %d", num);
}
printf("\n");
}
void patternC(int n) {
printf("\nThis will show the pattern of 4 * ((n + 1) %% 2)\n");
int num = 0;
printf("%d", num);
for (int i = 1; i < n; i++) {
num = 4 * (i % 2);
printf(", %d", num);
}
printf("\n");
}
void patternD(int n) {
int num = 1;
int sum = 0;
printf("\nThis will show the pattern and sum of 1 + 2n * ((n %% 2) - 1)\n");
sum += num;
printf("%d", num);
for (int i = 1; i < n; i++) {
num = abs(num + (2 * ((i) % 2))) * pow(-1, i);
if (i % 2) {
printf(" - %d", abs(num));
} else {
printf(" + %d", num);
}
sum += num;
}
printf(" = %d\n", sum);
}
void patternE(int n) {
printf("\nThis will show the pattern of 3^n\n");
int num = 3;
printf("%d", num);
for (int i = 1; i < n; i++) {
num = pow(3, i + 1);
printf(", %d", num);
}
printf("\n");
}
void patternF(int n) {
int top = 1;
int bot = 4;
printf("\nThis will show the pattern and product of (3^n-1)/2+2n\n");
float product = 1;
product *= (float)top / (float)bot;
printf("(%d/%d)", top, bot);
for (int i = 1; i < n; i++) {
top = pow(3, i);
bot += 2;
printf(" * (%d/%d)", top, bot);
product *= (float)top / (float)bot;
}
printf(" = %.1f\n", product);
}
void patternG(int n) {
int num = 9;
int sum = 9;
printf("\nThis will show the pattern and sum of (2 + n)^2\n");
printf("%d", num);
for (int i = 1; i < n; i++) {
num = pow(i + 3, 2);
sum += num;
printf(" + %d", num);
}
printf(" = %d\n", sum);
}
void hwMean() {
int n;
float grades[20];
float mean;
float sum = 0;
printf("\nThe program will show the mean of your inputed grades\n");
printf("Enter the number of assignments: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter your grade on HW%d: ", i + 1);
scanf("%f", &grades[i]);
sum += grades[i];
}
mean = sum / n;
printf("Your overall average is: %.1f\n", mean);
}
void hwStats() {
int n = 1;
float grades[20];
float mean;
float sum = 0;
float sd;
printf("\nThe program will show the mean, and standard deviation of your inputed grades\n");
while(1) {
printf("Enter the number of assignments: ");
scanf("%d", &n);
if (n <= 20 && n > 0) {
break;
} else {
printf("Must be less than or equal to 20!\n");
}
}
for (int i = 0; i < n; i++) {
printf("Enter your grade on HW%d: ", i + 1);
scanf("%f", &grades[i]);
sum += grades[i];
}
mean = sum / n;
printf("Your overall average is: %.1f\n", mean);
sum = 0;
for (int i = 0; i < n; i++) {
sum += pow(grades[i] - mean, 2);
}
sd = sqrt((1.0/n) * sum);
printf("The standard deviation(σ) is: %.1f\n", sd);
}