1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
BigorU [14]
2 years ago
11

Write two recursive versions of the function minInArray. The function will be given a sequence of elements and should return the

minimum value in that sequence. The two versions differ from one another in the technique we use to pass the sequence to the function. In version 1 – The prototype of the function should be: int minInArray1(int arr[], int arrSize) Here, the function is given arr, an array of integers, and its logical size, arrSize. The function should find the minimum value out of all the elements in positions: 0, 1, 2, …, arrSize-1. In version 2 – The prototype of the function should be: int minInArray2(int arr[], int low, int high) Here, the function is given arr, an array of integers, and two additional indices: low and high (low ≤ high), which indicate the range of indices that need to be considered. The function should find the minimum value out of all the elements in positions:
Computers and Technology
1 answer:
Viktor [21]2 years ago
7 0

Answer:

Following are the code to this question:

#include <iostream>//defining header file

using namespace std;//using namespace

int minInArray1(int arr[],int arrSize)//declaring method minInArray1

{

if(arrSize == 1)//use if block to check arrSize value is equal to 1  

{

return arr[0];//return first element of array

}

else //defining else block

{

int max= minInArray1(arr, arrSize-1);//use integer variable max to call method recursively  

if(arr[arrSize-1] < max)//use if block to check array value  

{

max = arr[arrSize-1];//use max to hold array value

}

return max;//return max variable value

}

}

int minInArray2(int arr[], int low, int high)//defining a method minInArray2  

{

if(low == high) //use if to check low and high variable value are equal

{

return arr[low];//return low variable value

}

else //defining else block

{

int minimum = minInArray2(arr, low+1, high);//defining integer variable minimum to call method minInArray2 recursively

if(arr[low] < minimum)

{

minimum = arr[low];//use minimum variable to hold array value

}

return minimum;//return minimum value

}

}

int main()//defining main method

{

int arr[10] = { 9, -2, 14, 12, 3, 6, 2, 1, -9, 15 };//defining an array arr

int r1, r2, r3, r4;//defining integer variable

r1 = minInArray1(arr, 10);//use r1 variable to call minInArray1 and hold its return value

r2 = minInArray2(arr, 0, 9);//use r1 variable to call minInArray2 and hold its return value

cout << r1 << " " << r2 << endl; //use print method to print r1 and r2 variable value

r3 = minInArray2(arr, 2, 5);//use r3 variable to call minInArray1 and hold its return value

r4 = minInArray1(arr + 2, 4); //use r4 variable to call minInArray2 and hold its return value

cout<<r3<< " " <<r4<<endl; //use print method to print r3 and r4 variable value

return 0;

}

Output:

please find the attached file.

Explanation:

In the given code two methods, "minInArray1 and minInArray2" is defined,  in the "minInArray1" it accepts two-variable "array and arrSize" as the parameter, and in the "minInArray2" method it accepts three integer variable "array, low, and high" as the parameter.

  • In the "minInArray1" method, and if the block it checks array size value equal to 1 if the condition is true it will return the first element of the array, and in the else block the max variable is defined, that calling method recursively
  • and store its value.
  • In the "minInArray2" method, an if the block it checks low and high variable value is equal. if the condition is true it will return a low array value. In the next step, the minimum value is defined, which checks the element of the array and uses a low array to store its value.
  • In the main method an array and four integer variable "r1, r2, r3, and r4" is defined, which calls two methods "minInArray1 and minInArray2" and use print method to print its value.
You might be interested in
Three types of neurons and their function
kramer

<u>Efferent Neurons:</u> efferent neurons are motor neurons that carry neural impulses away from the central nervous systme and towards muscles to cause movement.

<u>Interneurons: </u> a neuron which transmits impulses between other neurons, especially as part of a reflex arc

<u>Astrocytes:</u> Astrocytes are the most numerous cell type within the central nervous system (CNS) and perform a variety of tasks, from axon guidance and synaptic support, to the control of the blood brain barrier and blood flow. To perform these roles, there is a great variety of astrocytes.

Explanation:

7 0
3 years ago
So I try to login into my origin account and this popped up.(photo). But everytime I try to type in something I’m unable to clic
Oduvanchick [21]
You have to restart your computer or maybe use another internet browser to access the website your trying to reach
7 0
3 years ago
The Sleeping-Barber Problem:
s2008m [1.1K]

Answer:

Explanation:

Following are the Semaphores:

Customers: Counts waiting customers;

Barbers: Number of idle barbers (0 or 1)

mutex: Used for mutual exclusion.

Cutting: Ensures that the barber won’t cut another customer’s hair before the previous customer leaves

Shared data variable:

count_cust: Counts waiting customers. ------------copy of customers. As value of semaphores can’t access directly.

// shared data

semaphore customers = 0; semaphore barbers = 0; semaphore cutting = 0; semaphore mutex = 1;

int count_cust= 0;

void barber() {

while(true) { //shop is always open

wait(customers); //sleep when there are no waiting customers

wait(mutex); //mutex for accessing customers1

count_cust= count_cust-1; //customer left

signal(barbers);

signal(mutex);

cut_hair();

}

}

void customer() {

wait(mutex); //mutex for accessing count_cust

if (count_cust< n) {

count_cust= count_cust+1; //new customer

signal(customers); signal(mutex);

wait(barbers); //wait for available barbers get_haircut();

}

else { //do nothing (leave) when all chairs are used. signal(mutex);

}

}

cut_hair(){ waiting(cutting);

}

get_haircut(){

get hair cut for some time; signal(cutting);

}

6 0
3 years ago
________ speeds time to insights and enables better data governance by performing data integration and analytic functions inside
Lyrx [107]
<h3><em>↓Answer↓</em></h3>

<u>In-database analytics</u> speeds time to insights and enables better data governance by performing data integration and analytic functions inside the database.

3 0
1 year ago
How many pins are typical molex connector?
kondor19780726 [428]
There are 4 connectors, from left to right the pins are +5v ground ground +12v
5 0
2 years ago
Other questions:
  • How Java provides protection through stack inspection approach ?
    7·1 answer
  • How do you restore deleted notpad++ file?
    12·1 answer
  • Suppose you wanted to run a web server or ftp server from your home. what type of ip address would you want?​
    6·1 answer
  • If you “bury” a story on Digg, what have you done
    10·1 answer
  • Need help with java project please!!
    6·2 answers
  • In your own words explain how to add footer and head in a word doc.
    10·1 answer
  • Help me with this, please. Are vacuum cleaners, Cd players, and telephones considered computers? Do they store any data or proce
    8·2 answers
  • Ten examples of an interpreter
    8·1 answer
  • Who is the father of computer ethics?​
    7·1 answer
  • What is the impact of VR on Educational Learning rather than games?​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!