Question

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, run·gcc 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 doesnt know how much space it needs to store it!)

sort.c

#include <stdlib.h>
#include <stdio.h>
#include "libsort.h"

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

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

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

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

    for (c = 0; c < size; c++)
        scanf("%d", &array[c]);

    sort(array, size);

    printf("Array sorted in ascending order:\n");
    for (c = 0; c < size; c++)
        printf("%d ", array[c]);

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

    return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find my answer.

########## libsort.h ###########
void sort(int array[], int size);
float find_median(int array[], int size);

########## libsort.c ###########

void sort(int array[], int size){

int i, j, min_idx;

// One by one move boundary of unsorted subarray

for (i = 0; i < size-1; i++)

{

// Find the minimum element in unsorted array

min_idx = i;

for (j = i+1; j < size; j++)

if (array[j] < array[min_idx])

min_idx = j;

// Swap the found minimum element with the first element

int t = array[min_idx];

array[min_idx] = array[i];

array[i] = t;

}

}

float find_median(int array[], int size) {

if(size %2 == 1)

return array[size/2];

else

return (array[size/2-1] + array[size/2]);

}

####### sort.c ######

#include <stdlib.h>
#include <stdio.h>
#include "libsort.h"

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

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

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

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

for (c = 0; c < size; c++)
scanf("%d", &array[c]);

sort(array, size);

printf("Array sorted in ascending order:\n");
for (c = 0; c < size; c++)
printf("%d ", array[c]);

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

return 0;
}

firstweek firstweek firstweek gcc sort.c libsort.c firstweek./a.out Enter the array size: Enter 5 integers: 5 4 1 2 3 Array s

Add a comment
Know the answer?
Add Answer to:
sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() {     int...

    Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() {     int number;     scanf("%d", &number);     if (number % 2 == 0) {         printf("Even\n");     }     else {         printf("Odd\n");     }     return 0; }

  • #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate...

    #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...

  • I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){...

    I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){ int i; for(i = 0; i < asize;i++) printf("%d", array[i]); printf("\n"); } int sum(int array[], int asize){ int result = 0; int i = 0; for(i=0;i < asize;i++){ result = result + array[i]; } return result; } int swap( int* pA,int*pB){ int result = 0; if(*pA > pB){ int tamp = *pA; *pA = *pB; *pB = tamp; result = 1; } return result;...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int...

    Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int var1, var2; int sum; printf("Enter number 1:\n "); scanf("%d",&var1); printf("Enter number 2:In ); scanf("%d",&var2); sum-var1+var2; printf ("Vnsum of two entered numbers : %d", //printf ("Output: %d", res); sum); return e; Modify this code by creating a function called "addition". Make the arguments of the functions numberl and number 2. Add the two values in the function. Return a value called "result". Print "result" in...

  • C program-- the output is not right please help me to correct it. #include <stdio.h> int...

    C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...

  • ​what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h>                         &

    ​what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h>                            #define MAXBINS 99 #define MAXCORS 26 #define MAXCUS 100 #define MAXPUR 10 /* datatype for a list of orders ----------------- */ typedef struct { int bin_order_number; char bin_order_char; }bin_order; typedef struct { int orderNo; bin_order orderbin; }order; typedef struct { int cusnumber; int n;   /* number of the orders what the txt include */ order oo[MAXPUR+1]; }customer; typedef struct{ int n; /*number of the customers */ customer cc[MAXCUS+1];...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT