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
You would like to create a graph that shows how much of your total investments are in stocks, savings, and mutual funds. You sho
sdas [7]
The answer is D. line graph
4 0
3 years ago
Read 2 more answers
Which heading size fits for the word headings​
hjlf
The first one h4 is yhat
3 0
3 years ago
Read 2 more answers
How can a user begin to work with a new sheet in excel?
Hitman42 [59]
I hope I'm answering this correctly I'm not too smart about it so I hope that sounds right if you arewanting to start with a new sheet in excel do one of the following click the new sheet button on the screen. now if you want to insert a new worksheet in front of its system worksheet and then click Home>insert> insert sheet. also Excel gives you three worksheets in a workbook, you can add much more worksheets and of course other types of sheets examples are micro sheet or dialog sheet. then you can rename them or delete them. so you can give any work sheet any name you want. sheets are displayed by default but if you don't see them click options>adcanced> display for options for the worksheet> show sheet tabs.
3 0
3 years ago
The default color scheme is called ____.
notsponge [240]
What program is this regarding?

If it's a Microsoft Office product, the default colour scheme is "office"
5 0
3 years ago
Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and events (like the 10 or 90
Norma-Jean [14]

Answer:

The C code for the problem is given below

Explanation:

#include <stdio.h>

int main() {

   int highwayNumber;

   int primaryNumber;

   scanf("%d", &highwayNumber);

   if (highwayNumber >= 1 && highwayNumber <= 999) {

       if (highwayNumber <= 99) {

           if (highwayNumber % 2 == 0) {

               printf("I-%d is primary, going east/west.\n", highwayNumber);

           } else {

               printf("I-%d is primary, going north/south.\n", highwayNumber);

           }

       } else {

           primaryNumber = highwayNumber;

           highwayNumber %= 100;

           if (highwayNumber % 2 == 0) {

               printf("I-%d is auxiliary, serving the I-%d, going east/west.\n", primaryNumber, highwayNumber);

           } else {

               printf("I-%d is auxiliary, serving the I-%d, going north/south.\n", primaryNumber, highwayNumber);

           }

       }

   } else {

       printf("%d is not a valid interstate highway number.\n", highwayNumber);

   }

   return 0;

}

4 0
3 years ago
Read 2 more answers
Other questions:
  • Press the ____ key(s) to erase one character to the left of the insertion point.
    10·2 answers
  • Some technologies like vertical farming have a number of negative effects. Which is a negative outcome of this technology? A. In
    10·1 answer
  • Which of the following is a reason why the comments tool within PDF documents could be helpful? (Select all that apply.)
    5·1 answer
  • William has an internet connection that does not allow him to make calls when connected to the Internet. What internet service c
    8·2 answers
  • In each iteration, selection sort (small) places which element in the correct location?
    9·1 answer
  • What does social protocol means in network?
    9·1 answer
  • Each computer on a network requires a unique way to identify itself and to refer to other computers. This is accomplished by usi
    8·1 answer
  • The list method reverse reverses the elements in the list. Define a function named reverse that reverses the elements in its lis
    10·1 answer
  • Write an algorithm for finding the perimeter of a rectangle
    6·1 answer
  • B) The company's chief financial officer recognizes the need for an upgrade to the smart watches, but does not understand why th
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!