Browse Source

week7

master
RinRi 1 year ago
parent
commit
9ab16f2603
13 changed files with 159 additions and 0 deletions
  1. +2
    -0
      week07/ex1.txt
  2. +14
    -0
      week07/ex2.c
  3. BIN
      week07/ex2.out
  4. +4
    -0
      week07/ex2.sh
  5. +50
    -0
      week07/ex3.c
  6. BIN
      week07/ex3.out
  7. +4
    -0
      week07/ex3.sh
  8. +67
    -0
      week07/ex4.c
  9. BIN
      week07/ex4.out
  10. +4
    -0
      week07/ex4.sh
  11. +10
    -0
      week07/ex5.c
  12. BIN
      week07/ex5.out
  13. +4
    -0
      week07/ex5.sh

+ 2
- 0
week07/ex1.txt View File

@@ -0,0 +1,2 @@
text data bss dec hex filename
53740 7528 1568 62836 f574 dwm

+ 14
- 0
week07/ex2.c View File

@@ -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);
}

BIN
week07/ex2.out View File


+ 4
- 0
week07/ex2.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex2.c -o ex2.out
./ex2.out

+ 50
- 0
week07/ex3.c View File

@@ -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;
}

BIN
week07/ex3.out View File


+ 4
- 0
week07/ex3.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex3.c -o ex3.out
./ex3.out

+ 67
- 0
week07/ex4.c View File

@@ -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;
}

BIN
week07/ex4.out View File


+ 4
- 0
week07/ex4.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex4.c -o ex4.out
./ex4.out

+ 10
- 0
week07/ex5.c View File

@@ -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);
}

BIN
week07/ex5.out View File


+ 4
- 0
week07/ex5.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex5.c -o ex5.out
./ex5.out

Loading…
Cancel
Save