Skip to content

Commit

Permalink
finished mini c challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
RRickyH committed Nov 14, 2024
1 parent 6df21af commit b9105af
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
Binary file added .challenge.c.swp
Binary file not shown.
Binary file added challenge
Binary file not shown.
94 changes: 78 additions & 16 deletions challenge.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Question 0
// Include the challenge.h header file
//-------------------------------------------------------------------------

#include "challenge.h"

//-------------------------------------------------------------------------
// Question 1
Expand All @@ -17,14 +17,17 @@
// of `q1B`should be initialized to 1.
//-------------------------------------------------------------------------

int q1A = 0;
int q1B = 1;

//-------------------------------------------------------------------------
// Question 2
// Declare a global variable for an integer array of size 10. The name of
// the array should be `q2Array`. The size should be defined by a macro
// named `Q2_ARRAY_SIZE`.
//-------------------------------------------------------------------------

#define Q2_ARRAY_SIZE 10
int q2Array[Q2_ARRAY_SIZE];

//-------------------------------------------------------------------------
// Question 3
Expand All @@ -37,7 +40,11 @@
// The function should return 0b0001001101100101
//-------------------------------------------------------------------------
uint16_t q3(uint8_t x, uint8_t y) {

x ^= 0b10000001;
uint16_t result = x;
result <<= 8;
result += y;
return result;
}

//-------------------------------------------------------------------------
Expand All @@ -51,10 +58,16 @@ uint16_t q3(uint8_t x, uint8_t y) {
// Note: The array contains 8-bit unsigned integers.
//-------------------------------------------------------------------------
int32_t q4(uint8_t * array, uint32_t arrayLength) {
for (uint8_t i = 0; i <= arrayLength; i++) {
int32_t sum = 0;
sum += array[i];
}

if ( array == NULL ){
return -1;
}

int32_t sum = 0;
for (uint32_t i = 0; i < arrayLength; i++) {
sum += array[i];
}
return sum;
}

//-------------------------------------------------------------------------
Expand All @@ -63,7 +76,10 @@ int32_t q4(uint8_t * array, uint32_t arrayLength) {
// - uint32_t a
// - uint16_t b
//-------------------------------------------------------------------------

typedef union {
uint32_t a;
uint16_t b;
} q5_t;

//-------------------------------------------------------------------------
// Question 6
Expand All @@ -72,7 +88,10 @@ int32_t q4(uint8_t * array, uint32_t arrayLength) {
// - uint32_t x
// - uint16_t y
//-------------------------------------------------------------------------

typedef struct {
uint32_t x;
uint16_t y;
} q6_t;

//-------------------------------------------------------------------------
// Question 7
Expand All @@ -81,14 +100,17 @@ int32_t q4(uint8_t * array, uint32_t arrayLength) {
// - SUCCESS = 0
// - FAIL = 1
//-------------------------------------------------------------------------

typedef enum {
SUCCESS = 0,
FAIL = 1
} error_t;

//-------------------------------------------------------------------------
// Question 8
// Define a macro called `MULTIPLY` that takes two parameters and multiplies
// them together. The macro should return the result.
//-------------------------------------------------------------------------

#define MULTIPLY( a, b ) ((a)*(b))

//-------------------------------------------------------------------------
// Question 9
Expand All @@ -101,8 +123,18 @@ int32_t q4(uint8_t * array, uint32_t arrayLength) {
// q9(&x, &y); // returns 0
// Now, x = 10 and y = 5
//-------------------------------------------------------------------------
int q9(int *a, int *b) {
#define nullptr NULL

int q9(int *a, int *b) {
// won't be able to dereference the variable and will cause an error
if ( a == nullptr || b == nullptr ){
return -1;
} else {
int tmp = *a;
*a = *b;
*b = tmp;
return 0;
}
}

//-------------------------------------------------------------------------
Expand All @@ -120,7 +152,12 @@ typedef struct {
} q10_t;

error_t q10(q10_t *q10) {

int result = q9(&q10->a, &q10->b);
if ( result == 0 ){
return SUCCESS;
} else {
return FAIL;
}
}

//-------------------------------------------------------------------------
Expand All @@ -143,14 +180,28 @@ typedef struct {
} q11_b_t;

error_t q11(q11_a_t *a, q11_b_t *b){

// if either of the pointers is null the we won't be able to access
// the values and copy anything
if ( a == nullptr || b == nullptr ){
return FAIL;
}

// similarly, the arrays of a and b must point to arrays
if ( a->array == nullptr || b->array == nullptr ){
return FAIL;
}
for ( int i = 1; i < 51; i++ ){
a->array[i - 1] = b->array[i];
}
return SUCCESS;
}

//-------------------------------------------------------------------------
// Question 12
// Define a macro called `MIN` that takes two parameters and finds the
// lesser value of the 2. The macro should return the result.
//-------------------------------------------------------------------------
#define MIN(a,b) (a < b ? a : b )

//-------------------------------------------------------------------------
// Question 13
Expand All @@ -162,7 +213,18 @@ error_t q11(q11_a_t *a, q11_b_t *b){
//-------------------------------------------------------------------------

void *q13(uint32_t *ptr1, uint16_t *ptr2){

// if either pointer is null the comparison will make no sense
if ( ptr1 == nullptr || ptr2 == nullptr ){
return -1;
} else {
// convert the addresses to char pointers so we can
// address individual bytes
char *char_ptr1 = ptr1;
char *char_ptr2 = ptr2;
char *min_ptr = char_ptr1 < char_ptr2 ? char_ptr1 : char_ptr2;
min_ptr += 5;
return min_ptr;
}
}
//-------------------------------------------------------------------------
// The following function is used to test your code. Do not remove any
Expand Down Expand Up @@ -273,4 +335,4 @@ int main(void) {
ASSERT(q13(ptr1, ptr2) == (void *)0x3129);

return 0;
}
}

0 comments on commit b9105af

Please sign in to comment.