forked from freedomzx/CS-211
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9010ba2
Showing
98 changed files
with
36,339 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall -Werror -fsanitize=address -std=c11 eighth.c -o eighth | ||
clean: | ||
rm -f *o eighth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall -Werror -fsanitize=address eighth.c -o eighth | ||
clean: | ||
rm -f *o eighth |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
struct BSTNode{ | ||
int value; | ||
int height; | ||
struct BSTNode* left; | ||
struct BSTNode* right; | ||
}; | ||
|
||
struct BSTNode* root = NULL; | ||
|
||
void insert(int); | ||
void search(int); | ||
struct BSTNode* allocate_treenode(int); | ||
void print_tree(struct BSTNode* root); | ||
void freeMyFellaTree(struct BSTNode* node); | ||
|
||
int main(int argc, char** argv){ | ||
FILE* fp = fopen(argv[1], "r"); | ||
if(fp == NULL){ | ||
printf("error"); //file doesn't exist | ||
return 0; | ||
} | ||
|
||
while(!feof(fp)){ | ||
char letter; | ||
int number; | ||
fscanf(fp, "%c %d\n", &letter, &number); | ||
if(letter == 'i') insert(number); | ||
if(letter == 's') search(number); | ||
} | ||
fclose(fp); | ||
freeMyFellaTree(root); | ||
return 0; | ||
} | ||
|
||
void freeMyFellaTree(struct BSTNode* node){ | ||
if(node != NULL){ | ||
freeMyFellaTree(node->left); | ||
freeMyFellaTree(node->right); | ||
free(node); | ||
} | ||
} | ||
|
||
void insert(int num){ | ||
int heightCounter = 1; | ||
int cmp = 0; | ||
struct BSTNode* current = root; | ||
struct BSTNode* parent = NULL; | ||
while(current != NULL){ | ||
if(num == current->value){ | ||
printf("duplicate\n"); | ||
return; | ||
} | ||
else if(num < current->value) cmp = -1; | ||
else cmp = 1; | ||
parent = current; | ||
if(cmp == -1) current = current->left; | ||
else current = current->right; | ||
heightCounter += 1; | ||
} | ||
|
||
struct BSTNode* toInsert = allocate_treenode(num); | ||
toInsert->height = heightCounter; | ||
|
||
if(parent == NULL) root = toInsert; | ||
else if(cmp == -1) parent->left = toInsert; | ||
else parent->right = toInsert; | ||
printf("inserted %d\n", heightCounter); | ||
} | ||
|
||
void search(int num){ | ||
struct BSTNode* current = root; | ||
|
||
while(current != NULL){ | ||
if(num == current->value){ | ||
printf("present %d\n", current->height); | ||
return; | ||
} | ||
if(num < current->value) current = current->left; | ||
else current = current->right; | ||
} | ||
|
||
printf("absent\n"); | ||
} | ||
|
||
struct BSTNode* allocate_treenode(int value){ | ||
struct BSTNode* temp = malloc(sizeof(struct BSTNode)); | ||
temp->value = value; | ||
temp->height = 1; | ||
temp->left = NULL; | ||
temp->right = NULL; | ||
return temp; | ||
} | ||
|
||
void print_tree(struct BSTNode* root){ | ||
if(root == NULL) return; | ||
printf("value: %d\n", root->value); | ||
print_tree(root->left); | ||
print_tree(root->right); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall -Werror -fsanitize=address -std=c11 fifth.c -o fifth | ||
clean: | ||
rm -f *o fifth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall -Werror -fsanitize=address fifth.c -o fifth | ||
clean: | ||
rm -f *o fifth |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
int** allocate_matrix(int, int); | ||
void free_matrix(int**, int); | ||
int hasPotential(int**, int); | ||
int magicRows(int**, int); | ||
int magicColumns(int**, int); | ||
int magicDiags(int**, int); | ||
|
||
int main(int argc, char** argv){ | ||
FILE* fp = fopen(argv[1], "r"); | ||
if(fp == NULL) return 0; //i think the assignment said assume all correct inputs but just in case! | ||
int dims, y, x; | ||
fscanf(fp, "%d\n", &dims); | ||
int** matrix = allocate_matrix(dims, dims); | ||
for(y = 0; y < dims; y++){ | ||
for(x = 0; x < dims; x++){ | ||
int toAdd; | ||
fscanf(fp, "%d\t", &toAdd); | ||
matrix[y][x] = toAdd; | ||
} | ||
fscanf(fp, "\n"); | ||
} | ||
|
||
int p, mr, mc, md; | ||
p = hasPotential(matrix, dims); | ||
mr = magicRows(matrix, dims); | ||
mc = magicColumns(matrix, dims); | ||
md = magicDiags(matrix, dims); | ||
if(p == 1 && mr != 0 && mr == mc && mc == md){ | ||
printf("magic"); | ||
return 0; | ||
} | ||
printf("not-magic"); | ||
free_matrix(matrix, dims); | ||
fclose(fp); | ||
return 0; | ||
} | ||
|
||
int hasPotential(int** arr1, int dimens){ | ||
int squared = dimens * dimens; | ||
int numbers[squared]; | ||
int i, j, h; | ||
|
||
for(i = 0; i < squared; i++){ | ||
numbers[i] = 0; | ||
} | ||
|
||
h = 0; | ||
for(i = 0; i < dimens; i++){ | ||
for(j = 0; j < dimens; j++){ | ||
numbers[h] += arr1[i][j]; | ||
h++; | ||
} | ||
} | ||
|
||
/*for(i = 0; i < squared; i++){ | ||
printf("%d ", numbers[i]); | ||
} | ||
printf("\n"); //PRINT HERE*/ | ||
|
||
for(i = 0; i < squared; i++){ | ||
for(j = 0; j < squared; j++){ | ||
if(numbers[j] > numbers[i]){ | ||
int temp = numbers[i]; | ||
numbers[i] = numbers[j]; | ||
numbers[j] = temp; | ||
} | ||
} | ||
} //sort the array | ||
/*for(i = 0; i < squared; i++){ | ||
printf("%d ", numbers[i]); | ||
} | ||
printf("\n"); //PRINT HERE*/ | ||
for(i = 0; i < squared; i++){ | ||
if(numbers[i] == 0){ | ||
continue; | ||
} | ||
else if(numbers[i] == numbers[i-1]){ | ||
return 0; | ||
break; | ||
} | ||
else if(numbers[i] > squared || numbers[i-1] > squared){ | ||
return 0; | ||
break; | ||
} | ||
else if(abs(numbers[i] - numbers[i-1]) > 1){ | ||
return 0; | ||
break; | ||
} | ||
} | ||
//printf("it has potential\n"); | ||
return 1; | ||
} | ||
|
||
int magicDiags(int** arr1, int dimens){ | ||
int sum, i, j; | ||
sum = 0; | ||
for(i = dimens-1; i > 0; i--){ | ||
for(j = 0; j < dimens; j++){ | ||
sum += arr1[i][j]; | ||
} | ||
} | ||
|
||
int temp = 0; | ||
for(i = 0; i < dimens; i++){ | ||
for(j = dimens-1; j > 0; j--){ | ||
temp += arr1[i][j]; | ||
} | ||
} | ||
if(sum != temp){ | ||
return 0; | ||
} | ||
//printf("%d\n", sum); | ||
return sum/2; | ||
} | ||
|
||
int magicRows(int** arr1, int dimens){ | ||
int sum, i, j; | ||
sum = 0; | ||
|
||
for(i = 0; i < dimens; i++){ | ||
for(j = 0; j < dimens; j++){ | ||
sum += arr1[i][j]; | ||
} | ||
break; | ||
} | ||
|
||
for(i = 0; i < dimens; i++){ | ||
int temp = 0; | ||
for(j = 0; j < dimens; j++){ | ||
temp += arr1[j][i]; | ||
} | ||
if(temp != sum){ | ||
return 0; | ||
break; | ||
} | ||
} | ||
//printf("%d\n", sum); | ||
return sum; | ||
} | ||
|
||
int magicColumns(int** arr1, int dimens){ | ||
int sum, i, j; | ||
sum = 0; | ||
for(i = 0; i < dimens; i++){ | ||
sum += arr1[i][0]; | ||
} | ||
for(i = 0; i < dimens; i++){ | ||
int temp = 0; | ||
for(j = 0; j < dimens; j++){ | ||
temp += arr1[i][j]; | ||
} | ||
if(temp != sum){ | ||
return 0; | ||
break; | ||
} | ||
} | ||
//printf("%d\n", sum); | ||
return sum; | ||
} | ||
|
||
int** allocate_matrix(int rows, int cols){ | ||
int** ret_val = (int**)malloc(rows * sizeof(int*)); | ||
int i; | ||
for(i = 0; i < rows; i++){ | ||
ret_val[i] = (int*)malloc(cols * sizeof(int)); | ||
} | ||
return ret_val; | ||
} | ||
|
||
void free_matrix(int** arr1, int rows){ | ||
int i; | ||
for(i = 0; i < rows; i++){ | ||
free(arr1[i]); | ||
} | ||
free(arr1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall -Werror -fsanitize=address -std=c11 first.c -o first | ||
clean: | ||
rm -f *o first |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall -Werror -fsanitize=address first.c -o first | ||
clean: | ||
rm -f *o first |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
int isprime(int num); | ||
|
||
int main(int argc, char** argv){ | ||
//getting first line, how many numbers there are in the file | ||
FILE* fp = fopen(argv[1], "r"); | ||
int numOfNums = 0; | ||
fscanf(fp, "%d\n", &numOfNums); | ||
//determining whether or not each number is a right truncatable prime | ||
|
||
int i = 0; | ||
for(i = 0; i < numOfNums; i++){ | ||
int temp = 0; | ||
int tempPrime = 0; | ||
fscanf(fp, "%d\n", &temp); | ||
//if number is single digit | ||
if(temp < 10 && temp > 0){ | ||
tempPrime = isprime(temp); | ||
if(tempPrime == 1) printf("yes\n"); | ||
else printf("no\n"); | ||
continue; | ||
} | ||
//numbers longer than single digit | ||
while(temp / 10 != 0){ | ||
tempPrime = isprime(temp); | ||
if(tempPrime == 0) break; | ||
else temp = temp / 10; | ||
} | ||
if(temp < 10 && temp > 0){ | ||
tempPrime = isprime(temp); | ||
if(tempPrime == 1) printf("yes\n"); | ||
else printf("no\n"); | ||
} | ||
else{ | ||
if(tempPrime == 0) printf("no\n"); | ||
else printf("yes\n"); | ||
} | ||
} | ||
/* | ||
int test = isprime(4); | ||
printf("%d\n", test);*/ | ||
|
||
fclose(fp); //closing the file | ||
return 0; | ||
} | ||
|
||
int isprime(int num){ | ||
if(num == 1) return 0; | ||
int i = 0; | ||
for(i = 2; i < num; i++){ | ||
if(num % i == 0 && i != num) return 0; | ||
} | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall -Werror -fsanitize=address -std=c11 fourth.c -o fourth | ||
clean: | ||
rm -f *o fourth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall -Werror -fsanitize=address fourth.c -o fourth | ||
clean: | ||
rm -f *o fourth |
Binary file not shown.
Oops, something went wrong.