Concept explainers
Explanation of Solution
Recursive version of the function “indexOfSmallest”:
The recursive version of the function “indexOfSmallest” is shown below:
/* Recursive function definition for compute index of smallest element */
int indexOfSmallest(const int a[], int startIndex, int numberUsed)
{
/* Assign minimum to array of starting index */
int min = a[startIndex];
/* If starting index is equal to "numberUsed-1", then */
if(startIndex == numberUsed - 1)
/* Returns the starting index */
return startIndex;
/* Otherwise recursively call the function "indexOfSmallest" */
int indexOfMin = indexOfSmallest(a, startIndex+1, numberUsed);
/* If the value of "min" is greater than "a[indexOfMin]" */
if(min > a[indexOfMin])
/* Returns index of minimum value */
return indexOfMin;
//Otherwise
else
/* Returns the starting index */
return startIndex;
}
Complete executable program code:
The modified complete code is implemented for given function is given below:
//Header file
#include <iostream>
//Function declaration
void fillArray(int a[], int size, int& numberUsed);
//Precondition: size is the declared size of the array a.
//Postcondition: numberUsed is the number of values stored in a.
//a[0] through a[numberUsed - 1] have been filled with
//nonnegative integers read from the keyboard.
void sort(int a[], int numberUsed);
//Precondition: numberUsed <= declared size of the array a.
//The array elements a[0] through a[numberUsed - 1] have values.
//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1].
void swapValues(int& v1, int& v2);
//Interchanges the values of v1 and v2.
int indexOfSmallest(const int a[], int startIndex, int numberUsed);
//Precondition: 0 <= startIndex < numberUsed. Referenced array elements have
//values.
//Returns the index i such that a[i] is the smallest of the values
//a[startIndex], a[startIndex + 1], ..., a[numberUsed - 1].
//Main function
int main( )
{
//For standard input and output
using namespace std;
//Prompt statement
cout << "This program sorts numbers from lowest to highest.\n";
//Declare variables
int sampleArray[10], numberUsed;
//Call fill array function
fillArray(sampleArray, 10, numberUsed);
//Prompt statement
cout << "Index of minimum number in given array: ";
//Call the function "indexOfSmallest"
cout << indexOfSmallest(sampleArray, 0, numberUsed) << endl;
//Call the function "sort"
sort(sampleArray, numberUsed);
//Prompt statement
cout << "In sorted order the numbers are:\n";
//Display the sorted number using "for" loop
for (int index = 0; index < numberUsed; index++)
cout << sampleArray[index] << " ";
cout << endl;
return 0;
}
//Uses iostream:
//Function definition for fill array
void fillArray(int a[], int size, int& numberUsed)
{
//For standard input and output
using namespace std;
//Prompt statement
cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number...
Want to see the full answer?
Check out a sample textbook solutionChapter 14 Solutions
Problem Solving with C++ (10th Edition)
- write a recursive version. The function takes two string parameters, s1 and s2 and returns the starting index of s2 inside the first string s1, or -1 if s2 is not found in s1. You must not use any loops; you also cannot use the string member functions find or rfind. You may use the member functions size, at and substr. Your function must be recursive. CANNOT MODIFY GRAY AREAarrow_forward3. Write a recursive function that ассepts 8x8 array of an characters that represents a maze. Each position can contain either an X or a blank. Starting at position (0,1), list any path through the maze to get to the location (7,7). Only horizontal and vertical moves are allowed. If no path exists, write a message indicating there is no path. Moves can be made only to the locations that contain a blank. If an X is encountered, that path is blocked and another must be chosen. [6]arrow_forwardDevelop a Java function that returns true if every element of an array is inside another array. The function should return false if otherwise.arrow_forward
- Write these in Pseudocode #1a – In pseudocode, write a call to a function that passes 1 Integer variable and 1 Integer array, and saves a Boolean value in return. #1b – In pseudocode, write the function that accepts 1 Integer and 1 Integer array and returns a Boolean. In the function, search the Integer array with a for-loop, and if the Integer parameter is found in the array, return false. If the Integer parameter is not found, return true. #2a – In pseudocode, write a call to a module that passes 1 Integer variable, 1 Real variable, 1 String constant, and 1 String literal as arguments. #2b – In pseudocode, write the module header that accepts 1 Integer, 1 Real, and 2 Strings as parameters. #3 – This pseudocode has multiple problems. Fix the calling statement and the definition below so that the routine accepts 3 grades as parameters and returns the average into a variable.…arrow_forwardin c++, use parallel arrays , and srand(time(NULL)) to randomize the two cards dealt at the start Create a Blackjack (21) game. Your version of the game will imagine only a SINGLE suit of cards, so 13 unique cards, {2,3,4,5,6,7,8,9,10,J,Q,K,A}. Upon starting, you will be given two cards from the set, non-repeating. Your program MUST then tell you the odds of receiving a beneficial card (that would put your value at 21 or less), and the odds of receiving a detrimental card (that would put your value over 21). Recall that the J, Q, and K cards are worth ‘10’ points, the A card can be worth either ‘1’ or ‘11’ points, and the other cards are worth their numerical values.arrow_forwardWrite 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_forward
- Need python help. For problems 1 and 2, add your code to the file Lab2.java. Add your tests in the main function of Lab2.java. Do not use static variables in class Lab2 to implement recursive methods. Problem 1: Implement a recursive method min that accepts an array and returns the minimum element in the array. The recursive step should divide the array into two halves and find the minimum in each half. The program should run in O(n) time and consume O(logn) memory. Demonstrate the output of min on the array int [] a = { 2, 3, 5, 7, 11, 13, 17, 19, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 23, 29, 31, 37, 41, 43 } Problem 2 You have been offered a job that pays as follows: On the first day, you are paid 1 cent, on the second day, 2 cents, on the third day, 4 cents and so on. In other words, your pay doubles every day. Write a recursive method computePay that for a given day number computes the pay in cents. Assume that you accumulate all the money that you are paid. Write a…arrow_forwardWrite a C++ Program using classes, functions (recursive and otherwise), arrays and other C++ commands that reads words from a file (one column only), storing them into an array. Once the array is sorted alphabetically ascending, the user is prompted to enter a word. The program reports whether or not the word was in the array of words. and its position on the array. If the word is found, the program should report the number of letters in that word.arrow_forwardUsing C++arrow_forward
- in c++arrow_forwardWrite a recursive function double recSum(double array[], int count); that takes an int argument and returns the sum of count array entries. C++, please explain how the code works.arrow_forwardProfile the performance of the memoized version of the Fibonacci function defined in Project 6. The function should count the number of recursive calls. State its computational complexity using big-O notation, and justify your answer. The fib function header has been modified to include the counter as the second parameter. Define the Counter class, it should have three methods: __init__, increment, and __str__. When an instance of the Counterclass is passed as a parameter, the countproperty of that instance should be incremented based on the number of recursive calls. The __str__ method should return the countproperty's value as a string. Please can you change the solution to this problem here, because this is wrong. """ File: fib.py Project 11.7 Employs memoization to improve the efficiency of recursive Fibonacci. Counts the calls and displays the results. """ class Counter(object): def__init__(self,count=0): self.count=count defincrement(self): self.count+=1 def__str__(self):…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning