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

Write a program that reads its input into an array and then uses selection sort

ID: 3810460 • Letter: W

Question

Write a program that reads its input into an array and then uses selection sort to sort the array. In selection sort, we first find the smallest element in the array and exchange it with the first array element's value; then we find the next smallest element in the array and exchange it with the second array element's value; and so on, until the array is sorted.

Have your program include 4 functions, besides main, which perform the following functions:

1. Read in an undetermined number of array elements.

2. Print the array elements (call this function both before and after the elements are sorted).

3. Sort the array elements.

4. Swap two elements of an array (this function will be called by your sort function).

Do not use any jump statement, e.g., the break statement.

Declare the maximum array size as a constant MAXSIZE with a value of 10.

Run your program with the following set of input:

1. 5 12 -7 3 0

2. 1 2 3 4 5 7 6

3. Some input that will trigger each of your errors checks, what happens when you enter more values than MAXSIZE or when invalid (nonnumeric) data is entered. Flush the invalid data and continue reading until EOF or the array is full. Print appropriate error message for invalid character data and for too many values. Data items should still be sorted, even if invalid character data is encountered or too many values are entered.

Explanation / Answer

#include<stdio.h>
#define MAXSIZE 10
//To sort array using selection sort
void Selection_Sort(int arr[], int len)
{
int mini, loc, temp, x, y;
//Loops till length - 1 times
for(x = 0; x <= len - 1; x++)
{
//Initializes array x position to minimum
mini = arr[x];
//Assigns x position as the location
loc = x;
//Loops till length - 1 times
for(y = x + 1; y <= len -1; y++)
{
//Checks the current position of the array is less than minimum
if(arr[y] < mini)
{
//Update the minimum to array current position
mini = arr[y];
//Update the location to current position
loc = y;
}//End of if
}//End of inner loop
//Checks if location is not equal to x value
if(loc != x)
{
//Swapping operation
temp = arr[x];
arr[x] = arr[loc];
arr[loc] = temp;
}//End of if
}//End of outer loop
}//End of function
//Main function
int main()
{
//Declares an array of MAXSIZE
int arr[MAXSIZE], len, x;
//Loops till valid size
do
{
//Accept size
printf(" Enter the length of array ");
scanf("%d", &len);
//Checks size for negative
if(len < 0)
printf(" Length cannot be zero. Please reenter! ");
//Checks the size for greater than specified size
else if(len > MAXSIZE)
printf(" Length cannot be greater than zero. Please reenter! ");
else
break;
}while(1);

//Accept data to array
printf(" Enter data into array ");
for(x = 0; x < len; x++)
scanf("%d", &arr[x]);

printf(" Before Sorting: ");
for(x = 0; x < len; x++)
printf("%4d", arr[x]);

Selection_Sort(arr, len);

printf(" After Sorting: ");
for(x = 0; x < len; x++)
printf("%4d", arr[x]);
}//End of main

Sample run1:

Enter the length of array 5

Enter data into array 5 12 -7 3 0

Before Sorting: 5 12 -7 3 0
After Sorting: -7 0 3 5 12

Sample run2:

Enter the length of array 7

Enter data into array 1 2 3 4 5 7 6

Before Sorting: 1 2 3 4 5 7 6
After Sorting: 1 2 3 4 5 6 7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote