-
Notifications
You must be signed in to change notification settings - Fork 16
Assignment4 Sheqi #12
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,29 @@ | ||
| #include <stdio.h> | ||
|
|
||
| int main(){ | ||
| int n,x; | ||
| int i=0; | ||
| int j=1; | ||
| int iarray[n]; | ||
| printf("Please type in an array:\n"); | ||
| scanf("%d",&iarray[n]); | ||
| for(i=0;i<n;i++){ | ||
| for(j=i+1;j<=n;j++){ | ||
| if(iarray[i]>iarray[j]){ | ||
| x=iarray[j]; | ||
| iarray[j]=iarray[i]; | ||
| iarray[i]=x; | ||
| } | ||
| continue; | ||
| }} | ||
| for(i<0;i<n;i++){ | ||
| if(i>0&&iarray[i-1]==iarray[i]){ | ||
| break;} | ||
| printf("%d ",iarray[i]); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| Assignment 4 Sheqi Zhang | ||
|
|
||
| 1. Discuss the differences between strings and a character array. | ||
| Answer: | ||
| A string is an array of chars, and an array is a group of characters within the same type. An array is static, and we can't change its size after declarition, while we can change the strings after declaeition. There is always a "\0" at the end of a string, while an array doesn't have that. | ||
|
|
||
| 2. Explain the advantages and disadvantages of arrays in C. | ||
| Answer: | ||
| Advantage: We can use it to store and use a group of similar data. | ||
| Disadvantage: We can't change the property like the size of the array, which will make our programming not convenient. | ||
|
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! |
||
|
|
||
| 3. When does the compiler not implicitly generate the address of the first element of an array? | ||
|
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. |
||
| Answer: | ||
| There are three situations: | ||
| 1. The array is a string literal initializer for a character array; | ||
| 2. The array is a operend of sizeof operator; | ||
| 3. The array is a operand of "&" operator. | ||
|
|
||
| 4. How would two strings be compared to see if they’re equivalent in content? | ||
|
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! |
||
| Answer: | ||
| Using the string function: strcmp(string1,string 2); | ||
| It is comparing string 2 to string 1. | ||
| If it returns 0, the two string are equivalent. | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <time.h> | ||
|
|
||
|
|
||
| int main(){ | ||
| srand(time(0)); | ||
| char r =rand(); | ||
| int length = rand(); | ||
| length<15; | ||
| printf("This word has \"%d\" characters", length); | ||
| char word[length]; | ||
| char guess[length]; | ||
| int x = 0; | ||
| char letter; | ||
| while(x<4){ | ||
| scanf("%c",&letter); | ||
| if(letter==word[x]){ | ||
| guess[x]==letter; | ||
| printf("Please guess the next letter!\n"); | ||
| } | ||
| else | ||
| { | ||
| printf("Try again!\n"); | ||
| } | ||
| ++x; | ||
| continue; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| int main(){ | ||
| int n; | ||
| int i=0; | ||
| int sum=0; | ||
| int inumbers[n]; | ||
| int inew[n]; | ||
|
|
||
| printf("Please enter a group of number:"); | ||
| scanf("%u ",&inumbers[n]); | ||
|
|
||
| while(i<n){ | ||
| sum += (int) inumbers[i]; | ||
| // printf("sum=%d",sum); | ||
| i++; | ||
| // printf("i=%d",i); | ||
| continue; | ||
| } | ||
| //printf("sum=%d",sum); | ||
|
|
||
| inew[i]= sum - inumbers[i]; | ||
| printf("%u",inew[n]); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #include <stdio.h> | ||
|
|
||
| int main(){ | ||
| int n; | ||
| int i=0; | ||
| int j=0; | ||
| char ia[n]; | ||
| char jletter[52]; | ||
| int jtimes[52]={0}; | ||
| n<=100; | ||
| printf("Please type in an array with only letters:\n"); | ||
| scanf("%c",&ia[n]); | ||
| for(i=0;i<52;i++){ | ||
| for(j=0;j<52;j++){ | ||
| if(ia[i]==jletter[j]) | ||
| ++jtimes[j]; | ||
| continue; | ||
| } | ||
| printf("\"%c\":\"%d\"\n",jletter[j],jtimes[j]); | ||
| continue; | ||
| } | ||
| 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.
right!