In C DO NOT USE POINTER write a program that reads its input into an array and t
ID: 3814649 • Letter: I
Question
In C DO NOT USE POINTER 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. The program should allow the user to enter input. Also the program should use local prototypes and comment all variable and constant declarations.
In Main it should include the following:
func.protype
var.declars
n=table_fill (a, maxsize);
table_print (a, n);
table_sort (a, n);
table_print (a, a);
return 0;
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). Use the code below
cnt = 0;
while ((r=scanf(“%i”, &next)) != EOF && cnt < max)
{
if (r == 0) /* invalid input data */
{
printf(“Nonnumeric data entered. Please enter a number.”);
while (getchar( ) != ‘ ’) /* flush invalid data */
;
}
else
a[cnt++] = next;
}
if (r == 1) /* another value was read, but the array is full */
printf(“Error - too many values. Array size is %i. ”, max);
3. Sort the array elements use the code below
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
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 when program when user input the following sets:
1. 5 12 -7 3 0
2. 1 2 3 4 5 7 6
MAKE SURE THAT THE PROGRAM CHECK THE THESE STATEMENT: "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."
Explanation / Answer
You have not described about a condition on how can a user stop giving inputs (I mean if user wants to sort a set of values whose size is less than the max size) . For now user has to enter values till the max size is reached.
/*********************************************
* Progam reads input from the user and stores
* in an array. Later it sorts the array in
* ascending order and prints it.
* ******************************************/
#include<stdio.h>
int table_fill(int a[], int max);
void table_print(int a[], int len);
void table_sort(int a[], int len);
void swap(int a[], int index1, int index2);
int main()
{
int a[5] = {0}; // Array to hold the values entered
int count = 0; // To hold count of values entered by the user
count = table_fill(a,5); // Function the read values from user and store in array 'a'
printf("Before sorting : ");
table_print(a, count); // Print the array
table_sort(a,count); // Sort the array
printf("After sorting : ");
table_print(a, count); // Print the sorted array
return 0;
}
int table_fill(int a[], int max)
{
int cnt = 0; // Count of values entered
int next = 0; // Variable to hold the current value entered by the user
int r = 0; // Variable to hold the return value of scanf()
printf("Please enter the numbers : ");
while ((r=scanf("%d", &next)) != EOF && cnt < max)
{
if (r == 0) /* invalid input data */
{
printf("Nonnumeric data entered. Please enter a number.");
while (getchar( ) != ' '); /* flush invalid data */
}
else
a[cnt++] = next;
}
if (r == 1) /* another value was read, but the array is full */
printf("Error - too many values. Array size is %i. ", max);
return cnt;
}
void table_print(int a[], int len)
{
int i =0 ; // Array iterator
printf("Array values : ");
for(i=0;i<len;i++)
printf("%d ",a[i]);
printf(" ");
}
void swap(int a[], int index1, int index2)
{
a[index1] = a[index1] + a[index2];
a[index2] = a[index1] - a[index2];
a[index1] = a[index1] - a[index2];
}
void table_sort(int a[], int len)
{
int i=0, j=0; // Array iterator
int min = 0; // Minimum value for each iteration
int min_index = 0; // Index of minimum value
for(i=0 ; i< len ;i++){
min = a[i];
min_index = i;
for(j=(i+1); j < len;j++){
if(a[j] < min){
min = a[j];
min_index = j;
}
}
if(i != min_index)
swap(a,i,min_index);
}
}