diff --git a/assignment4.txt b/assignment4.txt new file mode 100644 index 0000000..61c1a12 --- /dev/null +++ b/assignment4.txt @@ -0,0 +1,9 @@ +Aly Milich + +1. A string is collection of characters that can make up words or sentences. In order to read a string in C, strings have to be split into character arrays which break apart the string into individual chars. + +2. Arrays are advantageous because they give the programmer complete control over what's inside and organizes data. It is easy and efficient to create an array and add/delete things from the array. A disadvantage is that they can be difficult to navigate sometimes and require a lot of loops to organize/search through. + +3. The compiler won't implicitly generate the address of the first element in the array when an array is initialized without values originally added to it. + +4. The easiest way to compare two strings to see if they're equal is the function strcmp(). If 0 is returned, the strings are equal. diff --git a/dubarr.c b/dubarr.c new file mode 100644 index 0000000..406c936 --- /dev/null +++ b/dubarr.c @@ -0,0 +1,35 @@ +#include +/*remove doubles in an array + @author Aly Milich + */ +int main(){ + + int input[5] = {4, 5, 4, 5, 6}; + int output[5]; + + int swap, i, j; + + + for(i =0; i<5; i++){ + for(j=0; j<4; j++){ + if(input[j]>input[j+1]){ + swap = input[j]; + input[j] = input[j+1]; + input[j+1] = swap; + } + } + } + + for(i=0; i<5; i++){ + if(input[i] != input[i-1]) + output[i] = input[i]; + } + + for(i=0; i<5; i++){ + if(output[i]!=0) + printf("%d\n", output[i]); + } + + + return 0; +} diff --git a/hangman.c b/hangman.c new file mode 100644 index 0000000..52dc0d4 --- /dev/null +++ b/hangman.c @@ -0,0 +1,41 @@ +#include + +/* game of hangman + + @author Aly Milich + */ + +int main(){ + + char word[8] = "computer", guess; + int lives=5; + + printf("You have 5 lives.\n"); + printf("The word has 8 letters."); + + while(1){ + printf("Enter your guess.\n"); + scanf("%c", &guess); + + guess = getchar(); + + for(int i =0; i<5; i++){ + + if(word[i] == guess) + printf("That is one of the letters!\n"); + else{ + printf("That is not one of the letters.\n"); + lives--; + printf("You have %d lives left.\n", lives); + } + + } + } + + if(lives == 0){ + printf("You lose."); + } + + return 0; + +} diff --git a/numchars.c b/numchars.c new file mode 100644 index 0000000..0a1aefc --- /dev/null +++ b/numchars.c @@ -0,0 +1,59 @@ +#include + +/*number of a char in an input +I wanted to do something a little nicer than just creating a counter for each letter so I tried to do this (alphabetizes and splits the array). It mostly works except it doesn't recognize uppercase and lowercase as different and sometimes has a few glitches. I've been looking at it for a while and was wondering someone could advise me on how to fix this but I can look more tomorrow. Sorry it isn't 100% yet, but I thought it would be good to try something a little more complex. + @author Aly Milich + */ + +int main(){ + + int amount, i, j, counter=1, resets =0; + + printf("How many characters do you want to input?\n"); + scanf("%d", &amount); + + char input[amount]; + char letter[amount]; + int number[amount]; + + printf("Enter the chars you would like to input.\n"); + + char temp; + + for(i=0; iinput[j+1]){ + temp = input[j+1]; + input[j+1] = input[j]; + input[j] = temp; + } + } + } + + letter[0] = input[0]; + number[0] = 1; + + for(i=1; i + +int main(){ + + int arr[5] = {1, 2, 3, 5, 6}; + int output[5]; + + int i, sum; + + for(i =0; i<5; i++) + sum = sum + arr[i]; + + for(int j =0; j<5; j++) + output[j] = sum-arr[j]; + + for(int k=0; k<5; k++) + printf("%d\n", output[k]); + + return 0; + +}