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,56 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: perrenc_hw0_1.c
* Created: Unknown by perrenc
* Last Modified: 2012 February 21,20:33 by perrenc
*
* Prints out integers and decimals with specific numbers of decimal points.
*
*/
#include <stdio.h>
int main (int argc, char **argv) {
/*
* Creates integers and float variables, then assigns a computed value to them
*/
int a = (2 * 3);
int b = (1 / 5);
int c = (1 / 2);
int d = (3 / 2);
int e = (1 / 3);
int f = (2 / 3);
double g = (1.0 / 5);
double h = (1.0 / 2);
double i = (3.0 / 2);
double j = (1.0 / 3);
double k = (2.0 / 3);
double l = (1 / 5.0);
double m = (1 / 2.0);
double n = (3 / 2.0);
double o = (1 / 3.0);
double p = (2 / 3.0);
/*
* Prints the integers with no decimal points and then prints the float values
* with 6 and 20 decimal points
*/
printf("a = %i\n", a);
printf("b = %i\n", b);
printf("c = %i\n", c);
printf("d = %i\n", d);
printf("e = %i\n", e);
printf("f = %i\n", f);
printf("g = %.06f\n %.20f\n", g, g);
printf("h = %.06f\n %.20f\n", h, h);
printf("i = %.06f\n %.20f\n", i, i);
printf("j = %.06f\n %.20f\n", j, j);
printf("k = %.06f\n %.20f\n", k, k);
printf("l = %.06f\n %.20f\n", l, l);
printf("m = %.06f\n %.20f\n", m, m);
printf("n = %.06f\n %.20f\n", n, n);
printf("o = %.06f\n %.20f\n", o, o);
printf("p = %.06f\n %.20f\n", p, p);
}

View File

@@ -0,0 +1,35 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: perrenc_hw0_2.c
* Created: Unknown by perrenc
* Last Modified: 2012 February 21, 20:36 by perrenc
*
* Uses printf to print integers the same way through two different methods.
*
*/
#include <stdio.h>
int main (int argc, char **argv) {
/*
* Creates variables for holding integers between and including 1 to 4 assigns
* one of these values to each variable.
*/
int a = 1;
int b = 2;
int c = 3;
int d = 4;
/* Prints the four integer values on one line using a single printf statement. */
printf("%d, %d, %d, %d\n\n", a, b, c, d);
/*
* Prints the four integer values on the same line using four different printf
* statements.
*/
printf("%d, ", a);
printf("%d, ", b);
printf("%d, ", c);
printf("%d\n", d);
}

View File

@@ -0,0 +1,29 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: perrenc_hw0_4.c
* Created: Unknown by perrenc
* Last Modified: 2012 February 21, 20:41 by perrenc
*
* Prints out integers from 10 down by using a for loop and if statement.
*
*/
#include <stdio.h>
int main (int argc, char **argv) {
/* Creates a variable for manipulation and sets it to an inital value of 10. */
int x = 10;
/*
* Takes x and prints its value and a comma from 10 to 2, then when the if
* statement falsifies, it prints the final value with a new line and exits.
*/
for( x ; x >= 1 ; x -= 1) {
if (x != 1){
printf("%d, ", x);
}else{
printf("%d\n", x);
}
}
}

View File

@@ -0,0 +1,26 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: perrenc_hw0_5.c
* Created: Unknown by perrenc
* Last Modified: 2012 February 21, 20:49 by perrenc
*
* Total all integers from 1 to whatever value is necessary to make x in the loop equal
* to 1000.
*
*/
#include <stdio.h>
int main(int argc, char **argv){
int x; // Variable used for incrementing
int total; // Variable used for holding the total amount
/* Increments x, then adds current x to the total until x is equal to 1000. */
for(x = 1 ; x <= 1000 ; x++){
total += x;
}
/* Prints the total after going through the loop. */
printf("%d\n", total);
}

View File

@@ -0,0 +1,42 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: perrenc_hw0_6.c
* Created: Unknown by perrenc
* Last Modified: 2012 February 21, 20:54 by perrenc
*
* Calulates and prints out the fibonacci sequence up to a value the user specifies.
*
*/
#include <stdio.h>
int main (int argc, char **argv){
int x; // Used for loop limiting
int k; // Used for user input
int a = 0; // Beginning of the fibonacci sequences
int b = 1; // Another beginning value of the fibonacci sequence
int total;
/*
* Asks users to what value of the fibonacci sequence they would like to
* calculate and then stores it in k
*/
printf("Enter to what iteration of the fibonacci sequence you want to calculate: ");
scanf("%d", &k);
/*
* Prints an iteration of the fibonacci sequence, totals the two previous values
* to make the next iteration, then loops until the number of iterations reaches
* what the user inputted previously.
*/
for(x = 0 ; x < k ; x++){
printf("%d ", a);
total = a + b;
a = b;
b = total;
}
/* Prints a new line to make the output cleaner. */
printf("\n");
}

View File

@@ -0,0 +1,30 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: perrenc_hw0_7.c
* Created: Unknown by perrenc
* Last Modified: 2012 February 21, 20:54 by perrenc
*
* Prints the ascii character for the number a user enters.
*
*/
#include <stdio.h>
int main(int argc, char **argv){
int x; // Variable used for user input
/* Ask user for input, then store it on x. */
printf("Please enter a number between 0 and 255: ");
scanf("%d", &x);
/* Prints the appropriate ascii character for an entered value as long as it is
* within range, else it prints an error, or exits with a "-1" input.
*/
if ((x < 0 | x > 255) & (x != -1)){
printf("You entered an improper value or it was out of range...\n");
}else if ((x >= 0) & (x <= 255)){
printf("%c\n", x);
}
}

View File

@@ -0,0 +1,31 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: perrenc_hw0_9.c
* Created: Unknown by perrenc
* Last Modified: 2012 February 21, 20:54 by perrenc
*
* Print the square and cube roots of values from 1 to 25.
*
*/
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv){
int x; // Variable used for incrementing from 1 to 25
double y; // Variable used for squares and cubes manipulation
/*
* Increments x from 1 to 25 and prints the x value, its square root, and cube
* root in column format, then ends.
*/
for(x = 1 ; x <= 25 ; x++){
y = x;
printf("%i ", x);
y = sqrt (x); // Takes the square root of x
printf("%f ", y);
y = pow(x, (1/3.0)); // Takes the cube root of x
printf("%f \n", y);
}
}

View File

@@ -0,0 +1,3 @@
Corwin Perren
ECE 151 Section 10
931759527

View File

