Skip to content

Commit

Permalink
Replaced ambigous return codes with macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
A Silent Cat committed Jul 19, 2018
1 parent 6c5128e commit 97be2b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
26 changes: 14 additions & 12 deletions Data Structures/Array/CArray.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "CArray.h"

void swap(CArray *array, int position1, int position2);
Expand All @@ -49,11 +50,11 @@ int insertValueCArray(CArray *array, int position, int value)
if (position >= 0 && position < array->size) {
if (array->array[position] == 0) {
array->array[position] = value;
return 0; // OK
return SUCCESS;
}
else return 2; // Position already initialized (use update function)
else return POSITION_INIT;
}
return 1; // Invalid position
return INVALID_POSITION;
}

int removeValueCArray(CArray *array, int position)
Expand All @@ -62,9 +63,9 @@ int removeValueCArray(CArray *array, int position)
if (array->array[position] != 0) {
array->array[position] = 0;
}
else return 4; // Position already empty
else return POSITION_EMPTY;
}
return 1; // Invalid position
return INVALID_POSITION;
}

int pushValueCArray(CArray *array, int value)
Expand All @@ -78,8 +79,8 @@ int pushValueCArray(CArray *array, int value)
break;
}
}
if (ok == 1) return 0;
else return 5; // Array is full
if (ok == 1) return SUCCESS;
else return ARRAY_FULL;
}

int updateValueCArray(CArray *array, int position, int value)
Expand All @@ -88,9 +89,10 @@ int updateValueCArray(CArray *array, int position, int value)
if (array->array[position] != 0) {

}
else return 3; // Position not initialized (use insert function)

else return POSITION_NOT_INIT;
}
return 1; // Invalid Position
return INVALID_POSITION;
}

int eraseCArray(CArray *array)
Expand All @@ -110,7 +112,7 @@ int switchValuesCArray(CArray *array, int position1, int position2)
array->array[position1] = array->array[position2];
array->array[position2] = temp;
}
return 1; // Invalid Position
return INVALID_POSITION;
}

int reverseCArray(CArray *array)
Expand All @@ -119,7 +121,7 @@ int reverseCArray(CArray *array)
for (i = 0; i < array->size / 2; i++) {
swap(array, i, array->size - i - 1);
}
return 0;
return SUCCESS;
}

int displayCArray(CArray *array)
Expand Down Expand Up @@ -250,4 +252,4 @@ int findMaxCArray(CArray *array)
}
}
return max;
}
}
10 changes: 9 additions & 1 deletion Data Structures/Array/CArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
extern "C" {
#endif

#define ARRAY_ERASED -1
#define SUCCESS 0
#define INVALID_POSITION 1
#define POSITION_INIT 2
#define POSITION_NOT_INIT 3
#define POSITION_EMPTY 4
#define ARRAY_FULL 5

typedef struct CArray {
int *array;
int size;
Expand Down Expand Up @@ -73,4 +81,4 @@ extern "C" {

#ifdef __cplusplus
}
#endif
#endif

0 comments on commit 97be2b2

Please sign in to comment.