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
14 changes: 14 additions & 0 deletions array1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<stdio.h>

int main(){
int x;
int sum=0;
printf("How many numbers are you inputing? \n");
scanf("%d", &x);
int array[x];
for(x=x-1;x>=0;x--){
scanf("%d",&array[x]);
sum=sum+array[x];
}
printf("The Sum is: %d \n" , sum);
}
30 changes: 30 additions & 0 deletions array2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<stdio.h>

int main(){
int i,j,k,n,x;
printf("How long is the array? \n");
scanf("%d",&n);
int l[n];
for (i=0;i<n;i++){
printf("Please type your next number: ");
scanf("%d",&l[i]);
}
for (x=0;x<2;x++){
for (i=0;i<n;i++){
for (j=i+1;j<n;j++){
if (l[i]==l[j]){
for (k=j;k<n-1;k++){
l[k]=l[k+1];
}
n--;
}
}
}
}
printf("After removing duplicates, your array is: \n");
for (i=0;i<n;i++){
printf("%d ",l[i]);
}
printf("\n");
}

9 changes: 9 additions & 0 deletions assignment4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1. A character array is a simple array while the string is a sequence of characters combined which can perform many different operations in the program.
2. There can be multiple data under one array so it is easy to put similar variables together. When performing and dealing with sequences and frequencies, array also benefits the programmer. The main disadvantage that I have faced in my programs is allocating a size for my arrays, because arrays have a fixed size. The bigger the array, the more memory it takes up. However, if there is too little, the program will not function.
3. Not sure, but maybe when it functions with the &operator, it does not necessarily use the first element, but instead, whichever element necessary?
4. Use strcmp to compare the two codes. eg. int strcmp(s1,s2);
if return =0l both lines are equal in length.
if return is negative, s1 is shorter and more efficient.
if return is positive, s1 is longer.

-- Oscar So --
134 changes: 134 additions & 0 deletions hangman.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<string.h>
#include<ctype.h>

void man(int x){
switch (x){
case 1:
printf("Number of Wrong letters: %d \n" , x);
printf(" _______ \n");
printf(" | | \n");
printf(" | O \n");
printf(" | \n");
printf(" | \n");
printf(" | \n");
printf("---------\n");
break;
case 2:
printf("Number of Wrong letters: %d \n" , x);
printf(" _______ \n");
printf(" | | \n");
printf(" | O \n");
printf(" | | \n");
printf(" | | \n");
printf(" | \n");
printf("---------\n");
break;
case 3:
printf("Number of Wrong letters: %d \n" ,x);
printf(" _______ \n");
printf(" | | \n");
printf(" | O \n");
printf(" | | \n");
printf(" | | \n");
printf(" | / \\\n");
printf("---------\n");
break;
case 4:
printf("Number of Wrong letters: %d \n" , x);
printf("_______ \n");
printf("| | \n");
printf("| X \n");
printf("| \\|/ \n");
printf("| | \n");
printf("| / \\\n");
printf("--------- \n");
break;
}
}
int main(void){
char rsp;
do{
srand(time(NULL));
char WordArray[][16] = {
"hitler",
"giraffe",
"olive",
"pickle",
"programming",
"diamond",
"california",
"trash",
"phone",
"luggage"
};
int r = rand()%10;
char *Word = WordArray[r];
char Word2[strlen(Word)+1];
strcpy(Word2,Word);
int WordLength = strlen(Word2);
char *DotWord = malloc(WordLength);
int t;
for (t=0;t<= WordLength; t++){
if (t==WordLength){
DotWord[t]='\0';
}else{
DotWord[t]='*';
}
}
printf("Welcome to Hangman! \n");
printf("To win the game, you must guess the word. \n");
printf("You will lose after four incorrect letter guesses. \n");
printf("All words are in lower case, please disable caps lock. \n");
printf("Your word is: %s \n", DotWord);
printf("Please Enter Your Guess:");
int mistakes=0;
int check=0;
int x,n=0;
char CharInput[100];
char* CorrectWord = Word2;
while((strcmp(DotWord, CorrectWord) !=0) && (mistakes <4)){
scanf("%c", &CharInput[n]);
if (CharInput[n] != '\n'){
for(x=0;x<WordLength;x++){
if(CorrectWord[x]==CharInput[n]){
DotWord[x] = CharInput[n];
check=1;
}
}
if(check == 0){
mistakes++;
printf("The Letter was incorrect.\n");
man(mistakes);
}else{
check=0;
printf("The Letter appeared in the word. \n");
}
printf("The word is: %s \n", DotWord);
printf("The words you have inputted are: \n");
for (int y = 0; y < n+1; y++){
printf("%c\n" , CharInput[y]);
}
if((strcmp(DotWord,CorrectWord)!=0)&&(mistakes<4)){
printf("Please Enter Your Guess:");
}
}
n++;
}
printf("--Your Result is-- \n");
if ((strcmp(DotWord, CorrectWord) ==0) && (mistakes <4)){
printf("Good Job, You have guessed the word! \n");
} else {
printf("You have guessed the wrong word. \n Sorry, the correct word was: %s \n", Word2);
}
printf("Do you want to play again? Y/N \n");
getchar();
rsp=getchar();
printf("-------------------------------------------- \n");
}while(rsp == 'Y' || rsp == 'y');
printf("Thank you for playing. \n");
return 0;
}
17 changes: 17 additions & 0 deletions string1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include<stdio.h>
int main(){
char string[1000];
int x;
int freq[256] = {0};
printf("Please type in your string. \n");
gets(string);
for (x=0; string[x]!='\0';x++){
freq[string[x]]++;
}
for (x=0;x<256;x++){
if (freq[x]!=0){
printf("%c: %d times \n", x, freq[x]);
}
}
}