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
Why can videos be streamed from the cloud to a computer with no loss in
mezya [45]
I think the answer is B
3 0
3 years ago
Read 2 more answers
Is frequency measured in henry.
soldier1979 [14.2K]
No its measured in Hertz Hz
5 0
3 years ago
What is an individual section machine?
irina1246 [14]
The most widely used forming machine arrangement is the individual section machine (or IS machine). This machine has a bank of 5–20 identical sections, each of which contains one complete set of mechanisms to make containers. ... Sections make either one, two, three or four containers simultaneously.
7 0
3 years ago
Write a python program that prints the multiplication table of 9 ?​
ololo11 [35]
X = int(input(“Enter a number”))
table = x * 9
print table

#For the number inputed it will output that number multiplied by 9
7 0
3 years ago
Read 2 more answers
Difference between software developer and software engineer.
vredina [299]

Answer:

The core difference between the two jobs is that software developers are the creative force that deals with design and program implementation, while software engineers use the principles of engineering to build computer programs and applications.

Explanation:

5 0
2 years ago
Other questions:
  • What are the advantages of repeating a header row? check all that apply
    14·1 answer
  • Oxygen-18 has an atomic number of 8. How many neutrons are in this isotope?
    7·1 answer
  • While interoperability and unrestricted connectivity is an important trend in networking, the reality is that many diverse syste
    12·1 answer
  • Which technology forms the foundation for cloud computing? forms the foundation for cloud computing.
    12·2 answers
  • Suppose that a is a one-dimensional array of ints with a length of at least 2. Which of the following code fragments successfull
    8·1 answer
  • The CSS property of top , bottom , left , or right moves the element the specified distance from the respective edge of the clos
    7·1 answer
  • This project involves writing a program that encodes and decodes messages. The program should prompt the user to select whether
    15·1 answer
  • Por que se dice que la tecnología ha tenido muchos aportes positivos para la sociedad pero también nos ha hecho dependientes de
    9·1 answer
  • WAFL (write-anywhere file layout) Select one: a. is a distributed file system b. is a file system dedicated to single user opera
    6·1 answer
  • Copy the formula in cell M7 to the range M8:M15, and edit the copied formulas to return the value from the column indicated by t
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!