Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

sort.c #include #include #include libsort.h int main() { int* array; int size, c; float median; printf(Enter the array size: ); scanf(%d, &size); array = (int*)

image text in transcribed

sort.c

#include #include #include "libsort.h"

int main() { int* array; int size, c; float median;

printf("Enter the array size: "); scanf("%d", &size);

array = (int*) malloc(size * sizeof(int));

printf("Enter %d integers: ", size);

for (c = 0; c

sort(array, size);

printf("Array sorted in ascending order: "); for (c = 0; c

printf(" "); median = find_median(array, size); printf("The median is: %.1f ", median);

return 0; }

Q1) Write a function that takes an array and its size as inputs, and then sorts the elements of the array in ascending order. For example, if the array contains the values 12-3,5,4,7), then the array sorted in ascending order w be (-3, 4,5,7, 12) In order to sort the array, you must use the following algorithm (0) Initialize 0; (1) Starting at array index i, find the smallest value in the array at or after index (i.e., any index greater than or equal to i within the proper array bounds). (2) Swap the smallest value found in the array with the value t ndexi (3) Increment i by one; (4) If i is less than the array size, then repeat the process starting at step (1) You must use the following function prototype void sort (int array[], int size); Note: You must write your function prototype in a header file named libsort.h and you must write your function definition in a source file named libsort.c. We are providing you the main source file sort.c, which you can use to test your function. Do not change any- thing in sort.c. In the same libsort.c, write a function that calculates the median of the array. The me- dian is the "middle" number of a sorting array. If the array size is a even number, the median is the average of two middle numbers. If the array size is an odd number, the median will be equal to the value of the middle number. For example, the array with the even numbers, is given by 1,4, 2, 5,7,3). After sorting it, the array becmes 11,2, 3, 4,5,7) and the median will be (3 +4)/2 3.50. You must also add the following prototype to libsort.h: float find median (int array[], int size); To compile your code, remember that you must compile both sort.c and libsort.c but not libsort. h. In other words, rungcc sort. c libsort.c Hint: To determine wether a number is a even number or odd number, you can use the modulo operator %. If you type n%2 The number n is divided by 2 and it will return the remainder. If the remainder is 0, the number wl be even. If the remainder is 1, the number will be odd. For example, 3%2 1.6%2-0 Note: In C, you need to specify the memory for a variable before you start using it; there is no way to automatically add memory. Thus, the program must know how much memory to allocate for the string t cannot allocate memory after you input the string because it doesn't know how much space it needs to store it!)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

show that (In(c)) 100 100Lt where L

Answered: 1 week ago

Question

3. How has e-commerce transformed marketing?

Answered: 1 week ago