
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 solution
Chapter 14 Solutions
Problem Solving with C++ (9th Edition)
- Design a dynamic programming algorithm for the problem described below Input: A list of numbers A = = [a1,..., an]. Output: A contiguous subsequence of numbers with the maximum sum. The algorithm must take O(n) time. You must also write and explain the recurrence. (I am looking for an i ≥ 1 and k ≥ 0 such that a + ai+1 + ···ai+k has the largest possible sum among all possible values for i and k.) Example 1: Input: A[5, 15, -30, 10, -5, 40, 10]. Output: [10, 5, 40, 10] Example 2: Input: A = [7, 5, 7, 4, -20, 6, 9, 3, -4, -8, 4] Output: [6,9,3]arrow_forwardDesign a dynamic programming algorithm for the Longest Increasing Subsequence problem described below: Input: A sequence of n integers Output: The length of the longest increasing subsequence among these integers. The algorithm must take O(n²) time. You must also write and explain the recurrence. Example 1: Input: [5, 3, 6, 8, 4, 6, 2, 7, 9, 5] Output: 5 ([3, 4, 6, 7, 9]) Example 2: Input: [12, 42, 66, 73, 234, 7, 543, 16] Output: 6 ([42, 66, 73, 234, 543])arrow_forwardDesign a dynamic programming algorithm for the Subset Sum problem described below: Input: A set of n integers A and an integer s Output: A subset of A whose numbers add up to s, or that no such set exists. The algorithm must take O(n·s) time. You must also write and explain the recurrence. Example 1: Input: A = {4, 7, 5, 2, 3}, s = 12 Output: {7,2,3} Example 2: Input: A{4, 7, 5,3}, s = 6 Output: 'no such subset'arrow_forward
- TECNOLOGIE DEL WEB 2023/2023 (VER 1.1) Prof. Alfonso Pierantonio 1. Project Requirements The project consists in designing and implementing a Web application according to the methodology and the technologies illustrated and developed during the course. This document describe cross-cutting requirements the application must satisfy. The application must be realized with a combination of the following technologies: PHP MySQL HTML/CSS JavaScript, jQuery, etc templating The requirements are 2. Project size The application must have at least 18 SQL tables The number of SQL tables refers to the overall number of tables (including relation normalizations). 3. Methodology The application must be realized by adopting separation of logics, session management, and generic user management (authentication/permissions). Missing one of the above might correspond to a non sufficient score for the project. More in details: 3.1 Separation of Logics The separation of logics has to be realizse by using…arrow_forwardWrite a C program to calculate the function sin(x) or cos(x) using a Taylor series expansion around the point 0. In other words, you will program the sine or cosine function yourself, without using any existing solution. You can enter the angles in degrees or radians. The program must work for any input, e.g. -4500° or +8649°. The function will have two arguments: float sinus(float radians, float epsilon); For your own implementation, use one of the following relations (you only need to program either sine or cosine, you don't need both): Tip 1: Of course, you cannot calculate the sum of an infinite series indefinitely. You can see (if not, look in the program) that the terms keep getting smaller, so there will definitely be a situation where adding another term will not change the result in any way (see problem 1.3 – machine epsilon). However, you can end the calculation even earlier – when the result changes by less than epsilon (a pre-specified, sufficiently small number, e.g.…arrow_forwardWrite a C program that counts the number of ones (set bits) in the binary representation of a given number. Example:Input: 13 (binary 1101)Output: 3 unitsarrow_forward
- I need help to resolve or draw the diagrams. thank youarrow_forwardYou were requested to design IP addresses for the following network using the addressblock 166.118.10.0/8, connected to Internet with interface 168.118.40.17 served by the serviceprovider with router 168.118.40.1/20.a) Specify an address and net mask for each network and router interface in the table provided. b) Give the routing table at Router 1.c) How will Router 1 route the packets with destinationi) 168.118.10.5ii) 168.118.10.103iii) 168.119.10.31iii) 168.118.10.153arrow_forwardI would like to get help to draw an object relationship diagram for a typical library system.arrow_forward
- Given the network of bridges in figure, and assuming that LAN ports on A, B, C, D, E, J are 10 Mbs (cost 100 for ports) except for ports on F, G, I, H, K which are 100Mbps LANs (cost 19 for ports) Draw the obtained spanning tree, cross the blocking state ports, and circle the designated ports and write the best cost broadcasted by each router next to its root port. list in logic level detail the expected last STP messages that will define the final status at each router.arrow_forwardNext, you are going to combine everything you've learned about HTML and CSS to make a static site portfolio piece. The page should first introduce yourself. The content is up to you, but should include a variety of HTML elements, not just text. This should be followed by an online (HTML-ified) version of your CV (Resume). The following is a minimum list of requirements you should have across all your content: Both pages should start with a CSS reset (imported into your CSS, not included in your HTML) Semantic use of HTML5 sectioning elements for page structure A variety other semantic HTML elements Meaningful use of Grid, Flexbox and the Box Model as appropriate for different layout components A table An image Good use of CSS Custom Properties (variables) Non-trivial use of CSS animation Use of pseudeo elements An accessible colour palette Use of media queries The focus of this course is development, not design. However, being able to replicate a provided design for the web is…arrow_forwardI would like to get help to draw an object relationship diagram for a typical library system.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- New Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT




