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

In C write a program that reads its input into an array and then uses selection

ID: 3811076 • 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. The program should allow the user to enter input.

In Main it should include the following:

func.protype

var.declars

n=table_file (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)

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 when program when user input the following sets:

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.

As before, your grade will be based on the correctness and format of your output, as well as the proper use of the following:

1. meaningful variable names

2. indentation

3. blank lines and spacing

4. comments on the following:

- program description

- function descriptions

- all variable and constant declarations

- ambiguous or complex sections of code

5. the correct use of local variables, local prototypes, and parameter passing

6. format and appearance of output

7. structured code

Explanation / Answer

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

int table_file(int a[], int maxsize);
void table_print(int a[], int n);
void table_sort (int a[], int n);
void swap(int *x, int *y);

int main()
{
  
    int a[MAX_SIZE];
    int n = table_file(a, MAX_SIZE);
    table_print(a, n);
    table_sort (a, n);
    table_print(a, n);
    return 0;
}


int table_file(int a[], int maxsize) {
  
    int temp;
    int i=0, n = 0;
    printf("Enter the input size : ");
    scanf("%d", &n);
  
    if (n > maxsize) {
        printf("Error : input size is larger than maximum capacity ");
        exit(0);
    }
  
    printf("Enter the input data : ");  
    for (; i<n; i++) {
        scanf("%d", &a[i]);
    }
    return n;
}

void table_print(int a[], int n) {
    int i=0;
    printf("The contents of the array is : ");
    for (; i<n; i++) {
        printf("%d ", a[i]);      
    }
    printf(" ");
}

void table_sort(int array[], int n) {
    int i=0, j=0;
  
    for ( ; i<(n-1) ; i++) {
      int position = i;
    
      for (j = position + 1 ; j < n ; j++ ) {
         if (array[position] > array[j])
            position = j;
      }
      if (position != j) {
          swap(&array[position], &array[i]);
      }
   }
}

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}