Added new lab and final project

This commit is contained in:
Haldrup-tech 2024-11-12 15:07:45 -05:00
parent 69cbdaed11
commit 952112be15
39 changed files with 494 additions and 11 deletions

BIN
.DS_Store vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
PHYS112/HaldrupHW4.pdf Normal file

Binary file not shown.

BIN
PHYS112/HaldrupHW6.pdf Normal file

Binary file not shown.

BIN
PHYS112/HaldrupRQ2.pdf Normal file

Binary file not shown.

BIN
PHYS112/Hw7Haldrup.pdf Normal file

Binary file not shown.

BIN
PHYS112/Lab 10.pdf Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1 +1 @@
Subproject commit 41c2aecf88dee9efe9896171d4d70dc89f0c4d75
Subproject commit a63285a8b51d61877433cc18100fbd584eb91bee

Binary file not shown.

View File

@ -1,3 +1,10 @@
/* Name: Garrett Haldrup
pi.c
Problem: Experiments with different methods of calculating pi, and sees how close they get to the real thing
Certification of Authenticity:
I certify that this assignment is entirely my own work.
*/
#include <stdio.h>
#include <math.h>
@ -10,9 +17,12 @@ void piCompare(double pi);
int main() {
int n = 10000;
int n;
double pi;
printf("Enter the number of n iterations to do\nfor each method of calculating pi: ");
scanf("%d", &n);
pi = piWallis(n);
piCompare(pi);
pi = piGregory(n);
@ -27,6 +37,9 @@ double piWallis(int n) {
int top = 0;
int bottom = 1;
double pi = 1;
printf("\nThis will calculate pi using the Wallis Method.\n");
for (int i = 0; i < n; i++) {
top += ((i + 1) % 2) * 2;
bottom += (i % 2) * 2;
@ -42,6 +55,8 @@ double piGregory(int n) {
int bottom = 1;
double pi = 0;
printf("\nThis will calculate pi using the Gregory Method.\n");
for (int i = 0; i < n; i++) {
bottom = (1 + (2 * i)) * pow(-1, i);
pi += (float)top / (float)bottom;
@ -55,6 +70,8 @@ double piNilakantha(int n) {
int bottom;
double pi = 3;
printf("\nThis will calculate pi using the Nilakantha Method.\n");
for (int i = 1; i < n; i++) {
bottom = (2*i) * (2*i+1) * (2*i+2) * pow(-1, i - 1);
pi += (float)top / (float) bottom;
@ -68,6 +85,8 @@ double piEuler(int n) {
int bottom;
double pi = 0;
printf("\nThis will calculate pi using the Euler Method.\n");
for (int i = 0; i < n; i++) {
bottom = pow(i+1,2);
pi += 1.0 / bottom;
@ -81,5 +100,5 @@ void piCompare(double pi) {
double diff;
diff = M_PI - pi;
printf("%lf\n", diff);
printf("The difference from the actual value of pi is: %lf\n", diff);
}

BIN
csci218/Labs/.DS_Store vendored

Binary file not shown.

BIN
csci218/Labs/Final Project/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,48 @@
#define outputA 2
#define outputB 3
#define buttonPin 8
int counter = 0;
int aState;
int aLastState;
int buttonState;
int lastButtonState;
void setup() {
// put your setup code here, to run once:
pinMode(outputA, INPUT);
pinMode(outputB, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
aLastState = digitalRead(outputA);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
aState = digitalRead(outputA);
if (aState != aLastState) {
if(digitalRead(outputB) != aState) {
counter++;
} else {
counter--;
}
Serial.print("Position: ");
Serial.println(counter);
}
if (!buttonState && buttonState != lastButtonState) {
counter /= 2;
Serial.print("Position: ");
Serial.println(counter);
}
lastButtonState = buttonState;
aLastState = aState;
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 MiB

View File

@ -0,0 +1,60 @@
// Define Pins & Delay
#define BLUE 4
#define GREEN 5
#define RED 6
#define DELAY 10
// define global variables
int redValue;
int greenValue;
int blueValue;
int redDir = -1;
int greenDir = 1;
int blueDir = 0;
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
redValue = 0;
greenValue = 0;
blueValue = 255;
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
Serial.begin(9600);
}
// main loop
void loop() {
if (redValue == 255) {
redDir = -1;
greenDir = 1;
blueDir = 0;
} else if (greenValue == 255) {
redDir = 0;
greenDir = -1;
blueDir = 1;
} else if (blueValue == 255) {
redDir = 1;
greenDir = 0;
blueDir = -1;
}
redValue += redDir;
greenValue += greenDir;
blueValue += blueDir;
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
delay(DELAY);
}

BIN
csci218/Labs/Lab10/a.out Executable file

Binary file not shown.

View File

@ -0,0 +1,27 @@
int buttonA = 2;
int buttonB = 3;
void setup(){
Serial.begin(9600);
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(10, OUTPUT); //Sum, Red LED
pinMode(12, OUTPUT); //Carryout, Yellow LED
}
void loop() {
int inputA = digitalRead(buttonA); // read voltage on pin
int inputB = digitalRead(buttonB);
if(inputA && inputB) {
digitalWrite(10, LOW);
digitalWrite(12, HIGH);
} else if (inputA || inputB) {
digitalWrite(10, HIGH);
digitalWrite(12, LOW);
} else {
digitalWrite(10, LOW);
digitalWrite(12, LOW);
}
}

View File

@ -0,0 +1,95 @@
/* Name: Garrett Haldrup
# lab10.c
# Purpose: Solves problems assigned in Lab 10
*/
#include <stdio.h>
#include <math.h>
#include <assert.h>
float distance(float x1, float y1, float x2, float y2);
float perimeterOfTriangle(float x1, float y1, float x2, float y2, float x3, float y3);
float circleArea(float radius);
char scoreToLetter(int score);
int main(){
float x1, y1, x2, y2, x3, y3;
x1 = 1, y1 = 2, x2 = 4, y2 = 6, x3 = 5, y3 = 3;
char list[] = {'F', 'D', 'E', 'C', 'B', 'A', 'N'};
int count = 0;
printf("\nTest cases for score to letter\n");
for (int i = 51; i <= 101; i += 10) {
char grade = scoreToLetter(i);
assert(grade = list[count]);
printf("The score of %d is letter grade: %c\n", i, scoreToLetter(i));
printf("Pass!---------------------------------------------------\n");
count++;
}
//test example
printf("\n\ntest distance(): \n");
float radius = distance(x1, y1, x2, y2);
assert(radius == 5.00);
printf("Pass!---------------------------------------------------\n");
printf("\n\ntest circleArea(): \n");
float area = circleArea(radius);
assert((int)area == 78);
printf("Pass!---------------------------------------------------\n");
printf("\n\ntest perimeterOfTriangle(): \n");
float perimeter = perimeterOfTriangle(x1, y1, x2, y2, x3, y3);
assert((int)perimeter == 12);
printf("Pass!---------------------------------------------------\n");
return 0;
}
char scoreToLetter(int score) {
switch (score) {
case 60 ... 69:
return 'D';
break;
case 70 ... 79:
return 'C';
break;
case 80 ... 89:
return 'B';
break;
case 90 ... 100:
return 'A';
break;
default:
if (score < 0 || score > 100) {
return 'N';
} else {
return 'F';
}
break;
}
}
float distance(float x1, float y1, float x2, float y2) {
float distance = sqrt(pow(x1 - x2, 2)+pow(y1 - y2, 2));
return distance;
}
float perimeterOfTriangle(float x1, float y1, float x2, float y2, float x3, float y3) {
float perimeter = distance(x1, y1, x2, y2) + distance(x2, y2, x3, y3) + distance(x3, y3, x1, y1);
return perimeter;
}
float circleArea(float radius) {
float area = M_PI * pow(radius, 2);
return area;
}

View File

@ -0,0 +1,30 @@
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 10; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Show the state of pushbutton on serial monitor
Serial.println(buttonState);
// check if the pushbutton is pressed.
//
if (buttonState == 1) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
// Added the delay so that we can see the output of button
delay(50);
}

View File

@ -0,0 +1,69 @@
// Define Pins & Delay
#define BLUE 4
#define GREEN 5
#define RED 6
// define global variables
int redValue;
int greenValue;
int blueValue;
int redDir = -1;
int greenDir = 1;
int blueDir = 0;
int de = 10;
int potPin = A0;
int potValue;
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
redValue = 0;
greenValue = 0;
blueValue = 255;
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
Serial.begin(9600);
}
// main loop
void loop() {
potValue = analogRead(potPin);
de = 1 + ((99.9 / 1023) * potValue);
Serial.println(de);
if (redValue == 255) {
redDir = -1;
greenDir = 1;
blueDir = 0;
} else if (greenValue == 255) {
redDir = 0;
greenDir = -1;
blueDir = 1;
} else if (blueValue == 255) {
redDir = 1;
greenDir = 0;
blueDir = -1;
}
redValue += redDir;
greenValue += greenDir;
blueValue += blueDir;
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
delay(de);
}

BIN
csci218/Labs/Lab11/a.out Executable file

Binary file not shown.

137
csci218/Labs/Lab11/lab11.c Normal file
View File

@ -0,0 +1,137 @@
/* Name: Garrett Haldrup
# lab11.c
# Purpose: Solves problems assigned in Lab 11
*/
#include <stdio.h>
#include <math.h>
#include <assert.h>
float findMax(float array[], int n);
float findMin(float array[], int n);
float getMean(float array[], int n);
float getStd(float array[], int n);
void raiseWages(float wages[], float raises[], int n);
void printArray(float array[], int n);
int main() {
printf("This program will calculate the max, min, mean, and std of a number set\n");
printf("It will also update a wages array through a function call\n\n");
float result;
float testFloats[] = {85, 93.3, 90.5};
float wages[] = {15.00, 14.75, 15.00, 19.50, 13.00};
float raises[] = { 1.00, 2.00, 1.50, 0.00, 1.50};
float newWages[] = {16, 16.75, 16.5, 19.5, 14.5};
int testFloatSize = sizeof(testFloats)/sizeof(testFloats[0]);
int wageSize = sizeof(wages)/sizeof(wages[0]);
result = findMax(testFloats, testFloatSize);
assert(result - 93.3 <= 0.001);
printf("Max of set is: %.1f\n", result);
printf("Pass!---------------------------------------------------\n");
result = findMin(testFloats, testFloatSize);
assert(result - 85.0 <= 0.001);
printf("Min of set is: %.1f\n", result);
printf("Pass!---------------------------------------------------\n");
result = getMean(testFloats, testFloatSize);
assert(result - 89.6 <= 0.001);
printf("Mean of set is: %.1f\n", result);
printf("Pass!---------------------------------------------------\n");
result = getStd(testFloats, testFloatSize);
assert(result - 3.4477 <= 0.001);
printf("Std of set is: %.1f\n", result);
printf("Pass!---------------------------------------------------\n");
printf("\n");
raiseWages(wages, raises, wageSize);
for (int i = 0; i < wageSize; i++) {
printf("wages[%d] = %.2f = %.2f\n", i, wages[i], newWages[i]);
assert(wages[i] - newWages[i] <= 0.000001);
printf("Pass!---------------------------------------------------\n");
}
printArray(wages, wageSize);
return 0;
}
float findMax(float array[], int n) {
float max = array[0];
for (int i = 1; i < n; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
float findMin(float array[], int n) {
float min = array[0];
for (int i = 1; i < n; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
float getMean(float array[], int n) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += array[i];
}
return sum / n;
}
float getStd(float array[], int n) {
float sum = 0;
float mean = getMean(array, n);
for (int i = 0; i < n; i++) {
sum += pow(array[i] - mean, 2);
}
return sqrt(sum / n);
}
void raiseWages(float wages[], float raises[], int n) {
for (int i = 0; i < n; i++) {
wages[i] += raises[i];
}
}
void printArray(float array[], int n) {
printf("{%.2f", array[0]);
for (int i = 1; i < n; i++) {
printf(", %.2f", array[i]);
}
printf("}\n");
}

View File

@ -30,7 +30,6 @@ int main() {
void triangle() {
int n = 10;
char symbol = '*';
int count = 1;
printf("\nThis program will show a trangle with width of n = 10 and symbol '*'.\n");
@ -43,7 +42,6 @@ void triangle() {
}
}
printf("\n");
count++;
}
}
@ -55,9 +53,9 @@ void multiplicationTable() {
printf("Enter n for a n x n multiplcaiton table: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d\t", (i + 1) * (j + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%d\t", i * j);
}
printf("\n");
}
@ -141,9 +139,9 @@ void triangleN(int n) {
void multiplicationTableN(int n) {
printf("\nThis program will make a %d x %d multiplication table.\n", n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d\t", (i + 1) * (j + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%d\t", i * j);
}
printf("\n");
}

Binary file not shown.