This exercise consists of 5 questions.
Each question has an associated test which is in the corresponding
test_ .c file. (e.g. the test for the max function is in the
test_max.c file).
The prototypes for each function that you have to implement are given in the test files.
You have to implement all your solutions in a file named
solns.c. An empty solns.c has been provided. Implement the
functions as per the questions in that file.
You are not to modify any of the test_ files.
You can download the Makefile onto your machine to make local
testing a little easier.
-
Implement a function called
maxwith prototypeint max(int [], int);. This function should take an integer array and the number of elements in that array and return the largest integer in that array. Implement this function in thesolns.cfile. You can test it by compiling thetest_max.cfile like sogcc test_max.c solns.c -o test_maxand running it or, if you've downloaded the
Makefile, by simply runningmake run_test_max. -
Implement a function called
maxwith prototypeint min(int [], int);. This function should take an integer array and the number of elements in that array and return the smallest integer in that array. Implement this function in thesolns.cfile. You can test it by compiling thetest_min.cfile like sogcc test_min.c solns.c -o test_minand running it or, if you've downloaded the
Makefile, by simply runningmake run_test_min. -
Implement a function called
averagewith prototypefloat average(int [], int);. This function should take an integer array and the number of elements in that array and return the average of the elements in the array. Remember, the average need not be an integer. Implement this function in thesolns.cfile. You can test it by compiling thetest_average.cfile like sogcc test_average.c solns.c -o test_averageand running it or, if you've downloaded the
Makefile, by simply runningmake run_test_average. -
The mode of a list of numbers is the number that occurs most frequently in the list. e.g. The mode of 5, 6, 7, 7, 6, 6, 6, 4, 3 is 6 since it occurs 4 times.
Implement a function called
modewith prototypeint mode(int [], int);. This function should take an integer array and the number of elements in that array and return the mode of the numbers in the array. Implement this function in thesolns.cfile. You can test it by compiling thetest_mode.cfile like sogcc test_mode.c solns.c -o test_modeand running it or, if you've downloaded the
Makefile, by simply runningmake run_test_mode.Hint: One way to implement this would be to create a new array (let's call it
counts) that goes from 0 to the largest element in the given array. Then go through the given array and store the number of times each element occurs in thecountsarray. If your original array were{2, 2, 4, 4, 4, 5}, the counts array would be{0, 0, 2, 0, 3, 1}meaning0(first index) occurs0times,1occurs0times,2occurs2times,3occurs0times,4occurs3times and5occurs once. Then go through thiscountsarray and identify the element that occurs the maximum number of times (similar to how you did themaxproblem). That's the answer. -
The prime factors of a number are the prime numbers which when multiplied yield the given number. E.g. The prime factors of
2250are 2, 3, 3, 5, 5, and 5 since2 x 3 x 3 x 5 x 5 x 5is2250.Implement a function called
factorswith prototypeint factors(int, int []);. This function should take an integer and an empty integer array as arguments and should fill the provided integer array with the prime factors of the given number. In addition, it should return the number of prime factors. e.g. If we call this function like sofactors(2250, f)wherefis an empty integer array, then it should return6andfshould contain{2, 3, 3, 5, 5, 5}Implement this function in the
solns.cfile. You can test it by compiling thetest_factors.cfile like sogcc test_factors.c solns.c -o test_factorsand running it or, if you've downloaded the
Makefile, by simply runningmake run_test_factors.