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
11 changes: 11 additions & 0 deletions Part2
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 29 additions & 0 deletions array2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Matthew Danielson
* 7/4/16
* array2.c
* Takes ints as input, removes duplicates, and prints what is left
*/

#include <stdio.h>

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]);

}

}
86 changes: 86 additions & 0 deletions hangman.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Matthew Danielson
* 7/4/16
* Hangman.c
* The hangman game; pretty self explanatory
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

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<sizeof(guess)-1; x++)
printf("%c ", guess[x]);
printf("\n");
printf("Guesses remaining: \n");
printf("%d \n \n", remaining);
for(int x = 0; x < sizeof(correct)-1; x++){
if(correct[x] == 0)
printf("_ ");
else
printf("%c ", correct[x]);
}
printf("\n Please enter a guess:\n ");
scanf("%c", &cguess);
scanf("%c", &throwaway);
//Checking
check = 0;
for( int x = 0; x< sizeof(keyword)-1; x++){
if(cguess == keyword[x]){
correct[x] = keyword[x];
check = 1;
}
}
if(!(strcmp(keyword,correct))){
printf("You win!");
remaining = 0;
}
if( check != 1){
remaining--;
guess[counter] = cguess;
counter++;
}

check = 1;
if(remaining == 0)
gameloop = 0;

}
printf("Would you like to play again?(1 = yes, 0 = no)");
scanf("%d", &replay);
if(replay != 0)
gameloop = 1;

}
}
23 changes: 23 additions & 0 deletions narray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Matthew Danielson
* 7/4/16
* narray.c
* Takes the sum of elements of an array except the current element
*/

#include <stdio.h>

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



}
32 changes: 32 additions & 0 deletions strings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Matthew Danielson
* 7/4/16
* strings.c
* returns character frequency
*/

#include <stdio.h>
#include <string.h>

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<sizeof(chars)-1; x++){
sum = 1;
for(int y = 0; y< sizeof(chars); y++){
if(chars[x] == chars[y] && x !=y){
sum ++;
chars[y] = 0;
}
}
if((chars[x]> 65 && chars[x] < 90) ||( chars[x] >97 && chars[x] <122));
printf("%c: %d \n", chars[x], sum);
}

return 0;
}