Answer to Question #331314 in C for shiva

Question #331314

The principal of a school wants to speak with some of the students to assess their over all development including studies,most of the teachers were selecting the toppers of their classes but Mr,sam thought of an idea to select the bunch of students who did not have much difference in their marks so that he gains the reputation of a teacher who teaches well to the whole class and not just to a bunch of brilliant students.

Your are required to help mr,sam in selecting K students that can speak with principle from his class .you need to return an integer array representing the marks of the selected students sorted in ascending order.

I/p1:An integer value denoting the number of students in Mr.sam’s class.

I/p2:An integer array of size input1representing the marks of each student

I/p3:An integer K denoting the number of representatives to be

O/p:return an integer array containing the marks of K selected students sorted in ascending order.

Ex:

I/p1:10

I/p2:{950,477,55,602,881,302,859,438,551,382}

I/p3:1

O/pt:{950}


1
Expert's answer
2022-04-25T13:37:49-0400
#include <stdio.h>

int main()
{
    int i, j, n, k, start, temp, min;
    printf("Enter number of students:");
    scanf("%d", &n);
    int array[n];
    int diff[n-1];
    printf("Enter marks of each student:\n");
    for (i=0; i<n; i++)
       scanf("%d", &array[i]);
    printf("Enter value of K: ");
    scanf("%d", &k);
    printf("\nArray:\n");
    for (i=0; i<n-1; i++)
            printf("%d ", array[i]);
    for (i=0; i<n-1; i++)
        for (j=i+1; j<n; j++)
            if (array[i]>array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
    printf("\nOutput:\n");
    if (k == 1)
        printf("%d\n", array[n-1]);
    else
    {
        for (i=0; i<n-1; i++)
            diff[i] = array[i+1] - array[i];
        start = 0;
        min = array[n-1];
        for (i=0; i<n-k+1; i++)
        {
            temp = 0;
            for (j=i; j<i+k-1; j++)
                temp = temp + diff[j];
            if (min > temp)
            {
                min = temp;
                start = i;
            }
        }
        for (i=start; i<start+k; i++)
            printf("%d ", array[i]);                
    }
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS