Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
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
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.
1. The sorted values array contains 16 integers 5, 7, 10, 13, 13, 20, 21, 25, 30,32, 40, 45, 50, 52, 57, 60. Indicate the sequence of recursive calls that are made tobinaraySearch, given an initial invocation of binarySearch(32, 0, 15).show only the recursive calls. For example, initial invocation is binarySearch(45,0,15)where the target is 45, first is 0 and last is 15.
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