Write a program that asks the user to enter a 10 integers and stores them in an
ID: 3673641 • Letter: W
Question
Write a program that asks the user to enter a 10 integers and stores them in an array. Write a function (largest) that takes as input an array of int, the length of the array and a position in the array and returns the largest element of the array between the given position and the end of the array. Your program should then ask the user for a position and use the function largest to return the largest element starting at that position. Compile and test your program. Modify your function, change its name to largestPos and update its code so it returns the position of the largest element found. Adjust your program so it prints the largest element in the array starting at the user given position. Write a program (sort c) that asks the user to enter a number of integers, stores them in an array, and the sons the array in increasing order. Your program should do the following: Ask the user to enter an integer between 2 and 15. This wil represent the number of elements to in the array Check if the input is between 2 and 15. If not. your program should keep asking the user to enter an integer between 2 and 15 until a valid input is entered. Ask the user to enter the specified number of integers. Read those integers into an array Print the array before sorting Sort the array in increasing order: (assume that the last index of the array is LAST) Find the largest element of the array (use the function written in part II) Swap the largest element with the element at position 0 Find the largest element between position 1 and LAST Swap the largest element with the element at position 1 Find the largest element between position 2 and LAST Swap the largest element with the element at position 2 Repeat until all elements are sorted Print the array after sorting Compile your program, call the executable sort. Submit through the hand in system the source code sort c and the executable sortExplanation / Answer
sort.c
/*
* File: sort.c
* ------------
* This file implements the sort.h interface using the selection
* sort algorithm.
*/
#include <stdio.h>
#include "genlib.h"
#include "sort.h"
/* Private function prototypes */
static int FindSmallestInteger(int array[], int low, int high);
static void SwapInteger(int *p1, int *p2);
/*
* Function: SortIntegerArray
* --------------------------
* This implementation uses an algorithm called selection sort,
* which can be described in English as follows. With your left
* hand, point at each element in the array in turn, starting at
* index 0. At each step in the cycle:
*
* (1) Find the smallest element in the range between your left
* hand and the end of the array, and point at that element
* with your right hand.
*
* (2) Move that element into its correct index position by
* switching the elements indicated by your left and right
* hands.
*/
void SortIntegerArray(int array[], int n)
{
int lh, rh;
for (lh = 0; lh < n; lh++) {
rh = FindSmallestInteger(array, lh, n-1);
SwapInteger(&array[lh], &array[rh]);
}
}
/*
* Function: FindSmallestInteger
* Usage: index = FindSmallestInteger(array, low, high);
* -----------------------------------------------------
* This function returns the index of the smallest value in the
* specified array of integers, searching only between the index
* positions low and high, inclusive. It operates by keeping track
* of the index of the smallest so far in the variable spos. If the
* index range is empty, the function returns low.
*/
static int FindSmallestInteger(int array[], int low, int high)
{
int i, spos;
spos = low;
for (i = low; i <= high; i++) {
if (array[i] < array[spos]) spos = i;
}
return (spos);
}
/*
* Function: SwapInteger
* Usage: SwapInteger(&i1, &i2);
* -----------------------------
* This function swaps the integers i1 and i2. Note that the
* parameters to SwapInteger are pointers to the actual values.
* This technique is known as call by reference.
*/
static void SwapInteger(int *p1, int *p2)
{
int tmp;
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
sort.h
/*
* File: sort.h
* ------------
* This file provides an interface to a simple procedure
* for sorting an integer array into increasing order.
*/
#ifndef _sort_h
#define _sort_h
/*
* Function: SortIntegerArray
* Usage: SortIntegerArray(array, n);
* ----------------------------------
* This function sorts the first n elements in array into
* increasing numerical order. In order to use this procedure,
* you must declare the array in the calling program and pass
* the effective number of elements as the parameter n.
* In most cases, the array will have a larger allocated
* size.
*/
void SortIntegerArray(int array[], int n);
#endif
testsort.c
/*
* File: testsort.c
* ----------------
* This program tests the sort.c implementation. In this example
* the array is considered to be a list of exam scores. The
* test program reads in a list of scores, sorts them, and then
* displays the sorted list.
*/
#include <stdio.h>
//#include "genlib.h"
//#include "simpio.h"
#include "sort.h"
/*
* Constants
* ---------
* MaxScores -- Maximum number of scores
* Sentinel -- Value used to terminate input
*/
#define MaxScores 100
#define Sentinel -1
/* Private function prototypes */
static int GetIntegerArray(int array[], int max, int sentinel);
static void PrintIntegerArray(int array[], int n);
/* Main program */
main()
{
int scores[MaxScores];
int n;
printf("Enter exam scores, one per line, ending ");
printf("with the sentinel value %d. ", Sentinel);
n = GetIntegerArray(scores, MaxScores, Sentinel);
SortIntegerArray(scores, n);
printf(" The sorted exam scores are: ");
PrintIntegerArray(scores, n);
}
/*
* Function: GetIntegerArray
* Usage: n = GetIntegerArray(array, max, sentinel);
* -------------------------------------------------
* This function reads elements into an integer array by
* reading values, one per line, from the keyboard. The end
* of the input data is indicated by the parameter sentinel.
* The caller is responsible for declaring the array and
* passing it as a parameter, along with its allocated
* size. The value returned is the number of elements
* actually entered and therefore gives the effective size
* of the array, which is typically less than the allocated
* size given by max. If the user types in more than max
* elements, GetIntegerArray generates an error.
*/
static int GetIntegerArray(int array[], int max, int sentinel)
{
int n, value;
n = 0;
while (TRUE) {
printf(" ? ");
value = GetInteger();
if (value == sentinel) break;
if (n == max) Error("Too many input items for array");
array[n] = value;
n++;
}
return (n);
}
/*
* Function: PrintIntegerArray
* Usage: PrintIntegerArray(array, n);
* -----------------------------------
* This function displays the first n values in array,
* one per line, on the console.
*/
static void PrintIntegerArray(int array[], int n)
{
int i;
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.