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