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,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.
*/