Question
Write a C program to read integers into an array and then sort the array using selection sort, where you find the smallest element in
Write a C program to read integers into an array and then sort the array using selection sort, where you find the smallest element in the array and exchange it with the first element, and then find the second smallest element and exchange it with the second element, etc., until the entire 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)
Which of these four functions should have the array passed in as const?
Where should the local prototypes of each these functions be places?
Use #define to 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. Enter more values than the declared array size. Display an appropriate error message and sort the first 10 values anyway.
4. Enter an invalid (nonnumeric) character. Display an appropriate error message and flush the bad data and continue reading until end-of-file or the array is full
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 (e.g., no goto, break (except in a switch), continue, etc. statements)
Turn in printouts of your program listing and the output. Make sure that your full name is on all of your printouts. Staple the assignment sheet to the top of your program.
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started