Added files from first programming course at OSU

This commit is contained in:
2019-07-09 18:14:25 -07:00
parent 4a591de966
commit 8c92e250d9
48 changed files with 2858 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main (void) {
printf("User:perrenc\nName:Corwin\nFavorite Color:Dark Neon Green\n");
return 0;
}

View File

@@ -0,0 +1,85 @@
/**
Change Machine Source File for Lab2 in CS 151
Made by Corwin Perren
Takes a money amount as an input and tells you the appropriate number of each bill and
coin less than or equal to $20 increments to make up total amount.
*/
#include <stdio.h> /** Include the stdio.h file to allow for printf() to be used*/
/** @brief Main Function
@param void This function does not accept any input variables
@return This function would return an error code to the OS if needed.
*/
int main (int argc, char **argv) {
/** Original money amount and all bill/coin variables. x is used for manipulation of
the original amount.
*/
double i = 00.00; /* initial input variable */
int twenties = 0; /* $20 variable */
int tens = 0; /* $10 variable */
int fives = 0; /* $5 variable */
int ones = 0; /* $1 variable */
int quarters = 0; /* $.25 variable */
int dimes = 0; /* $.1 variable */
int nickels = 0; /* $.05 variable */
int pennies = 0; /* $.01 variable */
int x = 0; /* used for the running total as the code progresses */
system("clear"); /** Clears the screen */
/** Prompts the user for a numerical input, stores it, multiplies it by 100, then
stores that value as an integer on x.
*/
printf("Please enter the amount of money to make change for: ");
scanf("%lf",&i);
i = i*100 ;
x = (int)i;
/** Takes the money amount, determines the number of each bill or coin, and subtracts
that number of bills from the total amount to calculate the next amount.
*/
twenties = x/2000;
x = x-twenties*2000;
tens = x/1000;
x = x-tens*1000;
fives = x/500;
x = x-fives*500;
ones = x/100;
x = x-ones*100;
quarters = x/25;
x = x-quarters*25;
dimes = x/10;
x = x-dimes*10;
nickels = x/5;
x = x-nickels*5;
pennies = x/1;
system("clear"); /** Clears the screen */
/** Print the original input amount, then the number of bills/coins needed only
if that number is not zero.
*/
printf("%.2lf - Original Amount\n\n", (i/100));
if (twenties != 0){
printf("%i - Twenties\n",twenties);
}if (tens != 0){
printf("%i - Tens\n",tens);
}if (fives != 0){
printf("%i - Fives\n",fives);
}if (ones != 0){
printf("%i - Ones\n",ones);
}if (quarters != 0){
printf("%i - Quarters\n",quarters);
}if (dimes != 0){
printf("%i - Dimes\n",dimes);
}if (nickels != 0){
printf("%i - Nickels\n",nickels);
}if (pennies != 0){
printf("%i - Pennies\n",pennies);
}
return (0);
}

View File

@@ -0,0 +1,50 @@
/** ECE 151 Lab 3 Source File
* Modified by Corwin Perren
*/
#include <stdio.h> /** included to allow for printf() to be used*/
#include <time.h> /** included to allow time() to be used*/
#include <stdlib.h> /** include to allow rand() and srand() to be used*/
/** @brief main Function
@param void This function does not accept any input variables
@return This function would return an error code to the OS if needed.
*/
int main(int argc, char **argv) {
srand(time(NULL)); /* seed the number generator */
int x = rand(); /* variable to hold our random integer */
int i = 0; /* varaible used for user input */
int guesses = 0; /* used to track number of user guesses */
/* keeps randomizing x until it reaches a value under 50 */
while (x > 50) {
x = rand();
}
/* clears the terminal and asks the user for an input */
system ("clear");
printf("What is your guess for the random number under 50?\n", x);
/* loops through asking the user for guesses until five guesses have been made */
while (guesses < 5){
scanf ("%d", &i); /* takes in user input */
if (i < x) {
/* prints if the user's value is less than x */
system("clear");
printf("Your guess is too low.\n");
}else if (i == x) {
/* prints if the user's value is x, then breaks */
system("clear");
printf("You guessed right!\n");
break;
}else if (i > x) {
/* prints if the user's value is larger than x */
system("clear");
printf("Your guess is too high.\n");
}
guesses = ++guesses; /* incriments the number of guesses the user has made */
}
}

