-
Notifications
You must be signed in to change notification settings - Fork 16
Assignment 4 #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Assignment 4 #14
Changes from all commits
8b8afb1
6644797
265e880
ca6ff28
f77a158
0f18955
6c324f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| Ava N. | ||
| July 5, 2016 | ||
|
|
||
| 1. Character arrays are arrays of characters. For example: char c='a';. Strings are arrays of characters. For example: char[10]="Hello World";. What this will do is assign char[0]='H', char[1]='e', char[2]='l', char[3]='l', char[4]='o', char[5]=' ', char[6]='W', char[7]='o', char[8]='r', char[9]='l', char[10]='d'. String assignment is only at declaration. Also, at the beginning of the program should use "#include <string.h>". | ||
|
|
||
| 2. The advantages of arrays in C are that it is very easy to store many integers in an array. The disadvantages of arrays in C are that the size can't change after the declaration. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right - can you think of anything else? |
||
|
|
||
| 3. The compiler does not implicitly generate the address of the first element of an array when an array appears as an expression. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! |
||
|
|
||
| 4. Two strings can be compared to see if they're equivalent in content by using strcmp(), and if the output is 0, then the strings are equal. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! :) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /*Ava N.*/ | ||
| /*Write a program that takes a string of characters and returns the frequency of each character in that string.*/ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| int main(){ | ||
| char input_string[98]; /*input string*/ | ||
| char alpha_string[51]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nicely done! You had one small warning, which caused an edge case to be missed. If the string had any capital Z's, then they weren't counted. The reason for this is because the size of the array should have been [52] instead! |
||
| int str_counter; | ||
| int alpha_counter; | ||
| int input_char; | ||
| int duplicate_count[51] = {0}; | ||
|
|
||
| printf("Enter a character string less than 100 characters:"); | ||
|
|
||
| scanf("%s",input_string); | ||
|
|
||
| /* printf("Your input string is: %s \n", input_string); | ||
| printf("input string 0 is: %c\n", input_string[0]); | ||
| printf("input_string 1 is: %c\n", input_string[1]); | ||
|
|
||
| */ | ||
|
|
||
| for (str_counter = 0; input_string[str_counter] != '\0'; str_counter++) | ||
| { | ||
|
|
||
| for (alpha_counter = 0; alpha_counter < 52; alpha_counter++) | ||
|
|
||
| { | ||
|
|
||
| if (input_string[str_counter] == alpha_string[alpha_counter]) | ||
| duplicate_count[alpha_counter]++; | ||
| } | ||
| } | ||
|
|
||
| /* printf("Letter a in duplicate counter array %d\n\n",duplicate_count[0]); */ | ||
|
|
||
|
|
||
| for (alpha_counter = 0; alpha_counter < 52; alpha_counter++) | ||
|
|
||
| { | ||
| if (duplicate_count[alpha_counter] >= 1) | ||
| printf("%c was found %d times.\n",alpha_string[alpha_counter],duplicate_count[alpha_counter]); | ||
| } | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /*Ava N.*/ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why you had the word print out at the end of the line |
||
| /*a program implementing the Hangman game.*/ | ||
| /*I am submitting but would like to still work on this*/ | ||
| #include <stdio.h> | ||
| int main(){ | ||
|
|
||
| char word[4]="sand"; | ||
| char correct[4]="----"; | ||
| char input_guess; | ||
| int guessed_letters=0; | ||
| int correct_guesses; | ||
| char guesses [8]; | ||
| /*incorrect_correct_guesses[0]='\0';*/ | ||
| int counter; | ||
| char loop='y'; | ||
| while(loop == 'y') { | ||
| printf("Guess a letter: %c \n", input_guess); | ||
| scanf("%c", &input_guess); | ||
| for(counter=0; counter<4; counter++){ | ||
| if(input_guess==word[counter]){ | ||
| guessed_letters++; | ||
| //guesses--; | ||
| printf("You guessed a correct letter. \n"); | ||
| printf("These are the number of blank spots you have: %s \n", correct); | ||
| scanf("%s", &correct[4]); | ||
| printf("You used %d guesses of 8 guesses.\n", guessed_letters); | ||
| } | ||
| //} | ||
| else/*(input_guess!=word[counter])*/{ | ||
| guessed_letters++; | ||
| printf("You guessed an incorrect letter. \n"); | ||
| //guesses--; | ||
| printf("These are the number of blank spots you have: %s \n", correct); | ||
| scanf("%s", &correct[4]); | ||
| printf("You used %d guesses of 8 guesses.\n", guessed_letters); | ||
| //break; | ||
| } | ||
| } | ||
| printf("This is the word you have come up with so far: %s \n", correct); | ||
| //printf("You used %d guesses of 8 guesses. \n", guessed_letters); | ||
| if (guesses >= 8); { | ||
| printf ("You have lost. Try again next time.\n"); | ||
| break; | ||
| if(word==0 /*&& blank_spots==0*/); { | ||
| printf ("You got the word, sand! You have won the game!\n"); | ||
| //break; | ||
| } | ||
| printf("do you want to play again? (y/n) "); | ||
| scanf(" %c", &loop); | ||
| if(loop != 'y') | ||
| loop='n'; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /*Ava N.*/ | ||
| /*Given an array of n integers where n > 1, return an array of same size an input array where at every index of the output array should contain the sum of all elements in the array except the element at the given index. You can have it so that the array is input by the user. */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nicely done! You did it very efficiently, so great job! The only thing is I would have included a prompt statement so the user knows to input numbers. I wasn't quite sure if you were just stuck in an infinite loop or what. |
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <time.h> | ||
| #include <string.h> | ||
|
|
||
| int main() { | ||
| int array_size = 5; /* Array size of 5 */ | ||
| int input_array[array_size]; | ||
| int output_array[array_size]; | ||
| int array_sum = 0; | ||
| int counter; | ||
|
|
||
| for(counter = 0; counter < array_size; counter++) | ||
| { | ||
| scanf("%d", &input_array[counter]); /* input numbers into array */ | ||
| } | ||
|
|
||
| for(counter = 0; counter < array_size; counter++) | ||
| { | ||
| array_sum +=input_array[counter]; /* calculate sum of numbers in array */ | ||
| } | ||
|
|
||
| printf("Sum of numbers in array is: %d\n", array_sum); | ||
|
|
||
| for(counter=0; counter < array_size; counter++) | ||
| { | ||
| output_array[counter] = array_sum-input_array[counter]; | ||
| printf("output for element %d is %d\n", counter, output_array[counter]); | ||
| } | ||
|
|
||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /*Ava N.*/ | ||
| /*Write a program that takes an array as input, removes duplicates, and outputs that array. */ | ||
| //I am submitting but would like to still work on this | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <time.h> | ||
|
|
||
| int main() { | ||
| int array_size = 5; /* Array size of 5 */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, not sure what's going on - did you make more progress? |
||
| int input_array[5]; | ||
| int output_array[5]; | ||
| int counter; | ||
| int check=0; | ||
| int duplicates=0; | ||
| int source=0; | ||
|
|
||
| printf("\nInsert %d numbers into the array: \n", array_size); | ||
|
|
||
| for(counter=0; counter<5; counter++) | ||
| { | ||
| scanf("%d", &input_array[counter]); /* input numbers into array */ | ||
|
|
||
| printf("\n Numbers in the array: %d \n", input_array[counter]); | ||
| break; | ||
| } | ||
|
|
||
| for(source=0; source < array_size; source++) | ||
| { | ||
| for(check=source+1; check<array_size ;check++) | ||
| { | ||
| if(input_array[source]==input_array[check]) | ||
| { | ||
| input_array[check]=input_array[source]; | ||
| input_array[check]='\0'; | ||
| printf("Duplicate found! %d %d \n",input_array[source],input_array[check]); | ||
| printf("source and source+1 are: %d %d \n",source,check); | ||
| printf("The number of duplicates are: %d", input_array[source]); | ||
| } | ||
| } | ||
| } | ||
| printf("\nEnd Duplicate Check\n"); | ||
|
|
||
| for(counter = 0; counter < (array_size-duplicates+1); counter++) | ||
| { | ||
| printf("%d", input_array[counter]); | ||
| } | ||
|
|
||
| printf("\n\nThe End!\n\n"); | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not exactly - a string is basically a character array, but with a NULL terminator as the last element.