In C write a program that reads its input into an array and then uses selection
ID: 3810634 • Letter: I
Question
In C 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 four 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 statements, e.g., the break statement.
Declare the maximum array size as a constant MAXSIZE with a value of 10.
Run your program using the following sets 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 error checks, e.g., what happens when you enter more values than your declared array size or when invalid (nonnumeric) data is entered. Flush the invalid data and continue reading until end-of-file or the array is full. Print appropriate error messages 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.
The code should use the following:
1. meaningful variable names
2. comments on the following:
- program description
- function descriptions
- all variable and constant declarations
- ambiguous or complex sections of code
3. the correct use of local variables, local prototypes, and parameter passing
Selection Sort
algorithm in pseudocode
for i = 0 to (n - 2)
min = ai
min_index = i
for j = (i + 1) to (n - 1)
if aj < min
min = aj
min_index = j
endif
endfor
swap(a, i, min_index)
endfor
Explanation / Answer
// Create A file named as input_data.txt and paste your input into that file
#include<stdio.h>
#include<ctype.h>
#define MAXSIZE 10
int numel;
int* read_array()
{
static int data_array[MAXSIZE]; // Define Int to receive File Integer Values
FILE* file = fopen ("input_data.txt", "r"); // Open File
int i = 0;
for (i=0;((!feof(file)) && (i<MAXSIZE));i++) // Read only values till feof or MAXSIZE from file
{
fscanf (file, "%d", &data_array[i]);
}
numel=i; // Total Elements in Array
fclose (file); // File Close
return data_array; // Return Array
}
void print_array(int* data_array) // Print Array
{
int i;
printf("Array Elements Are: ");
for(i=0;i<numel;i++)
printf("%d ",data_array[i]); // Print Array
printf(" ");
}
int* swap_element(int* data_array,int i, int j) // Swap where data is data_array and swapping index is i and j
{
int temp;
temp=data_array[i];
data_array[i]=data_array[j];
data_array[j]=temp;
return data_array;
}
int* sort_array(int* data_array) // Sort Function
{
int i,j; // Loop Variables
for(i=0;i<numel-1;i++) // Outer Loop to Select One Element
{
for(j=i+1;j<numel;j++) // Inner Loop to Select Element for Comparison
{
if(data_array[i]>data_array[j]) // Comparison of element
{
data_array=swap_element(data_array,i,j); // Calling Swapping Function and receiving Swapped Array
}
}
}
return data_array; // Return Sorted Array
}
int main() // Main program with argc argument count and argv input arguments
{
int* data=read_array(); // read data
print_array(data);// print Data
sort_array(data); // Sort data
print_array(data); // print data after Sort
}