diff --git a/c_projects/1darraysinc/a.out b/c_projects/1darraysinc/a.out new file mode 100755 index 0000000..55ab7be Binary files /dev/null and b/c_projects/1darraysinc/a.out differ diff --git a/c_projects/1darraysinc/main.c b/c_projects/1darraysinc/main.c new file mode 100644 index 0000000..28e5363 --- /dev/null +++ b/c_projects/1darraysinc/main.c @@ -0,0 +1,19 @@ +#include +#include + +int main() { + int n, i, sum = 0; + + scanf("%d", &n); + int *arr = malloc( n * sizeof *arr); + + for ( i = 0; i < n; i++ ){ + scanf("%d", &arr[i]); + sum = sum + arr[i]; + } + printf("%d", sum); + + free(arr); + + return 0; +} diff --git a/c_projects/array_reverse/a.out b/c_projects/array_reverse/a.out new file mode 100755 index 0000000..43d30f5 Binary files /dev/null and b/c_projects/array_reverse/a.out differ diff --git a/c_projects/array_reverse/main.c b/c_projects/array_reverse/main.c new file mode 100644 index 0000000..1a50fec --- /dev/null +++ b/c_projects/array_reverse/main.c @@ -0,0 +1,24 @@ +#include +#include + +int main(){ + int num, *arr, i; + scanf("%d", &num); + arr = (int*) malloc(num * sizeof(int)); + for(i = 0; i < num; i++) { + scanf("%d", arr + i); + } + + + /* Write the logic to reverse the array. */ + for ( i = 0; i < num/2; i++){ + int temp = arr[i]; + arr[i] = arr[num-i-1]; + arr[num-i-1] = temp; + } + + for(i = 0; i < num; i++) + printf("%d ", *(arr + i)); + free(arr); + return 0; +} diff --git a/c_projects/arrays_and_strings/a.out b/c_projects/arrays_and_strings/a.out new file mode 100755 index 0000000..bf29453 Binary files /dev/null and b/c_projects/arrays_and_strings/a.out differ diff --git a/c_projects/arrays_and_strings/main.c b/c_projects/arrays_and_strings/main.c new file mode 100644 index 0000000..1c95058 --- /dev/null +++ b/c_projects/arrays_and_strings/main.c @@ -0,0 +1,41 @@ +#include +#include +#include + +#define NUM 10 + +void zero_arr(int *arr){ + int i; + for (i = 0; i < NUM; i++){ + arr[i] = 0; + } +} + +void count_nums(char *input, int *count){ + int i; + for ( i = 0; i < strlen(input); i++ ){ + count[input[i] - '0']++; + } + +} + +void print_arr(int *count){ + int i; + for ( i = 0; i < NUM; i++ ){ + printf("%d ", count[i]); + } +} + +int main(){ + int count[NUM]; + + char *input = malloc(1024 * sizeof *input); + scanf("%[^\n]", input); + input = realloc(input, strlen(input) + 1); + + zero_arr(count); + count_nums(input, count); + print_arr(count); + + return 0; +} diff --git a/c_projects/dynamic_arrays_in_c/a.out b/c_projects/dynamic_arrays_in_c/a.out new file mode 100755 index 0000000..73cc022 Binary files /dev/null and b/c_projects/dynamic_arrays_in_c/a.out differ diff --git a/c_projects/dynamic_arrays_in_c/main.c b/c_projects/dynamic_arrays_in_c/main.c new file mode 100644 index 0000000..8b1a24c --- /dev/null +++ b/c_projects/dynamic_arrays_in_c/main.c @@ -0,0 +1,80 @@ +#include +#include + +#define max_books 1100 + +/* + * This stores the total number of books in each shelf. + */ +int* total_number_of_books; + +/* + * This stores the total number of pages in each book of each shelf. + * The rows represent the shelves and the columns represent the books. + */ +int** total_number_of_pages; + +int main() +{ + int i; + int total_number_of_shelves; + scanf("%d", &total_number_of_shelves); + + int total_number_of_queries; + scanf("%d", &total_number_of_queries); + + total_number_of_pages = (int**)malloc( total_number_of_shelves * sizeof(int*) ); + total_number_of_books = (int*)malloc( total_number_of_shelves * sizeof(int) ); + + for ( i = 0; i < total_number_of_shelves; i++ ){ + *( total_number_of_books + i ) = 0; + } + + for ( i = 0; i < total_number_of_shelves; i++ ){ + *(total_number_of_pages + i) = malloc( max_books * sizeof( *(total_number_of_pages) ) ); + } + + while (total_number_of_queries--) { + int type_of_query; + scanf("%d", &type_of_query); + + if (type_of_query == 1) { + /* + * Process the query of first type here. + */ + int x, y; + scanf("%d %d", &x, &y); + + if ( x < total_number_of_shelves ){ + total_number_of_pages[x][*(total_number_of_books + x)] = y; + (*(total_number_of_books + x))++; + } + + + } else if (type_of_query == 2) { + int x, y; + scanf("%d %d", &x, &y); + printf("%d\n", *(*(total_number_of_pages + x) + y)); + } else { + int x; + scanf("%d", &x); + printf("%d\n", *(total_number_of_books + x)); + } + } + + if (total_number_of_books) { + free(total_number_of_books); + } + + for (int i = 0; i < total_number_of_shelves; i++) { + if (*(total_number_of_pages + i)) { + free(*(total_number_of_pages + i)); + } + } + + if (total_number_of_pages) { + free(total_number_of_pages); + } + + return 0; +} diff --git a/c_projects/permutations_of_strings/a.out b/c_projects/permutations_of_strings/a.out new file mode 100755 index 0000000..06a75a6 Binary files /dev/null and b/c_projects/permutations_of_strings/a.out differ diff --git a/c_projects/permutations_of_strings/main.c b/c_projects/permutations_of_strings/main.c new file mode 100644 index 0000000..2a42b83 --- /dev/null +++ b/c_projects/permutations_of_strings/main.c @@ -0,0 +1,14 @@ +#include +#include + +int main(){ + char* a = "ab"; + char* b = "ac"; + + if ( strcmp(a,b) ){ + printf("hi"); + + } + + return 0; +} diff --git a/c_projects/printing_tokens/a.out b/c_projects/printing_tokens/a.out new file mode 100755 index 0000000..14c32e1 Binary files /dev/null and b/c_projects/printing_tokens/a.out differ diff --git a/c_projects/printing_tokens/main.c b/c_projects/printing_tokens/main.c new file mode 100644 index 0000000..ae54bb6 --- /dev/null +++ b/c_projects/printing_tokens/main.c @@ -0,0 +1,26 @@ +#include +#include +#include + + + +int main() { + char *s; + s = malloc(1024 * sizeof(char)); + scanf("%[^\n]", s); + s = realloc(s, strlen(s) + 1); + //Write your logic to print the tokens of the sentence here. + char *ptr = s; + while ( *ptr != '\0' ){ + if ( *ptr == ' ' ){ + printf("\n"); + } + else{ + printf("%c", *ptr); + } + ptr++; + } + printf("\n"); + + return 0; +} diff --git a/c_projects/printpattern/a.out b/c_projects/printpattern/a.out new file mode 100755 index 0000000..7c337eb Binary files /dev/null and b/c_projects/printpattern/a.out differ diff --git a/c_projects/printpattern/main.c b/c_projects/printpattern/main.c new file mode 100644 index 0000000..f60b9ac --- /dev/null +++ b/c_projects/printpattern/main.c @@ -0,0 +1,64 @@ +#include +#include + +void print_array(int *arr){ + int *ptr = arr; + while ( *ptr != '\0' ){ + printf("%d ", *ptr); + ptr++; + } + printf("\n"); +} + +// starting at index 1, decrement by one until index size - 1 +// then move starting index up by one and have the stop address move back one and repeat +void first_half(int *arr, int n, int arr_size){ + int i; + for ( i = 0; i < (n - 1); i++ ){ + int *ptr = &arr[i]; + while ( ptr != &arr[(arr_size - 2) - i] ){ + ptr++; + (*ptr)--; + } + print_array(arr); + } +} + +// starting at the center, increment by one +// then expand the contraints by moving the start index back one and end index up one +// increment and repeat +void second_half(int *arr, int n, int arr_size){ + int i; + for ( i = (n - 1); i > 0; i-- ){ + int *ptr = &arr[i]; + while ( ptr != &arr[(arr_size) - i] ){ + (*ptr)++; + ptr++; + } + print_array(arr); + } +} + +int main(){ + int n, i, arr_size; + scanf("%d", &n); + // Complete the code to print the pattern. + + // Set array size to be able to count to 1 then back up to passed number + arr_size = (2 * n) - 1; + int *arr = malloc(arr_size * sizeof *arr); + + // Fill the array with the passed number and print array + for ( i = 0; i < arr_size; i++ ){ + arr[i] = n; + } + print_array(arr); + + // complete the first half of pattern + first_half(arr, n, arr_size); + second_half(arr, n, arr_size); + + return 0; + + +} diff --git a/c_projects/test/a.out b/c_projects/test/a.out new file mode 100755 index 0000000..464560c Binary files /dev/null and b/c_projects/test/a.out differ diff --git a/c_projects/test/sources b/c_projects/test/sources new file mode 100644 index 0000000..c328f25 --- /dev/null +++ b/c_projects/test/sources @@ -0,0 +1 @@ +http://web.mit.edu/15.053/www/AMP.htm diff --git a/c_projects/test/test.c b/c_projects/test/test.c new file mode 100644 index 0000000..095a61c --- /dev/null +++ b/c_projects/test/test.c @@ -0,0 +1,20 @@ +#include + +int main(){ + printf("Hello, World\n"); + int x = 40; + int y = 50; + int i,z = 0; + + for ( i = 0; i < 5; i++ ){ + int temp = x; + x = y; + z = temp + x + z; + y = temp; + + printf("%d\n", z); + } + + + return 0; +} diff --git a/c_projects/test/test.o b/c_projects/test/test.o new file mode 100644 index 0000000..a55ef58 Binary files /dev/null and b/c_projects/test/test.o differ