-
Couldn't load subscription status.
- Fork 16
Aly Milich Assignment 4 #5
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?
Changes from all commits
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,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. | ||
|
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 - they also don't allow you to use elements of different types and their sizes can't be modified. |
||
|
|
||
| 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. | ||
|
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. So the short answer is that a compiler doesn't generate the address whenever it appears in an expression. |
||
|
|
||
| 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. | ||
|
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! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include <stdio.h> | ||
| /*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; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #include <stdio.h> | ||
|
|
||
| /* 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; | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include <stdio.h> | ||
|
|
||
| /*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; i<amount; i++){ | ||
| scanf("%c", &input[i]); | ||
| input[i] = getchar(); | ||
| } | ||
|
|
||
| /*alphabetize array*/ | ||
| for(i=0; i<amount; i++){ | ||
| for(j=0; j<amount-1; j++){ | ||
| if(input[j]>input[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<amount; i++){ | ||
| if(input[i] == input[i-1]) | ||
| counter++; | ||
| else if(input[i] != input[i+1]){ | ||
| resets++; | ||
| number[resets-1] = number[resets-1] + counter; | ||
| letter[resets] = input[i]; | ||
| counter = 1; | ||
| } | ||
|
|
||
| number[amount-1] = counter; | ||
| } | ||
|
|
||
| for(i=0; i<amount; i++) | ||
| printf("%c, %d\n", letter[i], number[i]); | ||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include <stdio.h> | ||
|
|
||
| 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; | ||
|
|
||
| } |
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.
the main difference is that a string is a char array but with a null terminator as the last element!