Question: C PROGRAMMING Write a program that dynamically allocates an array of integers. The size of the array (the number of integers, not the byte size)
C PROGRAMMING



Write a program that dynamically allocates an array of integers. The size of the array (the number of integers, not the byte size) should be read from the user using scanf(). You may assume that the user will input a positive integer (i.e., don't do error checking). The elements of the array should be filled using random() function. After filling the array with random numbers, your program should then make a copy of the array, and sort the new array in ascending order: that is, the first entry of the array should contain the smallest integer in the array, while the last entry should contain the largest integer in the array. Then make a second copy of the original array, and sort it in descending order. Finally your program should print out all three arrays. All three arrays should be separately allocated using malloc() library function. Don't forget to call free () to deallocate the arrays. You should always check the return value of malloc(), and if it's NULL, print an error message and quit the program, like this: p = malloc( ...); if (p == NULL) { perror ("malloc returned NULL"); exit (1); } Make sure you do this everytime you call malloc(). This applies to all labs in this class. Please name your executable program "isort". Type "man 3 random" to read the man page for random(). In K&R2, there is no mention of random(). Instead, it describes the older and inferior rand() function. The usage is the same. Use random() in your code. The random () function returns a non-negative integer in the range from 0 to 2^31-1. In order to make the visual inspection of the output a little easier, please convert the large random integer to one in the range from o to 99. Please print the arrays in the following format: original: 31 57 1 44 ascending: 1 31 44 57 descending: 57 44 31 1 You will notice that random() function always returns the same sequence of integers. It's just a pseudo-random number generator that simulates randomness. You can "seed" the random number generator by calling srandom() function once in the beginning of your program. Calling it with the return value of time (NULL) will ensure a different sequence of random numbers everytime the program is run. You should do that. For sorting, you have two options. You can implement any sorting algorithm yourself. You are allowed to look up on the Internet or any book for a sorting algorithm. If you do, please cite the source in the comment for the function. Or you can use asort() function provided by the standard C library (just like they provide printf()). Using the qsort() library function is simpler, but requires that you know how to use pointers to functions, which we have not covered yet. Section 5.11 of K&R2 describes pointers to functions. Unfortunately, the section uses a sorting function named "sort" to illustrate pointers to functions, but it takes a different set of parameters than the real qsort () of standard library. The standard library version of qsort() is described on page 253, K&R2, or in the man page (type "man 3 qsort"). There is, however, one little requirement about calling the sorting function, whether it's your own function or the qsort() function. I want you to call your sorting function indirectly through the following function: /* This function sorts an integer array. begin points to the 1st element of the array. end points to ONE PAST the last element of the array. If ascending is 1, the array will be sorted in ascending order. If ascending is o, the array will be sorted in descending order. */ void sort_integer_array(int *begin, int *end, int ascending) { /* In here, you will call your real sorting function (your own * or the qsort()). Basically, I want to make sure that you * know how to translate the begin/end parameter to whatever * is required for your sorting function. */ } sort_integer_array() function should not access any global variables and should not be defined as a nested function inside another function. Nested functions are not part of standard c and should not be used in this class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
