Moved files of Assignment 1, Started Assignment 2

master
Nigel Barink 2020-10-18 21:39:34 +02:00
parent 74c77ecd89
commit 5787d314e0
22 changed files with 205 additions and 0 deletions

33
Assignment 2/exercise1.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
void print_memory_layout (char *data, int size );
int main (void)
{
short i = 0x1234;
char x = -127;
long sn1 = 1489156464;
long sn2 = 1541654916;
int y[2] = {0x11223344,0x44332211};
printf("Size in bytes of i: %d\n", sizeof (i));
printf("Size in bytes of x: %d\n", sizeof(x));
printf("Size in bytes of sn1: %d\n", sizeof(sn1));
printf("Size in bytes of sn2: %d\n", sizeof(sn2));
printf("Size in bytes of y: %d\n", sizeof(y));
// Print memory layout
printf( "Address\t\tContent (hex)\tContent(dec)\n");
print_memory_layout((char *)&i , sizeof(i));
print_memory_layout((char *)&x , sizeof(x));
print_memory_layout((char *)&sn1, sizeof(sn1));
print_memory_layout((char *)&sn2, sizeof(sn2));
print_memory_layout((char *)&sn1, sizeof(y));
}
void print_memory_layout (char *data , int size){
for( int i = 0 ; i < size ; i++ ){
printf("0x%08x\t0x%08x\t\%d\n", &data[i], data[i],data[i]);
}
}

View File

@ -0,0 +1,33 @@
Size in bytes of i: 2
Size in bytes of x: 1
Size in bytes of sn1: 8
Size in bytes of sn2: 8
Size in bytes of y: 8
Address Content (hex) Content(dec)
0x69caf3ae 0x00000034 52
0x69caf3af 0x00000012 18
0x69caf3ad 0xffffff81 -127
0x69caf3b0 0x00000070 112
0x69caf3b1 0xffffffb9 -71
0x69caf3b2 0xffffffc2 -62
0x69caf3b3 0x00000058 88
0x69caf3b4 0x00000000 0
0x69caf3b5 0x00000000 0
0x69caf3b6 0x00000000 0
0x69caf3b7 0x00000000 0
0x69caf3b8 0xffffff84 -124
0x69caf3b9 0xffffffc9 -55
0x69caf3ba 0xffffffe3 -29
0x69caf3bb 0x0000005b 91
0x69caf3bc 0x00000000 0
0x69caf3bd 0x00000000 0
0x69caf3be 0x00000000 0
0x69caf3bf 0x00000000 0
0x69caf3b0 0x00000070 112
0x69caf3b1 0xffffffb9 -71
0x69caf3b2 0xffffffc2 -62
0x69caf3b3 0x00000058 88
0x69caf3b4 0x00000000 0
0x69caf3b5 0x00000000 0
0x69caf3b6 0x00000000 0
0x69caf3b7 0x00000000 0

37
Assignment 2/exercise2.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdbool.h>
int main (int argc , char **args){
// How many bytes does the bool type use?
bool test = true;
printf("Size of bool in bytes: %d\n", sizeof(test));
// Hex of a boolean when its value is set to true
printf("Value of bool (State: True): 0x%08x\n", test);
// Hex of a boolean when its value is set to false
test = false ;
printf("Value of bool (state: False): 0x%08x\n", test);
test = 255;
if(test){
printf("Bool with a value of 255 is interpreted as true\n");
}
else{
printf("Bool with a value of 255 is interpreted as false\n");
}
test = -125;
if(test){
printf("Bool with a value of -125 is interpreted as true\n");
}
else{
printf("Bool with a value of -125 is interpreted as false\n");
}
test = 0;
if(test){
printf("Bool with a value of 0 is interpreted as true\n");
}
else{
printf("Bool with a value of 0 is interpreted as false\n");
}
}

11
Assignment 2/exercise3.c Normal file
View File

@ -0,0 +1,11 @@
void addvector(int *r, const int *a, const int *b, unsigned int len)
{
unsigned int i;
for(i=0;i<len;i++)
{
int *item = (r + i);
int a_value = *(a + i);
int b_value = *(b + i);
(*item) = a_value + b_value ;
}
}

2
Assignment 2/exercise3.h Normal file
View File

@ -0,0 +1,2 @@
void addvector(int *r, const int *a, const int *b, unsigned int len);

22
Assignment 2/main.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include "exercise3.h"
int main(void)
{
int a[10], b[10], r[10];
unsigned int i;
for(i=0;i<10;i++)
{
a[i] = b[i] = i;
}
addvector(r, a, b, 10);
for(i=0;i<10;i++)
{
printf("%d %d %d\n", a[i], b[i], r[i]);
}
return 0;
}

17
Assignment 2/memcmp.c Normal file
View File

@ -0,0 +1,17 @@
#include<stdlib.h>
// implementation of memcmp
int memcmp ( void *a, void *b, size_t n){
unsigned int i;
for ( i = 0; i < n ; i++) {
char *a1 = a + i;
char *b1 = b + i;
char return_value = *a1 - *b1;
if( return_value != 0)
return (int)return_value;
}
return 0;
}

2
Assignment 2/memcmp.h Normal file
View File

@ -0,0 +1,2 @@
int memcmp ( void *a, void *b, size_t n);

View File

@ -0,0 +1,48 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void print_array(int *array, size_t size);
int main ( int argc , char **argv )
{
// Test cases
int *test1 = malloc(3 * sizeof(int));
int *test1_2 = malloc(3 * sizeof(int));
*(test1) = *(test1_2) = 1;
*(test1 + 1) = *(test1_2 + 1) = 2;
*(test1 + 2) = *(test1_2 + 2) = 3;
int result;
result = memcmp(test1, test1_2, 0 );
printf ("Result should: 0 got %d\n", result);
result = memcmp(test1, test1_2, 3);
printf("Result should: 0 got %d\n", result);
*(test1 + 2) = 5;
printf("Array 1:");
print_array(test1, 3);
printf("Array 2:");
print_array(test1_2, 3);
result = memcmp(test1, test1_2, 3);
printf("Result should: 0 got %d\n", result);
free (test1);
free(test1_2);
}
void print_array(int *array, size_t size){
for( int i =0; i < size; i++){
printf( "%d\n", *(array+i));
}
}