Question #12505

Write the C function void kappa_min(int K) that reads from stdin a sequence of integers, of undefined length, terminated by the appearance of the value 0 (zero). The function finds and prints the K smallest integers read (in increasing order).

Expert's answer

void kappa_min(int k){

char byte[256];

int mass[10000] , ind = 0;

while(!feof(stdin)){

& fgets(byte,256,stdin);

& int number = atoi(byte);

& if (number == 0) break;

& mass[ind++] = number;

}

for (int j = 0 ; j < ind ; j++)

for (int i = 0 ; i < ind - 1 ; i++){

& if (mass[i] > mass[i+1]) {

& int temp = mass[i];

& mass[i] = mass[i+1];

& mass[i+1] = temp;

& }

}

for (int i = 0 ; i < k ; i++)

& printf("%i\n",mass[i]);

}

char byte[256];

int mass[10000] , ind = 0;

while(!feof(stdin)){

& fgets(byte,256,stdin);

& int number = atoi(byte);

& if (number == 0) break;

& mass[ind++] = number;

}

for (int j = 0 ; j < ind ; j++)

for (int i = 0 ; i < ind - 1 ; i++){

& if (mass[i] > mass[i+1]) {

& int temp = mass[i];

& mass[i] = mass[i+1];

& mass[i+1] = temp;

& }

}

for (int i = 0 ; i < k ; i++)

& printf("%i\n",mass[i]);

}

Learn more about our help with Assignments: C#

## Comments

## Leave a comment