Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
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.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents 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.
Chapter 14 Solutions
Problem Solving with C++ (9th Edition)
Ch. 14.1 - Prob. 1STECh. 14.1 - Prob. 2STECh. 14.1 - Prob. 3STECh. 14.1 - Prob. 4STECh. 14.1 - Prob. 5STECh. 14.1 - If your program produces an error message that...Ch. 14.1 - Write an iterative version of the function cheers...Ch. 14.1 - Write an iterative version of the function defined...Ch. 14.1 - Prob. 9STECh. 14.1 - Trace the recursive solution you made to Self-Test...
Ch. 14.1 - Trace the recursive solution you made to Self-Test...Ch. 14.2 - What is the output of the following program?...Ch. 14.2 - Prob. 13STECh. 14.2 - Redefine the function power so that it also works...Ch. 14.3 - Prob. 15STECh. 14.3 - Write an iterative version of the one-argument...Ch. 14 - Prob. 1PCh. 14 - Prob. 2PCh. 14 - Write a recursive version of the search function...Ch. 14 - Prob. 4PCh. 14 - Prob. 5PCh. 14 - The formula for computing the number of ways of...Ch. 14 - Write a recursive function that has an argument...Ch. 14 - Prob. 3PPCh. 14 - Prob. 4PPCh. 14 - Prob. 5PPCh. 14 - The game of Jump It consists of a board with n...Ch. 14 - Prob. 7PPCh. 14 - Prob. 8PP
Knowledge Booster
Similar questions
- Provide JAVA source code with proper comments for attached assginment.arrow_forward/** numUnique returns the number of unique values in an array of doubles. * The array may be empty and it may contain duplicate values. * Unlike the previous questions, you can assume the array is sorted. * * Your solution should contain at most one loop. You may not use recursion. * Your solution must not call any other functions. * Here are some examples (using "==" informally): * * * * * Ⓒ == numUnique (new double[] { }) 1 == numUnique (new double[] {11}) 1 == numUnique (new double[] { 11, 11, 11, 11 }) 8 == numUnique (new double[] { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 }) 8 == numUnique (new double[] { 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88 }) * */ public static int numUnique (double[] list) { return StdRandom.uniform (100); //TODO: fix thisarrow_forwardBelow is the recursive implementation of binary search from lab with one change (in red): the print statement at the top of the function which prints "BOO" There is also a main function which calls bsearchR on the sorted array data[] with 7 elements and searching for 10. QUESTION: How many BOOS will be printed by this program? // Recursive Binary Search Routine that does the "Real" Work int bsearchR(int a[], int lo, int hi, int x) { int m; printf("BO0\n"); // <<<<==== NEW STATEMENT if(hi < lo) return -1; m = (lo + hi) / 2; if(a[m] == x) return m; else if(x < a[m]) return bsearchR(a, lo, m-1, x); else return bsearchR(a, m+1, hi, x); } int main() { int data[] = {5, 8, 12, 15, 20, 25, 30}; int idx = bsearchR(data, e, 6, 10); return 0; }arrow_forward
- def removeMultiples(x, arr) - directly remove the multiples of prime numbers (instead of just marking them) by creating a helper function. This recursive function takes in a number, n, and a list and returns a list that doesn’t contain the multiples of n.def createList(n) - a recursive function, createList(), that takes in the user input n and returns an array of integers from 2 through n (i.e. [2, 3, 4, …, n]). def Sieve_of_Eratosthenes(list) - a recursive function that takes in a list and returns a list of prime numbers from the input list.Template below: def createList(n): #Base Case/s #ToDo: Add conditions here for base case/s #if <condition> : #return <value> #Recursive Case/s #ToDo: Add conditions here for your recursive case/s #else: #return <operation and recursive call> #remove the line after this once all ToDo is completed return [] def removeMultiples(x, arr): #Base Case/s #TODO: Add conditions here for your…arrow_forwardNo plagarism! Correct and detailed answer ( code with comments and output screenshot if necessary)arrow_forwardBelow is the recursive implementation of binary search from lab with one change (in red): the print statement at the top of the function which prints "BOO" There is also a main function which calls bsearchR on the sorted array data[] with 7 elements and searching for 12. QUESTION: How many BOOS will be printed by this program? // Recursive Binary Search Routine that does the "Real" Work int bsearchR(int a[], int lo, int hi, int x) { int m; printf("BOO\n"); // <<<<==== NEW STATEMENT if(hi < lo) return -1; m = (lo + hi) / 2; if(a[m] == x) return m; else if(x < a[m]) return bsearchR(a, lo, m-1, x); else return bsearchR(a, m+1, hi, x); } int main() { int data[] = {5, 8, 12, 15, 20, 25, 3e}; int idx = bsearchR(data, e, 6, 12); return e; }arrow_forward
- Write a function that accepts two arguments,, an array of integers, and a numberindicating the number of elements in the array. Th e function should recursive lycalculate the sum of all the numbers in the array. Demonstrate the use of the functionin a program that asks the user to enter an array of numbers and prints its sum.arrow_forwardWrite a RECURSIVE function, without using any loops, that prints the contents of a matrix with 3 columns. The function should take the matrix and the number of rows as arguments. void print_matrix(int arr[][3], int num_rows);arrow_forwardCreate a function that returns the nth catalan number. In combinatorial mathematics, the Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involving recursively-defined objects. They are named after the Belgian mathematician Eugène Charles Catalan (1814-1894). For more info, check out the resource tab. Examples getCatalanNumber (0) → 1 getCatalanNumber (6) → 132 getCatalanNumber (8) 1430 Notes Inputs are zero and positive integers.arrow_forward
- Part I Implement the Fibonnaci SequenceOne of this week’s quiz questions referred to the Fibonnaci sequence. This sequence of numbers is definedsuch that the nth number of the sequence is simply the sum of the two previous numbers in the sequence. Informal terms, Fn = Fn1 + Fn2, where Fnis the nth Fibonnaci number. Write a function in recursion.py, calledfibonnaci, which will accept one integer parameter (lets call it n) and returns the nth element of the Fibonnacisequence.Part II Implement Euclid’s GCD AlgorithmThe greatest common divisor, or GCD, of two integers is the largest number that divides both of them withno remainder. Euclid’s algorithm is one method to find the GCD of two numbers. Mathematically, we knowthat if r is the remainder when a is divided by b, then gcd(a, b) = gcd(b, r). Write a recursive function calledgcd that takes parameters a and b and returns their greatest common divisor. Think about what the basecase is for this algorithm.Part III String…arrow_forwardWrite a recursive function that find the minimum element in an array of integers.int findMin(int a[], int size);arrow_forwardcode is to be written in C A palindrome is a word that reads the same forward and backwards. For example, “mom”, “dad”, “anna”, “racecar”, “rotor”, “radar”, etc. Write a recursive function that determines if a word and/or phrase is a palindrome. Develop 5 test char arrays in main() (no user input is required) that are passed to your recursive palindrome function to test it. Your function should not return any values but should output to the screen the word passed and whether or not it is a palindrome. You may use string.h but NO STRING REVERSE functions…you must write your own RECURSIVE string reverse function. Submit this assignment to me by sending your source code in the body of an email.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning