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

View File

@@ -0,0 +1,459 @@
/*
* Original Author: Corwin A. Perren (perrenc)
* File: perrenc_hw1.c
* Created: 2012 February 17, 14:47 by perrenc
* Last Modified: 2012 February 20, 17:07 by perrenc
*
* This program is the entirety of assigment 2 for ece151
* and requires compiling with the -lm argument to include
* math linking.
*/
/* Includes */
#include <stdio.h>
#include <math.h>
/* Defines */
// For prob 1 ( 10.7 )
#define WEEK 5
#define DAY 7
/* --------------------Begin Functions-------------------- */
/* ********Problem 1******** */
/*
* This program takes a month of web page hit values and averages them per week day for
* the month.
*/
void prob1(void){
system("clear"); //Clears the screen
int dayloop; //For looping the day variable
int weekloop; //For looping the week variable
int mean[DAY] = {0, 0, 0, 0, 0, 0, 0}; //Array for storing mean values
/* This is the array taken from the book. */
int hits[WEEK][DAY] = {367, 654, 545, 556, 565, 526, 437,
389, 689, 554, 526, 625, 537, 468,
429, 644, 586, 626, 652, 546, 493,
449, 689, 597, 679, 696, 568, 522,
489, 729, 627, 729, 737, 598, 552};
/* Fills the mean array with sums of the weekday values for the month. */
for (weekloop = 0 ; weekloop < WEEK ; weekloop++){
for (dayloop = 0 ; dayloop < DAY ; dayloop++){
mean[dayloop] += hits[weekloop][dayloop];
}
}
/*
* Averages the values in mean for the number of weekdays summed in the previous
* step.
*/
for (dayloop = 0 ; dayloop < DAY ; dayloop++){
mean[dayloop] /= WEEK;
}
/* Prints the mean values for each day of the week. */
printf("The mean number of hits for Mondays of this month was %d\n", mean[0]);
printf("The mean number of hits for Tuesdays of this month was %d\n", mean[1]);
printf("The mean number of hits for Wednesdays of this month was %d\n", mean[2]);
printf("The mean number of hits for Thursdays of this month was %d\n", mean[3]);
printf("The mean number of hits for Fridays of this month was %d\n", mean[4]);
printf("The mean number of hits for Saturdays of this month was %d\n", mean[5]);
printf("The mean number of hits for Sundays of this month was %d\n", mean[6]);
pause (); //Waits for user to press enter before returning to the menu
}
/* ********Problem 2******* */
/* Calculates a centroid value for an array of pixels with intensities. */
void prob2(void){
system("clear"); //Clears the screen
int loop; //Used for loop incrementing
double centroidx = 0; //Stores the centroid x value
double centroidy = 0; //Stores the centroid y value
double intensitytotal = 0; //Stores the intensity total
/* Pixels locations and intensities from the book. */
double pixel[][2] = {5.0, -4.0, 4.0, 3.0, -1.0, 6.0, -9.0, 5.0};
double intensity[] = {.25, .57, .63, .1};
/*
* Loops through the pixel array and performs the summing for the centroid
* calculation and stores the values in centroidx and y.
*/
for (loop = 0 ; loop <= 3 ; loop++){
centroidx += (pixel[loop][0]*intensity[loop]);
centroidy += (pixel[loop][1]*intensity[loop]);
}
/*
* Loops through the intensity array and sums the four values and stores it in
* intensitytotal.
*/
for (loop = 0 ; loop <= 3 ; loop++){
intensitytotal += intensity[loop];
}
/*
* Divides the centroidx and y sums by the intensitytotal to complete the last
* step of the centroid calculation.
*/
centroidx /= intensitytotal;
centroidy /= intensitytotal;
/* Prints the centroid value. */
printf("Centroid = %.2lf, %.2lf", centroidx, centroidy);
pause (); //Waits for user input before returning to the menu.
}
/* ********Problem 3******** */
/*
* Searches an array of binary values for a specific binary value and returns whether it
* finds it.
*/
void prob3(void){
system("clear"); //Clears the screen
int loop; //Used for looping
int searchvalue = 0b01010001; //Value that is searched
int searchreturn; //Stores binary search return value
/* Random binary data. */
int data[8] = {0b00000001, 0b00010001, 0b10010101, 0b01101110,
0b11011011, 0b11111110, 0b00111101, 0b10011101};
printf("---------- Data Array Values --------\n"); //Prints a header
/* Prints the array. */
for (loop = 0 ; loop < 8 ; loop++){
printf("%d\n", data[loop]);
}
/* Searches the array with the binary value specified above. */
searchreturn = binarysearch (8, data, searchvalue);
/*
* Prints a different message depending on whether the value searched was
* found or not.
*/
if (searchreturn != -1){
printf("\nThis binary value equivalent to %d was found in ", searchvalue);
printf("element %d of the array.\n", searchreturn);
}else {
printf("\nThis binary value equivalent to %d could not ", searchvalue);
printf("be found in the array.");
}
pause (); //Watis for the user to press enter before returning to the menu
}
/*
* Function that compares an array of binary values against a search value and returns
* whether the search term was found.
*/
int binarysearch(int arraysize, int data[], int search){
int loop; //Used for looping
/*
* Searches the array for a binary value and returns whether it was found or
* not.
*/
for (loop = 0 ; loop < arraysize ; loop++){
if (search == data[loop]){
return loop;
}
}
return -1; //Returns if the search value is not found
}
/* ********Problem 5******** */
/* Performs matrix multiplication on two arrays the size of which the user can specify */
void prob5(void){
system("clear"); //Clears the screen
int arraysize; //Used for storing the array size
int loop1; //Used for level one looping
int loop2; //Used for level two looping
int loop3; //Used for level three looping
/* Allows the user to specify the size of the arrays to multiply. */
printf("Please enter the number of columns and rows the arrays should have: ");
scanf("%d", &arraysize);
system("clear"); //Clears the screen
int array1[arraysize][arraysize]; //First array
int array2[arraysize][arraysize]; //Second array
int array3[arraysize][arraysize]; //Third array with matrix multiplication
/*
* Fills arrays 1 and 2 with numbers based on where they are in the loops and fills
* array three with zeros so we don't get random values.
*/
for (loop1 = 0 ; loop1 < arraysize ; loop1++){
for (loop2 = 0 ; loop2 < arraysize ; loop2++){
array1[loop1][loop2] = (2*((loop1+1)*(loop2+1)));
array2[loop1][loop2] = (4*((loop1+1)*(loop2+2)));
array3[loop1][loop2] = 0;
}
}
printf("This is array 1:\n"); //Prints a header for array 1
/* Prints out array 1 */
for (loop1 = 0 ; loop1 < arraysize ; loop1++){
for (loop2 = 0 ; loop2 < arraysize ; loop2++){
printf("%d ", array1[loop1][loop2]);
}
printf("\n");
}
printf("\nThis is array 2:\n"); //Prints a header for array 2
/* Prints out array 2 */
for (loop1 = 0 ; loop1 < arraysize ; loop1++){
for (loop2 = 0 ; loop2 < arraysize ; loop2++){
printf("%d ", array2[loop1][loop2]);
}
printf("\n");
}
/*
* Performs matrix multiplication on array 1 and array 2 and stores it on
* array 3.
*/
for (loop1 = 0 ; loop1 < arraysize ; loop1++){
for (loop2 = 0 ; loop2 < arraysize ; loop2++){
for (loop3 = 0 ; loop3 < arraysize ; loop3++){
array3[loop1][loop2] += (array1[loop1][loop3] *
array2[loop3][loop2]);
}
}
}
/* Prints a header for array 3 */
printf("\nThis is array 3, which is the result of matrix multiplication on ");
printf("array 1 and 2:\n");
/* Prints out array 3 */
for (loop1 = 0 ; loop1 < arraysize ; loop1++){
for (loop2 = 0 ; loop2 < arraysize ; loop2++){
printf("%d ", array3[loop1][loop2]);
}
printf("\n");
}
pause (); //Waits for the user to press enter before returning to the menu
}
/* ********Problem 6******** */
/*
* Calculates primes up to a user entered value by marking composites and printing
* what's left.
*/
void prob6(void){
system("clear"); //Clears the screen
int userin1; //Used for user input
int loop1; //Used for level one loops
int loop2; //Used for level two loops
int loop3; //Used for level three loops
int total = 0; //Used for totaling the number of primes
/*
* Asks the user for a number to calculate prime number to, stores it, and
* clears the screen.
*/
printf("Enter the value you want primes calculated to: ");
scanf("%d", &userin1);
system("clear");
int sieve[(userin1-1)]; //Creates an array for holding sieve values in
/* Fills the array with integers from 2 to the user entered value */
for(loop1 = 2 ; loop1 <= userin1 ; loop1++){
sieve[(loop1-2)] = loop1;
}
/* Sets all composites in the array to zero. */
for(loop1 = 0 ; loop1 < userin1; loop1++){
for(loop2 = 2; loop2 < userin1; loop2++){
for(loop3 = loop2; loop3 < userin1; loop3++){
if ((loop2*loop3) == sieve[loop1]){
sieve[loop1] = 0;
}
}
}
}
/*
* Prints out all values in the array that aren't zero, which leaves you with
* only prime numbers.
*/
printf("Here are the prime numbers from 0 to the number you entered: \n\n");
for(loop1 = 0 ; loop1 < (userin1-1) ; loop1++){
if(sieve[loop1] != 0){
printf("%d\n", sieve[loop1]);
total++; //Increments total when something is printed
}
}
/* Prints the number of primes. */
printf("\nThere were %d primes in this list. ", total);
pause (); //Waits until the user presses enter, then returns to the menu
}
/* ********Problem 7******* */
/*
* This takes in a string, then copies each value to another string and adds a null
* character before returning the copied string.
*/
char str_copy(char *s2, const char *s1){
int loop; //Used for looping
/*
* Copies each character in string1 to the same place in string2 until it
* encounters the null character.
*/
for (loop = 0 ; s1[loop] != '\0' ; loop++){
s2[loop] = s1[loop];
}
s2[(loop++)] = '\0'; //Adds the null character back in
return *s2; //Returns the copied string
}
/* Takes a user entered string and copies it to another string. */
void prob7(void){
char string1[100]; //Storage for string1
char string2[100]; //Storage for string2
int loop; //Used for looping
system("clear"); //Clears the screen
/*
* Asks the user to enter a string, clears the buffer, and stores the string on
* string1.
*/
printf("Please enter a string:\n");
while(getchar() != '\n'){
; //VOID
}
fgets(string1, 100, stdin);
/*
* Replaces the newline character fgets scans in with a null character to make
* the array into a string.
*/
for (loop = 0 ; loop < 100 ; loop++){
if (string1[loop] == '\n'){
string1[loop] = '\0';
break;
}
}
printf("\nThis is string 1:\n%s\n\n", string1); //Prints string1 header
str_copy(string2, string1); //Copies str1 to str2
printf("This is string 2 after being copied:\n%s\n", string2); //Prints header
/*
* This is a modified version of my pause function that accounts for the fact
* that a buffer flush already happened. Essentially, this removes the extra
* getchar so that you don't have to hit enter twice to return to the menu.
*/
printf("\n\nPress enter to return to the menu.");
while(getchar() != '\n'){
; //VOID
}
}
/* Generic Functions */
/* Pauses until the user presses enter, then clears the buffer in case they type
* somthing before they hit enter */
int pause(void){
printf("\n\nPress enter to return to the menu.");
getchar ();
while('\n' != getchar()){
; //VOID
}
}
/* --------------------End Functions-------------------- */
int main(int argc, char **argv){
int menu; // Used for selecting a menu item
beginning: ; // Used for goto statements in switch
system("clear"); //Clears the screen
/* Creates the list of possible selections for the user menu. */
printf("Please select which homework problem you would like to run:\n");
printf("1 -- Problem 1 ( 10.7 )\n");
printf("2 -- Problem 2 ( 10.10 )\n");
printf("3 -- Problem 3 ( 10.30 )\n");
printf("4 -- Problem 5 ( Matrix Multiplication )\n");
printf("5 -- Problem 6 ( Sieve of Eratosthenes )\n");
printf("6 -- Problem 7 ( 11.6 )\n");
printf("7 -- Exit Program\n\n");
printf("Enter Selection: ");
scanf("%d", &menu);
/*
* Runs a particular program based on the user's input. Goto's are used since
* switch is a much cleaner way to impliment the menu selection and I could not
* find an alternative way.
*/
switch(menu){
case 1:
prob1 (); //Runs problem 1
goto beginning; //Goes back to the menu after running
case 2:
prob2 (); //Runs problem 2
goto beginning; //Goes back to the menu after running
case 3:
prob3 (); //Runs problem 3
goto beginning; //Goes back to the menu after running
case 4:
prob5 (); //Runs problem 5
goto beginning; //Goes back to the menu after running
case 5:
prob6 (); //Runs problem 6
goto beginning; //Goes back to the menu after running
case 6:
prob7 (); //Runs problem 7
goto beginning; //Goes back to the menu after running
case 7:
system("clear"); //Clears the screen
return 0; //Ends the program
}
}

View File

@@ -0,0 +1,254 @@
Corwin Perren - 931759527
Assignment 2 - ECE151
Written Questions
Problem 1(10.2)
a.
Original code:
char str[5];
scanf("%s", str); /* User types hello */
Fixed code:
char str[5];
scanf("%s", &str); //user types hello
Explanation:
An ampersand is needed to store the value to memory, and the commenting
is inconsistent with the style guide.
b.
Original:
int a[3];
printf("%d %d %d\n", a[1], a[2], a[3]);
Fixed:
int a[3] = {1, 3, 5};
printf("%d %d %d\n", a[0], a[1], a[2]);
Explanation:
The original creates an empty array with no values in it, which makes it
impossible to print its contents. The location in memory is dumped. Also,
the locations printf is pulling from are one block off. The first value
has location [0,0], not [1,1].
c.
Original:
double f[3] = {1.1, 10.01, 100.001, 1000.001};
Fixed:
double f[4] = {1.1, 10.01, 100.001, 1000.001};
Explanation:
The array is not large enough.
d.
Original:
double d[2][10];
d[1,9] = 2.345;
Fixed:
float d[2][10];
d[1][9] = 2.345;
Explanation:
Used incorrect location mapping and the double was not big enough to
handle this decimal.
Problem 2(10.4)
Original:
int N;
int M=90;
int funct4(int n) {
int i;
int m = n;
int a[n][n];
int b[n][m];
int c[printf("%d\n", n)][n];
int d[n][N];
int e[n][M];
int f[n] = {1, 2, 3, 4};
static int g[n];
for(i=0; i<n; i++) {
a[i][i] = 3+b[i][i];
}
funct4(4);
Fixed (option 1):
int funct4(int n) {
int N; // these two should be initialized within the function
int M = 90; //spacing
int i;
int n = 50; //int n needed, with value
int m = n;
int a[n][n];
int b[n][m];
int c[n][n]; //removed print statement (unnecessary)
int d[n][N];
int e[n][M];
int f[n] = {1, 2, 3, 4};
static int g[n];
for(i=0; i<n; i++) {
a[i][i] = 3+b[i][i];
}
} //end bracket needed
funct4(4); //sets n = 4
Fixed (option 2):
int funct4(int n) {
int n; //no capital letters allowed
int m = 90 //no capital letters allowed
int i;
int m = n;
int a[n][n];
int b[n][m];
int c[n][n]; //removed print statement (unnecessary)
int d[n][n]; //no capital letters allowed
int e[n][m]; //no capital letters allowed
int f[ ][n] = {1, 2, 3, 4}; //needed first dimension brackets
static int g [ ][n]; //needed first dimension brackets
for(i=0; i<n; i++) {
a[i][i] = 3+b[i][i];
}
} //end bracket needed
funct4(4);
Prob 3(11.1)
a. No errors, just prints an empty value.
b. Fixed:
float *realPtr;
int *integer pointer;
*integerPtr = (int) *realPtr //needed asterisks and typecasting.
c. Fixed:
int *x, y;
x = &y; //need to specify that y is the target of the pointer x
d. Fixed:
int i; //needs a looping variable
char s[] = 'this is a character array";
for(i = 0; *(s+1) != '\0'; i++) { //needs loop assignment, parentheses,
// and brackets
printf("%c", *(s+i)); //matches original pointer
} //needs end bracket
e. Fixed:
short *numPtr, result;
void *genericPtr = numPtr;
result = ((short) *genericPtr) + 7; //needs typecast change
f. Fixed:
float x = 19.34;
float *xPtr = &x; //missing splat
printf("%f\n", xPtr);
g. Fixed:
char *s;
printf("%c\n", *s); //since s is a char, it should be %c, and it also
//needs to be *s, since that's the real name
h. Fixed:
int funct1(int *p){
int i = -1;
int *pp;
return i; //it doesn't return a pointer
}
int main(int argc, char **argv){ //main needs arguments
int i = 5;
funct1(&i); //makes int *p point to the address of i, not the
//value.
}
Problem 4(11.2)
a. False. Pointers can always be dereferenced to be compared.
b. True.
Problem 5(11.3)
a. long long int *11ptr;
b. 11ptr = &value1;
c. printf("%d\n", *11ptr);
Problem 6(11.4)
a. int main(int argc, char **argv) {
int count = 0;
int a[10];
int *p = (int *) malloc(10*sizeof(int));
for(count = 0; count < 10; count++) {
a[count] = 5;
}
free (int *p);
return 0;
}
b. char *str = (char *) malloc(10 * sizeof("this string"));
Problem 7(11.8)
This application tries to scan two strings and print only the first, but because
of the error on line "scanf("%s%s", string1, string2);", it puts both strings
into string1 and prints them consecutively.
Problem 8(11.9)
This application scans a user string and outputs its length.
Problem 9(11.21)
Original test value:
a[1][2] (8)
in int a[3][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
everything that isn't equal to 8 is false
Test Code Output:
8 - A
8 - C
8 - D
8 - E
4 - F
-1071285860 - G
8 - H
6 - I
8 - J
-1071285780 - K
8 - L
8 - M
5 - N
7 - O
-1071285800 - P
9 - Q
B wouldn't even run as given.
True Values: A, C, D, E, H, J, L, M
False Values: F, G, I, K, N, O, P, Q
10. In logical operations, there is a concept known as short circuiting. What this means
is that as soon as a logical expression's truth value can be determined, evaluation
stops. For instance, with an OR statement, if the first portion is TRUE, the second would
not be evaluated in a short circuit evaluation system. Discuss how you might determine
whether C uses short circuit evaluation. Consider implementing your idea to test it. Did
it work? Include any lines of code necessary to test within the text file.
Yes, C uses short circuit evaluation. The follow function returned:
"Yup, it worked.
That worked too."
#include <stdio.h>
#define x 5
#define y 10
int main (int argc, char **argv) {
if ((x == 5) | (x == 6)) {
printf("Yup, it worked.\n");
}
if ((x == 6) | (y == 10)) {
printf("That worked too.\n");
}
return 0;
}

View File

@@ -0,0 +1,160 @@
Corwin Perren - 931759527
Assignment 2 - ECE151
Pseudocode
--------Problem 1 ( 10.7 )--------
Clear the screen.
Initialize the variables dayloop and weekloop.
Initialize the array "mean" and fill it with zeros.
Initialize and fill the array "hits" with the array data from the book.
Sum all integers in the same column and store them in the corresponding
column of "mean".
Divide each column of "mean" by the number of values that were summed and
store that number back onto that value of mean.
Print out the values of "mean" for each day of the week.
Pause until the user presses enter, then return to the menu.
--------Problem 2 ( 10.10 )--------
Clear the screen
Initialize loop, centroidx, centroidy, and intenstity total.
Initialize the arrays "pixel" and "intensity" and fill them with the values
from the book.
Multiply each row of pixel by their respective column in intensity and sum
these onto centroidx and y respectively.
Add all the intensity values together and store it on intensitytotal.
Divide centroidx and y by intensitytotal and store the values back on those
those repective variables.
Print the values of centroidx and y.
Pause until the user presses enter, then return to menu.
--------Problem 3 ( 10.30 )--------
***Main Function***
Clear the screen.
Initialize loop, searchreturn, and searchvalue with a binary value you want to
search for.
Initialize the array "data" and fill it with made up binary numbers.
Print a header for a printout of the current array.
Print out the array "data".
Call binarysearch and store its return value on searchreturn.
If reutrnvalue is not equal to negative one, print a statement saying the
search was successful and the binary value was found, otherwise print a
statement saying the binary search failed and the value could not be found.
Pause until the user presses enter, then return to the menu.
***Binary Search Function***
Initialize loop.
Search for a match of the binary value in each element of the array. If a
result is found, return at which element it was found, else return negative
one.
--------Problem 5 ( Matrix Multiplication )--------
Clear the screen.
Initialize arraysize, loop1, loop2, and loop3.
Ask the user for the size of the arrays they would like to multiply and store
that value on arraysize.
Initialize array1, array2, and array3 as square arrays with arraysize as their
row and column sizes.
Fill array1 and array2 with numbers and array3 with zeros.
Print out a header for array1, then print array1.
Print out a header for array2, then print array2.
Make each element of array3 equal to the result of performing matrix
multiplication on array1 and array2.
Print a header for array3, then print out array3.
Pause until the user presses enter, then return to the menu.
--------Problem 6 ( Sieve of Eratosthenes )--------
Clear the screen
Initialize userin1, loop1, loop2, loop3, and total.
Ask the user for a number to calculate primes up to from zero, and store it on
userin1.
Initialize the array "sieve" with a size one less than userin1.
Fill "sieve" with integers from two to userin1.
Mark all composites for each integer from two to the user entered number by making
that value in the array equal to zero.
Print a header for the prime numbers that are left, then print out value from
the array that are not equal to zero while incrementing total.
Print out the number of prime numbers printed from the last step by printing
total.
Pause until the user presses enter, then return to the menu.
--------Problem 7 ( 11.6)--------
***Main Function***
Initialize arrays string1 and string2 and int loop.
Clear the screen.
Prompt the user to enter a string and store it on string1 after clearing the
buffer.
Loop through each element of string1 until a newline character is found.
Replace the newline character with a null character to turn the array into a
string.
Print out a header for string1 and the string itself.
Call str_cpy on string1.
Print a header for string2, then print the string itself.
Pause until the user presses enter, then return to the menu.
***Str_Copy Function***
Initialize loop.
Copy each element from the source string to the destination string, then add a
null character to the end to keep it a string.
Return the copied string.