diff --git a/Part2 b/Part2 new file mode 100644 index 0000000..a9b0406 --- /dev/null +++ b/Part2 @@ -0,0 +1,11 @@ +Matthew Danielson +7/1/16 +Assignment 4; Part 2 + +1) Strings are very similar to character arrays; the main difference is that a string has the \0 character at the end, denoting it as a string. Strings are defined by use of "", while chars are denoted by ''. Additionally, strings have functions that allow for concatenation, calculation of their size, and replacement of each other. + +2) In C, arrays have defined lengths, and their data types cannot be changed. Additionally, C does not check bounds, thus accessing indeces outside of the array calls other RAM values. However, accessing parts of an array individually does not take much CPU or time in C. + +3) When an array is used as an operand in either the sizeof or & operator, the first element is not implicitly generated. Additonally, when a string is used to initialize a character array, the first element is not generated implicitly. + +4) The function strcomp(s1,s2) will return 0 if the two strings are equivalent, thus allowing for a way of checking if they are the same. diff --git a/array2.c b/array2.c new file mode 100644 index 0000000..7816dd6 --- /dev/null +++ b/array2.c @@ -0,0 +1,29 @@ +/* Matthew Danielson +* 7/4/16 +* array2.c +* Takes ints as input, removes duplicates, and prints what is left +*/ + +#include + +int main(){ + printf("How many integers would you like to enter?"); + int total; + scanf("%d", &total); + int nums[total]; + for(int x = 0; x< total; x++){ + printf("Enter an integer: \n"); + scanf("%d", &nums[x]); + } + for(int x = 0; x< total; x++){ + for(int y = 0; y< total; y++){ + if(nums[x] == nums[y] && x != y) + nums[y] = '\0';//Prints as 0, but cannot be assigned as null. + //Thus, will not print zero's + } + if(nums[x] != 0) + printf("%d, ", nums[x]); + + } + +} diff --git a/hangman.c b/hangman.c new file mode 100644 index 0000000..41a5f5f --- /dev/null +++ b/hangman.c @@ -0,0 +1,86 @@ +/* Matthew Danielson +* 7/4/16 +* Hangman.c +* The hangman game; pretty self explanatory +*/ + +#include +#include +#include +#include + +int main(){ + //Hardcoded keyword + char keywords[5][6]; + strcpy(keywords[0], "train"); + strcpy(keywords[1], "power"); + strcpy(keywords[2], "magic"); + strcpy(keywords[3], "loose"); + strcpy(keywords[4], "wrath"); + int replay = 1; + int gameloop = 1; + srand(time(NULL)); + char keyword[6]; + char guess[26];//All characters that can be guessed + char correct[6]; + char cguess; + int counter = 0; + int check; + char throwaway; + while(replay){ + for(int x = 0; x < sizeof(guess); x++){ + guess[x] = 0; + if(x<6) + correct[x] = 0; + } + counter = 0; + int choice = rand()%6; + int remaining = 5; + strcpy(keyword, keywords[choice]); + printf("Welcome to Hangman! Attempt to guess the word a letter at a time!\n"); + while(gameloop){ + printf("Wrong guessed letters: "); + for(int x = 0; x + +int main(){ + int array[5]; + int sum=0; + printf("Please enter 5 integers. \n"); + for(int x = 0; x < 5; x++){ + scanf("%d", &array[x]); + sum += array[x]; + } + for(int x = 0; x < 5; x++){ + printf("%d \n", (sum - array[x])); + } + + + +} diff --git a/strings.c b/strings.c new file mode 100644 index 0000000..0eca4c8 --- /dev/null +++ b/strings.c @@ -0,0 +1,32 @@ +/* Matthew Danielson +* 7/4/16 +* strings.c +* returns character frequency +*/ + +#include +#include + +int main(){ + printf("How long wil your string be?"); + int length; + scanf("%d", &length); + char chars[length+1]; + printf("Please enter a string:"); + for(int x= 0; x< sizeof(chars); x++) + scanf("%c",&chars[x]); + int sum; + for(int x = 1; x 65 && chars[x] < 90) ||( chars[x] >97 && chars[x] <122)); + printf("%c: %d \n", chars[x], sum); + } + + return 0; +}