Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 10

ID: 3556170 • Letter: #

Question

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
/* Asks the user to enter a series of up to 'size' nonnegative integers, reads them from the terminal and stores in the 'number' array. Returns the number of integers read and placed into the array. */
int read_array(int *number, int size);
/* Sorts the 'number' array of 'size' integers, using the selection sort algorithm. The selection sort algorithm, when given an array with n elements, does the following: 1. Searches the array to find the largest element, then moves it to the last position in the array. 2. Calls itself recursively to sort the first $n-1$ elements of the array. */
void selection_sort(int *number, int size);
/* Prints the 'message', followed by 'size' integers from the 'number' array, and a new line. */
void print_array(char *message, int *number, int size);
/* If 'condition' is false (0), prints the 'message' and exits the program with an error code. */
void ensure(bool condition, char *message);
int main() {
int *number=(int *)malloc(sizeof(int)*MAX_SIZE);
int size;
size = read_array(number, MAX_SIZE);
ensure(size > 0, "The array is empty ");
print_array("Initial array: ", number, size);
selection_sort(number, size);
print_array("Sorted array: ", number, size);
free(number);
return EXIT_SUCCESS;
}
int read_array(int *number, int size) {
int i = 0, temp;
while(1)
{
printf(" Enter %d Integer :", (i + 1));
scanf("%d",&temp);
if(temp < 0)
break;
if(i == size){
printf(" maximum number of integers has been entered");
break;
}
number[i++] = temp;
}
printf(" ");
return i;
}
void selection_sort(int *number, int size) {
if(size == 1)
return;
int max = 0;
int i = 0;
for(i = 1; i < size; i++)
if(number[i] > number[max])
max = i;
int temp = number[size - 1];
number[size - 1] = number[max];
number[max] = temp;
//print_array(" temp:",number,size);
selection_sort(number,size - 1);
}
void print_array(char *message, int *number, int size) {
int i;
printf("%s", message);
for(i = 0; i < size; i++)
printf("%d ", number[i]);
printf(" ");
}
void ensure(bool condition, char *message) {
if (!condition) {
printf("%s",message);
exit(EXIT_FAILURE);
}
}

Explanation / Answer

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
/* Asks the user to enter a series of up to 'size' nonnegative integers, reads them from the terminal and stores in the 'number' array. Returns the number of integers read and placed into the array. */
int read_array(int *number, int size);
/* Sorts the 'number' array of 'size' integers, using the selection sort algorithm. The selection sort algorithm, when given an array with n elements, does the following: 1. Searches the array to find the largest element, then moves it to the last position in the array. 2. Calls itself recursively to sort the first $n-1$ elements of the array. */
void selection_sort(int *number, int size);
/* Prints the 'message', followed by 'size' integers from the 'number' array, and a new line. */
void print_array(char *message, int *number, int size);
/* If 'condition' is false (0), prints the 'message' and exits the program with an error code. */
void ensure(bool condition, char *message);
int main() {
int *number=(int *)malloc(sizeof(int)*MAX_SIZE);
int size;
size = read_array(number, MAX_SIZE);
ensure(size > 0, "The array is empty ");
print_array("Initial array: ", number, size);
selection_sort(number, size);
print_array("Sorted array: ", number, size);
free(number);
return EXIT_SUCCESS;
}
int read_array(int *number, int size) {
int i = 0, temp;
while(1)
{
printf(" Enter %d Integer :", (i + 1));
scanf("%d",&temp);
if(temp < 0)
break;
if(i == size){
printf(" maximum number of integers has been entered");
break;
}
*(number+i++) = temp;
}
printf(" ");
return i;
}
void selection_sort(int *number, int size) {
if(size == 1)
return;
int max = 0;
int i = 0;
for(i = 1; i < size; i++)
if(*(number+i) > *(number+max))
max = i;
int temp = *(number+size - 1);
*(number+size - 1) = *(number+max);
*(number+max) = temp;
//print_array(" temp:",number,size);
selection_sort(number,size - 1);
}
void print_array(char *message, int *number, int size) {
int i;
printf("%s", message);
for(i = 0; i < size; i++)
printf("%d ", *(number+i));
printf(" ");
}
void ensure(bool condition, char *message) {
if (!condition) {
printf("%s",message);
exit(EXIT_FAILURE);
}
}