diff options
Diffstat (limited to '14_array_max')
-rw-r--r-- | 14_array_max/.gitignore | 1 | ||||
-rw-r--r-- | 14_array_max/Makefile | 2 | ||||
-rw-r--r-- | 14_array_max/README | 16 | ||||
-rw-r--r-- | 14_array_max/arrayMax.c | 56 | ||||
-rw-r--r-- | 14_array_max/grade.txt | 21 |
5 files changed, 96 insertions, 0 deletions
diff --git a/14_array_max/.gitignore b/14_array_max/.gitignore new file mode 100644 index 0000000..bdf4bbd --- /dev/null +++ b/14_array_max/.gitignore @@ -0,0 +1 @@ +arrayMax diff --git a/14_array_max/Makefile b/14_array_max/Makefile new file mode 100644 index 0000000..51fb361 --- /dev/null +++ b/14_array_max/Makefile @@ -0,0 +1,2 @@ +arrayMax: arrayMax.c + gcc -o arrayMax -pedantic -std=gnu99 -Wall -Werror arrayMax.c diff --git a/14_array_max/README b/14_array_max/README new file mode 100644 index 0000000..e98f467 --- /dev/null +++ b/14_array_max/README @@ -0,0 +1,16 @@ + 1. Open the provided arrayMax.c file and write + the function: + int * arrayMax(int * array, int n); + which returns a pointer to the largest element + in the array passed in (whose length is n). + If the array has no elements (n is 0), this function + should return NULL. + + 2. Compile and test your code. + We have provided a main function which will print test cases + and your function's answer for them. You should get + 99, -3, 425, NULL, and NULL for the 5 test provided. + + 3. Submit your code. + + diff --git a/14_array_max/arrayMax.c b/14_array_max/arrayMax.c new file mode 100644 index 0000000..5fc5d62 --- /dev/null +++ b/14_array_max/arrayMax.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> + +int * arrayMax(int * array, int n) { + if (n == 0){ + return NULL; + } + int maxInt = array[0]; + int * answer = array; + for (int i = 1; i < n; i++) { + if (array[i] > maxInt) { + maxInt = array[i]; + answer = &array[i]; + } + } + return answer; +} + +void doTest(int * array, int n) { + printf("arrayMax("); + if (array == NULL) { + printf("NULL"); + } + else { + printf("{"); + for (int i =0; i < n; i++) { + printf("%d", array[i]); + if (i < n -1) { + printf(", "); + } + } + printf("}"); + } + printf(", %d) is \n", n); + int * p = arrayMax (array, n); + if (p == NULL) { + printf("NULL\n"); + } + else { + printf("%d\n", *p); + } +} + +int main(void) { + int array1[] = { 77, 33, 19, 99, 42, 6, 27, 4}; + int array2[] = { -3, -42, -99, -1000, -999, -88, -77}; + int array3[] = { 425, 59, -3, 77, 0, 36}; + + doTest (array1, 8); + doTest (array2, 7); + doTest (array3, 6); + doTest (NULL, 0); + doTest (array1, 0); + + return EXIT_SUCCESS; +} diff --git a/14_array_max/grade.txt b/14_array_max/grade.txt new file mode 100644 index 0000000..3b969b3 --- /dev/null +++ b/14_array_max/grade.txt @@ -0,0 +1,21 @@ +Grading at Wed 13 Oct 2021 02:00:59 AM UTC +Attempting to compile arrayMax.c +################################################# +testcase1: +Your file matched the expected output +Your output matched what we expected +Removing your main() and replacing it with our own to run more tests... +################################################# +testcase2: +array size:0 was Correct +################################################# +testcase3: +array size:1 was Correct +################################################# +testcase4: +array size:100 was Correct +################################################# +testcase5: +array size:5000 was Correct + +Overall Grade: A |