From e89c36f5c7e6769b915f171f247178f0fd3eae90 Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Tue, 5 Jul 2016 10:19:37 -0400 Subject: [PATCH] Assignment 4 --- arrayfreq.c | 40 +++++++++++++++++++++++++++++++++++++ arraysum.c | 31 +++++++++++++++++++++++++++++ arraytrim.c | 33 ++++++++++++++++++++++++++++++ assignment4.txt | 5 +++++ hangman.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 arrayfreq.c create mode 100644 arraysum.c create mode 100644 arraytrim.c create mode 100644 assignment4.txt create mode 100644 hangman.c diff --git a/arrayfreq.c b/arrayfreq.c new file mode 100644 index 0000000..852edb0 --- /dev/null +++ b/arrayfreq.c @@ -0,0 +1,40 @@ +/* Christopher Liu */ +#include +#include + +int main() +{ + char string[100] = {}; + char reference[53] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}; + + int counter[53] = {}; + for (int i = 0; i < (int) strlen(string); i++) + { + counter[i] = 0; + } + + + printf("Enter string: "); + fgets(string, sizeof(string), stdin); + string[strlen(string)-1] = '\0'; + + for (int i = 0; i < (int) strlen(string); i++) + { + for (int j = 0; j < (int) strlen(reference); j++) + { + if (string[i] == reference[j]) + { + counter[j] += 1; + } + } + } + + for (int i = 0; i < (int) sizeof(counter)/4; i++) + { + if (counter[i] != 0) + { + printf("%c: %d\n", reference[i], counter[i]); + } + } + return 0; +} diff --git a/arraysum.c b/arraysum.c new file mode 100644 index 0000000..adc0490 --- /dev/null +++ b/arraysum.c @@ -0,0 +1,31 @@ +/* Christopher Liu - I didn't get the instructions so I'm not sure if I interpreted it correctly*/ +#include + +int main() +{ + printf("Size of array: "); + + int array_size; + scanf("%d", &array_size); + + int input_array[array_size], sum_array[array_size], sum = 0; + + for (int i = 0; i < array_size; i++) + { + printf("Input number at index %d: ", input_array[i]); + scanf("%d", &input_array[i]); + sum += input_array[i]; + } + + int ignored; + printf("Ignore which index? "); + scanf("%d", &ignored); + sum -= input_array[ignored]; + + for (int i = 0; i < array_size; i++) + { + sum_array[i] = sum; + printf("Value of array index %d: %d\n", i, sum_array[i]); + } + return 0; +} diff --git a/arraytrim.c b/arraytrim.c new file mode 100644 index 0000000..30ceb4d --- /dev/null +++ b/arraytrim.c @@ -0,0 +1,33 @@ +/* Christopher Liu - I wasn't sure how to delete a value from an array so I just set it to zero. Sorry */ +#include + +int main() +{ + printf("Size of array: "); + + int array_size; + scanf("%d", &array_size); + + int input_array[array_size], trimmed_array[array_size]; + + for (int i = 0; i < array_size; i++) + { + printf("Input number at index %d: ", input_array[i]); + scanf("%d", &input_array[i]); + trimmed_array[i] = input_array[i]; + } + + for (int i = 0; i < array_size; i++) + { + for (int j = 0; j < array_size; j++) + { + printf("%d:%d i, %d:%d j\n", i, input_array[i], j, input_array[j]); + if (input_array[i] == input_array[j] && i != j) + { + trimmed_array[i] = 0; + } + } + printf("Value of array index %d: %d\n", i, trimmed_array[i]); + } + return 0; +} diff --git a/assignment4.txt b/assignment4.txt new file mode 100644 index 0000000..f99cf83 --- /dev/null +++ b/assignment4.txt @@ -0,0 +1,5 @@ +Christopher Liu - I didn't get question 3 +1. A string allows you to use string operations, and ends with '\0'. +2. Arrays are useful because they're an organized way to create a bunch of variables with the same type. Disadvantages are the size of arrays cannot be changed, and all the variables of the array must be the same type. +3. It does not implicitly generate the address of an array when it is used in an operator such as sizeof() +4. You would use strcmp() after including string.h. The function will return zero if the strings are equivalent in content. diff --git a/hangman.c b/hangman.c new file mode 100644 index 0000000..a1a4031 --- /dev/null +++ b/hangman.c @@ -0,0 +1,53 @@ +/* Christopher Liu - wasn't sure how to end the game if you won, and the try counter is messed up */ +#include +#include + +int main() +{ + char hangword[7] = "eighth"; + int guessed[6] = {0,0,0,0,0,0}, tries = 0; + + while (tries <= 10) + { + printf("Tries: %d/10\n", tries); + printf("Current board state: \n"); + for (int i = 0; i < (int) strlen(hangword); i++) + { + if (guessed[i]) + { + printf("%c ", hangword[i]); + } + else + { + printf("_ "); + } + } + printf("\n"); + + printf("Guess a letter: "); + char guess; + scanf("%c", &guess); + + for (int i = 0; i < (int) strlen(hangword); i++) + { + if (guess == hangword[i]) + { + guessed[i] = 1; + } + } + + int check_won = 0; + for (int i = 0; i <= (int) sizeof(guessed)/4; i++) + { + if (guessed[i] == 1) check_won++; + + } + if (check_won == 6) + { + printf("You won!\n"); + break; + } + tries++; + } + printf("no more tries\n"); +}