@@ -0,0 +1,193 @@
1 - What does a comment in C look like? Give some examples.
/** This is what the majority of block comments in C look like. Once it is
* recognized as a comment, the block color will change for easier
* readability.
*/
// This is an example of a single line comment. It's useful for commenting on
// the same line as actual code.
2 - How does one open a file in vi? Save a file? Quit?
a. To open a file, you type "vi filename" at the linux command line.
To save a file, enter vi command mode and type ":w"
To quit, enter vi command mode and type ":q" or press shift-z-z
3 - (3.1) Write the octal, decimal, and hexadecimal equivalents of these numbers.
Answers in form "Original: octal, decimal, hexadecimal"
a. 00110011 = 63, 51, 33
b. 01101100 = 154, 108, 3C
c. 01110111 = 167, 119, 77
d. 00111101 = 75, 61, 3D
e. 11111011 = -5, -5, -5
f. 11111111 = -1, -1, -1
g. 00100010001011010 = 42132, 17498, 445A
h. 10100010001011010 = -135646, -48038, -BBA6
4 - (3.5) Find the errors in each of the following segments.
a. printf("%s\n", 'Happy Birthday');
Fixed: printf("%s\n", "Happy Birthday");
b. printf("\%c\n", 'Hello');
Fixed: printf("%s\n", "Hello");
c. printf("%c\n", "This is a string);
Fixed: printf("%s\n", "This is a string");
d. printf(""%s"", 'Good Bye');
Fixed: printf("%s", "\"Good Bye\"\n");
e. char c = 'a';
printf("%s\n", c);
Fixed: char c = 'a';
printf("%c\n", c);
f. printf('Enter your name: ");
Fixed: printf("Enter your name: ");
g. printf( %f, 123.456);
Fixed: printf("%f", 123.456);
h. printf("%s%s\n", 'O', 'K');
Fixed: printf "%s%s\n, "O", "K");
i. char c;
scanf("%c", c);
Fixed: char c;
scanf("%c", &c);
j. double d;
scanf("%f", &d);
printf("%f", d);
Fixed: double d;
scanf("%lf", &d);
printf("%lf", d);
k. int d;
scanf("%f", &d);
printf("%f", d);
Fixed: int d;
scanf("%d", &d);
printf("%d", d);
l. double x = 123.45678, y;
int d;
d = x;
y = d;
printf("%f\n", y);
Fixed: double x = 123.45678
double d;
d = x;
y = d;
printf("%f\n", y);
5 - (3.6) How many digits are after the decimal point for floating point values
printed with printf? Does printf round or truncate values?
a. There are six values after the decimal point by default. Yes,
printf will always truncate to this number unless otherwise specified, and
while doing so, it rounds the answer.
6 - (3.17) Signed and unisgned representation problems
a. Signed - 256
Unsigned - 128
b. Signed - 65536
Unsigned - 32768
c. Signed - 4294967296
Unsigned - 2147483648
d. Signed - 64
Unsigned - 32
e. Signed - 1024
Unsigned - 512
7 - (4.1) State whether the following are true or false?
a. False. Operators in C are evaluated from right to left. The
compiler reads down the code, the evaluates coming back up in the opposite
direction.
b. False. The || operator is an inclusive or, meaning that if either
or both of the operator statements are true, the expression will evaluate as
true.
8 - (4.12) - Write C Expressions
a. (x + 4) / (y + 5)
b. 9 + (5*((x + 4) / (y + 5)))
c. (4 / 3) * 3.14 * r pow(r, 3)
d. x < y
e. (x >= y) & (y <= z)
f. (x >= y) & (x >= 0)
g. (x <= 1) | (x > 20)
h. (x > 1) & (x <= 20)
i. ( (x > 1) & (x <= 20) ) | ( (y <= 3) & (y >= 1) )
9 - (5.3) Correct errors in each of the code blocks
a. if(age >= 65);
printf("Age is greater than or equal to 65\n");
else
printf("Age is less than 65\n")
a fixed: if(age >= 65){
printf("Age is greater than or equal to 65\n");
}else{
printf("Age is less than 65\n");
}
b. if(i = 2)
printf("i is 2\n");
else
printf("i is not equal to 2\n");
b fixed: if(i == 2){
printf("i is 2\n");
}else{
printf("i is not equal to 2\n");
}
c. for(x = 999; x >= 1; x +=2)
printf("%d\n", n);
c fixed: for(int x = 999; x >= 1; x -=2){
printf("%d\n", x);
}
d. counter = 2;
Do {
if(counter % 2 == 0)
printf("%d\n", counter);
counter += 2;
}While(counter < 100)
d fixed: counter = 2;
while(counter < 100){
if((counter % 2) == 0){
printf("%d\n", counter);
counter += 2;
}
}
e. total = 1;
for(x = 100; x <= 150; x++);
total += x;
e fixed: total = 0;
for(int x = 100; x <= 150; x++){
total += x;
}

View File

@@ -0,0 +1,23 @@
/** Pseudocode for question 6, homework 1, in ece151
* Made by Corwin Perren
*/
create integer x
create integer t
create integer sum
create integer a and set to 0
create integer b and set to 1
print "Enter the step of the Fibonacci sequence you want to calculate:"
print a new line
set integer t from user input
while
t is greater than x
print a
sum a and b
set a = b
set b = sum
increment x by 1
when false print new line

View File

@@ -0,0 +1,21 @@
/** Pseudocode for question 7, homework 1, in ece151
* Made by Corwin Perren
*/
create integer x
print "Enter a number between 0 and 255"
print a new line
receive an integer "x" from user
if
1: x is less than 0 and greater than 255
and
2: x is not -1
print "The value you entered was improper or out or range"
print a new line
else if
1: x is greater than or equal to 0
and
2: x is less than or equal to 255
print corresponding ASCII character from value of x