View File

@@ -0,0 +1,73 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: lab3_extended.c
* Created: 2012 January 26, 13:42 by perrenc
* Last Modified: 2012 February 2, 01:31 by perrenc
*
* This program tries to guess a number the user has though of.
* It has five tries before erroring out.
*/
#include <stdio.h> /** included to allow for printf() to be used*/
#include <time.h> /** included to allow time() to be used*/
#include <stdlib.h> /** include to allow rand() and srand() to be used*/
/** @brief main Function
@param void This function does not accept any input variables
@return This function would return an error code to the OS if needed.
*/
int main (int argc, char **argv) {
srand(time(NULL)); /* seed the number generator */
int x = 0; /* variable to hold our random integer */
int i = 0; /* variable used for user input */
int guesses = 0; /* used to track number of user guesses */
int max = 51; /* used as a ceiling for guesses */
int min = 0; /* used as a floor for guesses */
/* Keeps randomizing x until it reaches a value under 50 */
x = rand()%51; /* assigns a value to x less than or equal to 50 that is random */
system("clear"); /* clears the screen */
/* Loops through asking the user for guesses until five guesses have been made */
while (guesses < 5){
/* Increments the number of guesses, asks the user about how close the computer is to
* guessing the number, then takes the users response, narrows its guess range by
* changing upper or lower limits, then randomly guesses a new value withing that range.
* After guessing right, it prints success, otherwise after five tries it prints that it
* lost.
*/
guesses = ++guesses;
printf("Is your number %d? Enter 1 for too low, 2 for correct, or 3 for too high: ", x);
scanf("%i", &i);
if(i == 1){
min = ++x; /* adjusts guess floor */
if(max == min){ /* is about to guess the right value
and avoids dividing by zero */
x = min;
continue;
}
x = rand()%(max-min) + min; /* used to make a guess within a range */
}else if (i == 2){
printf("Yay! I guessed right!\n");
break;
}else if (i == 3){
max = --x; /* adjusts guess ceiling */
if(max == min){ /* same as previous one */
x = min;
continue;
}
x = rand()%(max-min) + min; /* used to make a guess within a
range */
}else if (x != (3 | 2 | 1)){ /* prints an error if uncorrect values are
entered */
printf("You have entered an incorrect value, please try again.\n");
guesses -= 1;
}
}
if (i != 2){ /* prints that the computer lost */
printf("Awww, I lost.\n");
}
}

View File

@@ -0,0 +1,53 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: division.c
* Created: 2012 February 8, 16:58 by perrenc
* Last Modified: 2012 February 8, 23:15 by perrenc
*
* Has the user enter two integers, swaps them if the second one entered is larger than
* the first one, then divides the first by the second, first mod second, and the first
* by the second as doubles. It then prints out these values along with the equation.
*/
#include <stdio.h>
int main(int argc, char **argv) {
int userint1; //User input 1
int userint2; //User input 2
int swap; //Used for swapping input 1 and 2 if 2 is larger than 1
double double1; //Used for making a double of userint1
double double2; //Used for making a double of userint2
//Clears the screen, and asks the user to enter the two integers
system("clear");
printf("Please enter an integer: ");
scanf("%d", &userint1);
printf("Please enter a second integer: ");
scanf("%d", &userint2);
//Swaps userint1 and userint2 if userint2 is larger than userint1
if(userint2 > userint1){
swap = userint1;
userint1 = userint2;
userint2 = swap;
}
//Prints and error and ends the program if you try and divide by zero
if(userint2 == 0){
printf("\nYou cannot divide by zero, program ending...\n\n");
return(1);
}
//Prints userint1/userint2 and userint1 mod userint2
printf("\n%d / %d = %d\n", userint1, userint2, (userint1 / userint2));
printf("%d %% %d = %d\n", userint1, userint2, (userint1 % userint2));
//Converts userint1 and 2 to doubles
double1 = userint1;
double2 = userint2;
//Prints double1/double2
printf("%lf / %lf = %lf\n\n", double1, double2, (double1 / double2));
}

View File

