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
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.
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.
CodeW X b For func x C Solved X b Answer X https://codeworkou... CodeWorkout X270: Recursion Programming Exercise: Count Characters For function countChr() write the missing part of the recursive call. This function should return the number of times that the letter "A" appears in string "str". Recall that str.substring(a) will return the substring of str from position a to the end of str, while str.substring (a, b) will return the substring of str starting at position a and continuing to (but not including) the character at position b. Examples: countChr ("ctcoWCAt") -> 1 Your AnsSwer: 1 public int countChr(String str) { 2. if (str.length() return 0; } (0 4. { int count = 0; www. 5. 9. if (str.substring(0, 1).equals("A")) { count = 1 7. { 9. return count + > 1:10 AM 50°F Clear 12/4/2021 呼
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