Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions arrayfreq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Christopher Liu */
#include <stdio.h>
#include <string.h>

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;
}
31 changes: 31 additions & 0 deletions arraysum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Christopher Liu - I didn't get the instructions so I'm not sure if I interpreted it correctly*/
#include <stdio.h>

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;
}
33 changes: 33 additions & 0 deletions arraytrim.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

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;
}
5 changes: 5 additions & 0 deletions assignment4.txt
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 53 additions & 0 deletions hangman.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <string.h>

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");
}