Add c_projects
This commit is contained in:
parent
e92aa147fa
commit
eb4ffb54cb
BIN
c_projects/1darraysinc/a.out
Executable file
BIN
c_projects/1darraysinc/a.out
Executable file
Binary file not shown.
19
c_projects/1darraysinc/main.c
Normal file
19
c_projects/1darraysinc/main.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
c_projects/array_reverse/a.out
Executable file
BIN
c_projects/array_reverse/a.out
Executable file
Binary file not shown.
24
c_projects/array_reverse/main.c
Normal file
24
c_projects/array_reverse/main.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
c_projects/arrays_and_strings/a.out
Executable file
BIN
c_projects/arrays_and_strings/a.out
Executable file
Binary file not shown.
41
c_projects/arrays_and_strings/main.c
Normal file
41
c_projects/arrays_and_strings/main.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
BIN
c_projects/dynamic_arrays_in_c/a.out
Executable file
BIN
c_projects/dynamic_arrays_in_c/a.out
Executable file
Binary file not shown.
80
c_projects/dynamic_arrays_in_c/main.c
Normal file
80
c_projects/dynamic_arrays_in_c/main.c
Normal file
@ -0,0 +1,80 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
BIN
c_projects/permutations_of_strings/a.out
Executable file
BIN
c_projects/permutations_of_strings/a.out
Executable file
Binary file not shown.
14
c_projects/permutations_of_strings/main.c
Normal file
14
c_projects/permutations_of_strings/main.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(){
|
||||
char* a = "ab";
|
||||
char* b = "ac";
|
||||
|
||||
if ( strcmp(a,b) ){
|
||||
printf("hi");
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
c_projects/printing_tokens/a.out
Executable file
BIN
c_projects/printing_tokens/a.out
Executable file
Binary file not shown.
26
c_projects/printing_tokens/main.c
Normal file
26
c_projects/printing_tokens/main.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
BIN
c_projects/printpattern/a.out
Executable file
BIN
c_projects/printpattern/a.out
Executable file
Binary file not shown.
64
c_projects/printpattern/main.c
Normal file
64
c_projects/printpattern/main.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
|
||||
|
||||
}
|
BIN
c_projects/test/a.out
Executable file
BIN
c_projects/test/a.out
Executable file
Binary file not shown.
1
c_projects/test/sources
Normal file
1
c_projects/test/sources
Normal file
@ -0,0 +1 @@
|
||||
http://web.mit.edu/15.053/www/AMP.htm
|
20
c_projects/test/test.c
Normal file
20
c_projects/test/test.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
c_projects/test/test.o
Normal file
BIN
c_projects/test/test.o
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user