@@ -0,0 +1,2 @@ | |||||
text data bss dec hex filename | |||||
53740 7528 1568 62836 f574 dwm |
@@ -0,0 +1,14 @@ | |||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
int main() { | |||||
int n; | |||||
scanf("%d", &n); | |||||
int *arr = malloc(sizeof(int) * n); | |||||
for (int i = 0; i < n; ++i) { | |||||
arr[i] = i; | |||||
printf("%d ", arr[i]); | |||||
} | |||||
printf("\n"); | |||||
free(arr); | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex2.c -o ex2.out | |||||
./ex2.out |
@@ -0,0 +1,50 @@ | |||||
#include <stdlib.h> | |||||
#include <stdio.h> | |||||
#include <time.h> | |||||
int main(){ | |||||
//Allows you to generate random number | |||||
srand(time(NULL)); | |||||
// Allows user to specify the original array size, stored in variable n1. | |||||
printf("Enter original array size:"); | |||||
int n1=0; | |||||
scanf("%d",&n1); | |||||
//Create a new array of n1 ints | |||||
int* a1 = malloc(sizeof(int) * n1); | |||||
int i; | |||||
for(i=0; i<n1; i++){ | |||||
//Set each value in a1 to 100 | |||||
a1[i]=100; | |||||
//Print each element out (to make sure things look right) | |||||
printf("%d ", a1[i]); | |||||
} | |||||
//User specifies the new array size, stored in variable n2. | |||||
printf("\nEnter new array size: "); | |||||
int n2=0; | |||||
scanf("%d",&n2); | |||||
//Dynamically change the array to size n2 | |||||
a1 = realloc(a1, sizeof(int) * n2); | |||||
//If the new array is a larger size, set all new members to 0. Reason: dont want to use uninitialized variables. | |||||
for (int i = n1; i < n2; ++i) { | |||||
a1[i] = 0; | |||||
} | |||||
for(i=0; i<n2;i++){ | |||||
//Print each element out (to make sure things look right) | |||||
printf("%d ", a1[i]); | |||||
} | |||||
printf("\n"); | |||||
//Done with array now, done with program :D | |||||
free(a1); | |||||
return 0; | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex3.c -o ex3.out | |||||
./ex3.out |
@@ -0,0 +1,67 @@ | |||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
#include <time.h> | |||||
// Assuming that previous size is provided, since there is no standard way to know the size of the allocated block | |||||
void* myrealloc(void* ptr, size_t prev_size, size_t new_size) { | |||||
if (ptr == NULL) prev_size = 0; | |||||
char *res = malloc(new_size); | |||||
size_t copy_size = (prev_size < new_size ? prev_size : new_size); | |||||
for (size_t i = 0; i < copy_size; ++i) { | |||||
res[i] = ((char*) ptr)[i]; | |||||
} | |||||
free(ptr); | |||||
return res; | |||||
} | |||||
// test myrealloc using ex3 | |||||
int main() { | |||||
//Allows you to generate random number | |||||
srand(time(NULL)); | |||||
// Allows user to specify the original array size, stored in variable n1. | |||||
printf("Enter original array size:"); | |||||
int n1=0; | |||||
scanf("%d",&n1); | |||||
//Create a new array of n1 ints | |||||
int* a1 = malloc(sizeof(int) * n1); | |||||
int i; | |||||
for(i=0; i<n1; i++){ | |||||
//Set each value in a1 to 100 | |||||
a1[i]=100; | |||||
//Print each element out (to make sure things look right) | |||||
printf("%d ", a1[i]); | |||||
} | |||||
//User specifies the new array size, stored in variable n2. | |||||
printf("\nEnter new array size: "); | |||||
int n2=0; | |||||
scanf("%d",&n2); | |||||
//Dynamically change the array to size n2 | |||||
a1 = myrealloc(a1, sizeof(int) * n1, sizeof(int) * n2); | |||||
//If the new array is a larger size, set all new members to 0. Reason: dont want to use uninitialized variables. | |||||
for (int i = n1; i < n2; ++i) { | |||||
a1[i] = 0; | |||||
} | |||||
for(i=0; i<n2;i++){ | |||||
//Print each element out (to make sure things look right) | |||||
printf("%d ", a1[i]); | |||||
} | |||||
printf("\n"); | |||||
//Done with array now, done with program :D | |||||
free(a1); | |||||
return 0; | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex4.c -o ex4.out | |||||
./ex4.out |
@@ -0,0 +1,10 @@ | |||||
#include <stdio.h> | |||||
int main() { | |||||
char *s; | |||||
char foo[] = "Hello World"; | |||||
s = foo; | |||||
printf("s is %s\n", s); | |||||
printf("s[0] is %c\n", s[0]); | |||||
return (0); | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex5.c -o ex5.out | |||||
./ex5.out |