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
professor190 [17]
3 years ago
5

Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In t

his question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times.
Computers and Technology
1 answer:
ivolga24 [154]3 years ago
3 0

Answer:

Following are the program in c++ language  

#include <iostream> // header file

using namespace std; // namespace

void largest(int a[], int size); // function declaration

int main() // main method

{

int arr[30],n,count1=0; // variable declaration

cout<<" enter the size elements in array:";

cin>>n; // read the input by user

cout<<"enter array elements:";

for(int k=0;k<n;k++) // iterating over the loop

cin>>arr[k]; // read the elements in the array

largest(arr,n); // calling function largest

return 0;

}

void largest(int a[], int n1) // function definition

{

int i, maximum, second,count=0; // variable declaration

if(n1<2) // checkiing the condition

{

cout<<"invalid input"; // print the message

return;

}

else

{

maximum =INT8_MIN; // store the minimum value of an object

second=INT8_MIN; //store the minimum value of an object

for(i = 0;i<n1; i ++) // iterating over the loop

{

if (a[i]>maximum) // comparing the condition

{

second = maximum; // store the maximum value in the second variable

maximum = a[i];

count++; // increment the count

}

else if (a[i] > second && a[i] != maximum)

{

second = a[i];

count++;

}

}

}

if(count<2)

{

cout<<"Maximum value:"<<maximum; // display the maximum value

cout<<" all the value are equal";

}

else

{

cout<<"Maximum value:"<<maximum; // display the maximum value

cout<<"Second largest:"<<second;// display the  second maximum value

}

}

Output:

enter the size elements in array:4

enter array elements:5

47

58

8

Maximum value:58 Second largest:47

Explanation:

Following are the description of program

  • In this program, we create a function largest which calculated the largest and the second largest element in the array.
  • Firstly check the size of elements in the if block. If the size is less then 2 then an invalid messages will be printed on console otherwise the control moves to the else block.
  • In the else block it stores the minimum value of an object in a maximum and second variable by using INT8_MIN function. After that iterating the loop and find the largest and second-largest element in the array .
  • In this loop, we used the if-else block and find the largest and second-largest element.
  • Finally, print the largest and second-largest elements in the array
You might be interested in
____ is the encapsulation of method details within a class.
Anestetic [448]

Implementation hiding i<u>s the encapsulation of method details within a class</u>. Implementation can be interpreted as those specifications which can be altered without altering the correctness of an application. Wrapping data/methods within classes (descriptions of the way all objects of this type will look/act) in combination with implementation hiding is called encapsulation. Information users need to know about behaviors should be available without dependence on implementation specifications.

6 0
3 years ago
Is virtualization self monitoring
NISA [10]

Answer:

No

Explanation:

Virtual and physical metrics have to be collected and analysed to look for allocation problems such as: VM sprawl, too many VMs, or improperly provisioned VMs are occurring.

4 0
3 years ago
Talon is a new game designer working on an exciting and innovative game idea, but its time to examine the idea for technical fea
GarryVolchara [31]

Answer:

I think the answer is C.

Explanation:

4 0
2 years ago
Read 2 more answers
Consider the following methods:
il63 [147K]
<span>2. basketball This is a classic case of overloading in C++. You have 2 functions, both named "printSport", but one of the functions receives an input of type double, and the other receives an input of type int. The specified method call passes a parameter of type int, so the version of printSport is called that receives a parameter of type int. And that version of printSport only prints the word "basketball". The other version of printSport is never called at all.</span>
4 0
3 years ago
How many times do you need to click the Format Painter button to apply copied formats to multiple paragraphs one right after the
const2013 [10]

The answer is (A) Twice

Format painter is that icon tool that looks like a paintbrush that is used to pick up formatting of an existing text and then paints it on the next text you select. If you wish to apply the formatting to more than one element, you should double-click the format painter and press esc key to deactivate.


5 0
3 years ago
Read 2 more answers
Other questions:
  • What bus carries a status signal back to the CPU?
    14·1 answer
  • In a video, a motionless image is called
    7·2 answers
  • Your friends know that you understand a lot about computers, both the technical details of how they operate as well as informati
    12·1 answer
  • dentify the type of observational study​ (cross-sectional, retrospective, or​ prospective) described below. A research company u
    5·1 answer
  • How do you know if your phone has a virus?
    13·1 answer
  • Explain in a few sentences the difference between analytical papers and argumentative papers.
    8·2 answers
  • We want to transmit 40 packets and the following packets are getting lost, 3,9, 25,28, 35. How many rounds are needed if
    5·1 answer
  • Explain why there is an overlap of skills and qualities in design professions​
    14·1 answer
  • .In Python, comments begin with the comment marker and continue ______.
    14·1 answer
  • In vehicles equipped with ABS, the driver's foot must remain firmly on the _________ to activate the ABS.
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!