@@ -0,0 +1,58 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: lab4_1.c
* Created: 2012 February 6, 15:10 by perrenc
* Last Modified: 2012 February 8, 23:00 by perrenc
*
* Asks the user for five floating point values, and prints them
* if all are entered correctly, else it ends the program.
*/
#include <stdio.h>
int main(int argc, char **argv) {
double f1 = -1.0; //First user float value
double f2 = -1.0; //Second user float value
double f3 = -1.0; //Third user float value
double f4 = -1.0; //Fourth user float value
double f5 = -1.0; //Fifth user float value
// Clears the screen, then explains what needs to be entered, and what will be printed.
system("clear");
printf("I am going to ask you to enter 5 different positive floating point numbers.\n");
printf("I will end the program as soon as you enter a negative value or a repeat number.\n");
printf("I will congratulate you and print out the 5 numbers if you successfully complete the task.\n\n");
/* Has the user input five values, and quits the program if an entered number is negative
* or if two numbers entered in a row are the same. If the values are entered correctly,
* it prints out the five values the user entered.
*/
printf("Please enter your first number. ");
scanf("%lf", &f1);
if(f1 < 0){
return(0);
}
printf("Please enter your second number. ");
scanf("%lf", &f2);
if((f2 == f1) | (f2 < 0)){
return(0);
}
printf("Please enter your third number. ");
scanf("%lf", &f3);
if((f3 == f2) | (f3 < 0)){
return(0);
}
printf("Please enter your fourth number. ");
scanf("%lf", &f4);
if((f4 == f3) | (f4 < 0)){
return(0);
}
printf("Please enter your fifth number. ");
scanf("%lf", &f5);
if((f5 == f4) | (f5 < 0)){
return(0);
}
printf("\nYour numbers were: %lf, %lf, %lf, %lf, %lf\n\n", f1, f2, f3, f4, f5);
}

View File

@@ -0,0 +1,43 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: lab4_2.c
* Created: 2012 February 8, 16:58 by perrenc
* Last Modified: 2012 February 8, 23:09 by perrenc
*
* Asks the users for a number between 1 and 20 and prints up to that number from one
* and down to one from that number.
* It errors if the value is not within this range.
*/
#include <stdio.h>
int main(int argc, char **argv) {
int userinput; //Variable for user input
int loop; //Used for looping
// Clears the screen and asks the user to input an integere between one and twenty
system("clear");
printf("Please enter an integer between 1 and 20: ");
scanf("%d", &userinput);
// Quits the program if an incorrect value has been entered
if((1 > userinput) | (userinput > 20)){
printf("You have entered an incorrect value, closing program...\n");
return(-1);
}
//Prints up from one to the user entered value
for(loop = 1 ; (loop <= userinput) ; ++loop){
printf("%d ", loop);
}
//Prints newline
printf("\n");
//Prints down to one from the user entered number
for(loop = userinput ; ((loop <= userinput) & (loop != 0)) ; loop--){
printf("%d ", loop);
}
//Prints a newline
printf("\n");
}

View File

@@ -0,0 +1,58 @@
/* Original Author: Corwin Perren (perrenc)
* File: lab4_4.c
* Created: 2012 February 08, 21:05 by perrenc
* Last modified: 2012 February 08, 23:23 by perrenc
*
* Purposefully used for making and correcting errors
*/
#include <stdio.h>
int main(int argc, char **argv) {
/*creates the user input and incrementing variables*/
int i;
int loop;
system("clear");
printf("Enter an integer between 1 and 20:\n");
scanf("%d", &i);
/*errors and exits if i is greater than 20 or less than one*/
if((1 > i) | (i > 20)){
printf("You have entered an incorrect value.\n");
return(-1);
}
/*prints ascending characters from one up to the user entered value*/
for (loop = 1; (loop <= i); ++loop){
printf("%d ", loop);
}
printf("\n");
/*prints descending characters from the user entered value down to 1*/
for(loop = i; ((loop <= i) & (loop != 0)); loop--){
printf("%d ", loop);
}
printf("\n");
}
/*error listings
* Removed semicolon at line 23: "expected ; before return"
* Commented out variable i on line 14: "i undeclared (first use in this function.
Each undeclared identifier is reported only once for each function it appears in."
* Removed braces from the following if function: "expected } before else"
int extra() {
if(x < 3) {
printf("x is less than 3.\n");
printf("you fart loudly.");
else
printf("x is not less than 3.\n");
printf("et cetera\n");
}
}
* Removed parentheses from line 27: "expected ( before loop
expected ; before { token"
* Passed wrong type to line 28: It compiled, but output was empty.
*/

