Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
Question
Book Icon
Chapter 14, Problem 4PP
Program Plan Intro

Sorting an array of integers

Program Plan:

  • Include requires header file.
  • Declare the function for fill array, sort, swap values and index of smallest.
  • Define main function.
    • Create prompt statement.
    • Declare variables for sample array and number used.
    • Call the function “fillArray”.
    • Prompt statement for index of minimum number.
    • Call the function “indexOfSmallest”.
    • Call the function “sort”.
    • Prompt statement for sorted numbers.
    • Display the sorted number using “for” loop.
  • Define “fillArray” function.
    • This is function is used to read the numbers from user.
  • Define “sort” function.
    • This function is used to sort the number by recursively call the function “sort”.
    • In this function, first declare the variable for index of next smallest.
    • If the start index is less than “numberUsed – 1”, then
      • Compute the index of next smallest number by calling the function “indexOfSmallest” with arguments of array, start index, and number of values in array.
      • Call the function “swapValues” with argument array of start index and array of next element index.
      • Increment the start index.
      • Then recursively call the function “sort” with argument array “a”, starting index and number of values in a given array “a”.
  • Define “swapValues” function.
    • This function is used to swap the two numbers.
  • Define “indexOfSmallest” function.
    • This function is used to compute the index of smallest number by calling the function “indexOfSmallest” recursively.
    • In this function, first assign the minimum to array of starting index.
    • Then if the starting index is equal to “numberUsed – 1”, then returns the starting index value.
    • Otherwise, recursively call the function “indexOfSmallest” with three arguments such as array “a”, increment of start index by “1” and “numberUsed” and this function is assigned to a variable “indexOfMin”.
    • If the value of “min” is greater than “a[indexOfMin]”, then return the minimum index. Otherwise, return the starting index.

Blurred answer
Students have asked these similar questions
Write a recursive function to sort an array of integers into ascending order using the following idea: the function must place the smallest element in the first position, then sort the rest of the array by a recursive call. This is a recursive version of the selection sort. (Note: You will probably want to call an auxiliary function that finds the index of the smallest item in the array. Make sure that the sorting function itself is recursive. Any auxiliary function that you use may be either recursive or iterative.) Embed your sort function in a driver program to test it. Turn in the entire program and the output.
Fibonacci numbers are a sequence of integers, starting with 1, where the value of each number is the sum of the two previous numbers, e.g. 1, 1, 2, 3, 5, 8, etc. Write a function called fibonacci that takes a parameter, n, which contains an integer value, and have it return the nth Fibonacci number. (There are two ways to do this: one with recursion, and one without.)
Write a recursive function that returns the smallest integer in an array. Write a test program that prompts the user to enter a list of five integers and displays the smallest integer.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning