diff --git a/assignment4.txt b/assignment4.txt new file mode 100644 index 0000000..8dde58e --- /dev/null +++ b/assignment4.txt @@ -0,0 +1,3 @@ +1) A character array is a series of bytes representing characters. A string is all of the characters in that array that come before a '\0'. + +2) Arrays are useful when you need several variables, where the purpose of the values are too similar to give each their own name, and iterating through them would make more sense. diff --git a/charFrequency.c b/charFrequency.c new file mode 100644 index 0000000..7e8010b --- /dev/null +++ b/charFrequency.c @@ -0,0 +1,36 @@ +/* Harry Brickner + Given a string, will return the frequencies of each character in the string */ +#include +#include + +int main(){ + printf("Please input a string.\n"); + char input[101]; + scanf("%s", input); + int length = strlen(input); + + char uniqueChars[length]; + int uniqueCharCount[length]; + int ucSize = 0; + + for(int i = 0; i < length; i++){ + int contains = 0; + int j; + for(j = 0; j < ucSize; j++){ + if(uniqueChars[j] == input[i]){ + contains = 1; + break; + } + } + + if(contains){ + uniqueCharCount[j]++; + }else{ + uniqueChars[ucSize] = input[i]; + uniqueCharCount[ucSize++] = 1; + } + } + for(int i = 0; i < ucSize; i++){ + printf("\'%c\' = %d\n", uniqueChars[i], uniqueCharCount[i]); + } +} diff --git a/hangman.c b/hangman.c new file mode 100644 index 0000000..10c5742 --- /dev/null +++ b/hangman.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include + +char ascii[7][24]={ + "***********************\0", + "* * *\0", + "* * *\0", + "* *****************\0", + "* *\0", + "* *\0", + "*******\0" +}; + +char human[5][5] = { + "(._.)", + " /|\\ ", + " | ", + " / \\ ", + " | | ", +}; + +char words[5][20] = { + "gnu is not unix\0", + "lorem ipsum\0", + "sample text\0", + "i hate arrays\0", + "hello world\0" +}; + +char guessString[20]; + +int wrongAnswers = 0; + +void wrongAnswer(){ + wrongAnswers++; + switch(wrongAnswers){ + case 1: + for(int i = 0; i < 5; i++) + ascii[1][i + 1] = human[0][i]; + break; + case 2: + ascii[2][3] = human[1][2]; + break; + case 3: + ascii[3][3] = human[2][2]; + break; + case 4: + ascii[2][2] = human[1][1]; + ascii[2][4] = human[1][3]; + break; + case 5: + for(int i = 0; i < 5; i++) + ascii[4][i + 1] = human[3][i]; + break; + case 6: + for(int i = 0; i < 5; i++) + ascii[5][i + 1] = human[4][i]; + break; + } +} + +void drawAscii(){ + for(int i = 0; i < 5; i++) + printf("%s\n", ascii[i]); + printf("%s %s\n", ascii[5], guessString); + printf("%s\n", ascii[6]); +} + +int main(){ + srand(time(0)); + char word[20]; + strcpy(word, words[rand() % 5]); + for(int i = 0; i < strlen(word); i++){ + if(word[i] == ' '){ + guessString[i] = ' '; + }else{ + guessString[i] = '_'; + } + } + guessString[strlen(word)] = '\0'; + drawAscii(); + + while(1){ + printf("Please guess a character.\n"); + char guessChar = getchar(); + while(getchar() != '\n'); + + if(!strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", guessChar)) continue; + + int matches = 0; + for(int i = 0; i < strlen(word); i++){ + if(word[i] == guessChar){ + guessString[i] = guessChar; + matches++; + } + } + if(!matches){ + ascii[1][8 + wrongAnswers] = guessChar; + wrongAnswer(); + } + for(int i = 0; i < 9; i++) printf("\033[F"); + drawAscii(); + if(strcmp(word, guessString) == 0){ + printf("\nYou win!\n"); + return 0; + }else if(wrongAnswers == 6){ + printf("\nYou lose!\n"); + return 0; + } + } + return 0; +} diff --git a/removeDuplicates.c b/removeDuplicates.c new file mode 100644 index 0000000..a50adb3 --- /dev/null +++ b/removeDuplicates.c @@ -0,0 +1,68 @@ +/* Harry Brickner + Given an array of integers, removes an array where all duplicate entries have been removed */ +#include + +/* I might go back and write a more efficient sorting algorithm, but for now I'll just do a bubblesort */ +void bubbleSort(int array[], int length){ + int inPerfectOrder = 0; + for(int numLoops = 0; !inPerfectOrder; numLoops++){ + inPerfectOrder = 1; + for(int i = 0; i < length - numLoops - 1; i++){ + if(array[i] > array[i + 1]){ + /* swaps the values */ + array[i] += array[i + 1]; + array[i + 1] = array[i] - array[i + 1]; + array[i] -= array[i + 1]; + inPerfectOrder = 0; + } + } + } +} + +/* returns true if the given array has an element equal to the given value */ +int contains(int array[], int arrayLength, int value){ + for(int i = 0; i < arrayLength; i++){ + if(array[i] == value) return 1; + } + return 0; +} + +int main(){ + printf("How long should the array be? "); + int length; + /* This bit makes sure that length is assigned, and that it is greater than 0 */ + while(1){ + while(!scanf("%d", &length)){ + printf("That is not a valid number. Try again.\n"); + while(getchar() != '\n'); + } + if(length > 0) break; + printf("That length is too low. Try again.\n"); + } + int array[length]; + for(int i = 0; i < length; i++){ + int value; + printf("Please give a value for the element at index %d: ", i); + while(!scanf("%d", &value)){ + printf("That's not a valid number. Try again.\n"); + while(getchar() != '\n'); + } + array[i] = value; + } + + /* At this point, we have an array. Now we have to generate an array with no duplicates */ + int dupeless[length]; + int dlSize = 1; + bubbleSort(array, length); + dupeless[0] = array[0]; + for(int i = 1; i < length; i++){ + if(dupeless[dlSize - 1] != array[i]) + dupeless[dlSize++] = array[i]; + } + + printf("Output array is:\n"); + for(int i = 0; i < dlSize; i++) + printf("%d, ", dupeless[i]); + printf("\n"); + return 0; +} diff --git a/sumArray.c b/sumArray.c new file mode 100644 index 0000000..6cc96da --- /dev/null +++ b/sumArray.c @@ -0,0 +1,69 @@ +/* Harry Brickner + Given an array, will return an array where each element is equal to the sum of all elements of the provided array with inices different than that of the element in question */ + +#include +#include +#include + +/* Get an array where each element is equal to the sum of all elements with a different index in the provided array */ +void calculateSumArray(int array[], int length){ + int sumArray[length]; + int sum = 0; + printf("The input array is:\n"); + for(int i = 0; i < length; i++){ + sum += array[i]; + printf("%d, ", array[i]); + } + printf("\nAnd the output array is:\n"); + for(int i = 0; i < length; i++){ + sumArray[i] = sum - array[i]; + /* I could just print it out without storing it in an array, but the assignment sounded like I was supposed to store it in an array */ + printf("%d, ", sumArray[i]); + } + printf("\n"); +} + +/* checks if the given length is valid and prints an error message if it isn't.*/ +int checkLength(int length){ + if(length <= 1){ + printf("The given length is too low. Please try again.\n"); + while(getchar() != '\n'); + return 0; + } + return 1; +} + +int main(){ + printf("To input an array, please enter the number of elements you'd like to input. To generate a random array, please enter \"random \"\n"); + int length; + while(1){ + if(scanf("random %d", &length)){ + if(!checkLength(length)) continue; + int array[length]; + srand(time(0)); + for(int i = 0; i < length; i++) + array[i] = rand() % 100; + calculateSumArray(array, length); + return 0; + }else if(scanf("%d", &length)){ + if(!checkLength(length)) continue; + int array[length]; + int i = 0; + while(i < length){ + printf("Please input the value for the element with index %d: ", i); + int value; + if(scanf("%d", &value)){ + array[i++] = value; + }else{ + printf("That is not a valid number. Try again.\n"); + while(getchar() != '\n'); + } + } + calculateSumArray(array, length); + return 0; + }else{ + printf("That's not a valid command. Try again.\n"); + while(getchar() != '\n'); + } + } +}