View File

@@ -0,0 +1,47 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: flush.c
* Created: 2012 February 16, 03:11 by perrenc
* Last Modified: 2012 February 16, 03:11 by perrenc
*
* Has the user input some text, prints out the first character entered along with its
* ascii code and then reruns until it receives a "%" character.
*
*/
#include <stdio.h>
/* ------Begin Functions------ */
/*Has the user input text, uses getchar to store the first character, then loops through
* getchar until the buffer is clear when it receives a "\n". */
char getchar_and_flush(){
char foo; //Used for temp user input storage
int loop; //Used for looping to clear the buffer
printf("Please enter some text. Press enter to finish. To quit, type a \"%\" symbol. ");
foo = getchar ();
loop = foo;
while(loop != '\n'){
loop = getchar ();
}
return foo;
}
/* ------End Functions------ */
/* Initalizes foo, clears the screen, then calls getchar_and_flush until the return
* character is "%" */
int main(int argc, char **argv){
char foo; //Used to check the return value of getchar_and_flush for looping
system("clear");
while(foo != '%'){
foo = getchar_and_flush();
printf("\nThe first character was %c or %d in ASCII.\n\n", foo, foo);
}
return 0;
}

View File

@@ -0,0 +1,125 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: lab5.c
* Created: 2012 February 16, 00:22 by perrenc
* Last Modified: 2012 February 16, 00:22 by perrenc
*
* This program asks the user for two values, counts up to it from zero, down to zero
* from it, and bewteen the two values. It then asks whether the user would like to
* run the program again.
*/
#include <stdio.h>
/* ------Begin Functions------ */
/* Begin user input functions */
/* Asks the user for a positive integer and returns it */
int userinput1(void){
int a; // For temp user input storage
system("clear");
printf("Please enter your first positive integer: ");
scanf("%d", &a);
return a;
}
/* Asks the user for another positive integer and returns it */
int userinput2(void){
int a; // For temp user input storage
printf("Please enter your second positive integer: ");
scanf("%d", &a);
return a;
}
/* End user input functions */
/* Begin Math Functions */
/* Receives the first integer from the user and counts up from 0 to it with integers */
int count_up(int userin1){
int loop; //Used for loop incrementing
for(loop = 0 ; loop <= userin1 ; loop++){
printf("%d ", loop);
}
printf("\n");
}
/* Counts down to zero by integers from the first integer entered by the user */
int count_down(int userin1){
int loop; //Used for loop incrementing
for(loop = userin1 ; loop >= 0 ; loop--){
printf("%d ", loop);
}
printf("\n");
}
/* Counts from the smaller number up to the larger one based on those entered by
* the user */
int count_between_up(int userin1, int userin2){
int loop; //Used for loop incrementing
for(loop = userin1 ; loop <= userin2 ; loop++){
printf("%d ", loop);
}
printf("\n");
}
/* Counts down from the larger number to the smaller one from those entered by the user */
int count_between_down(int userin1, int userin2){
int loop; //Used for loop incrementing
for(loop = userin1 ; loop >= userin2 ; loop--){
printf("%d ", loop);
}
printf("\n");
}
/* End Math Functions */
/* Begin Generic Functions */
/* Assigns the user inputs to variables, counts up, then down, then counts up or down
* between the two entered number depending on which of the two is larger */
void task1(void){
int userin1 = userinput1 ();
int userin2 = userinput2 ();
count_up (userin1);
count_down (userin1);
if (userin1 < userin2){
count_between_up (userin1, userin2);
}else if (userin1 > userin2){
count_between_down (userin1, userin2);
}
}
/* Used for quitting or rerunning the program per user request */
int quittask1(void){
int endtask1;
printf("\nWould you like to run again? Type 0 for no or 1 for yes: ");
scanf("%d", &endtask1);
return endtask1;
}
/* ------End Functions------ */
/* Runs task 1 from above and determines whether the program should exit or rerun based
* on the return value from quittask1. To rerun, a goto is used since I could not get
* loops to work. */
int main(int argc, char **argv){
begintask1: ;
task1 ();
int endtask1 = quittask1 ();
if(endtask1 == 0){
//Break
}else if(endtask1 == 1){
goto begintask1;
}
return 0;
}