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
Firdavs [7]
3 years ago
12

Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.

Computers and Technology
1 answer:
mash [69]3 years ago
5 0

Answer:

Program for Second largest in an array:-

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int f,s,n; //declaring 3 variables f for first largest s for second largest n is size.

    cin>>n;//taking input size of the array.

    if(n<2)//n should be greater than 2..

    cout<<"n should be greater than 2"<<endl;

    int a[n];// array of size n.

    for(int i=0;i<n;i++)

    {

        cin>>a[i];

    }

   f = s = INT_MIN;//initialising f and  s with minimum value possible.

   for (int i =0; i <n;i ++)  

   {  

       if (a[i] > f)  

       {  

           s = f;  

           f = a[i];  

       }  

       else if (a[i] > s && a[i] != f)  

           s = a[i];  

   }  

   if (s == INT_MIN)  

       cout<<"No second largest exists"<<endl;

   else

       cout<<"Second largest element is :"<<s;

       return 0;

}

Program for second smallest element is:-

#include <bits/stdc++.h>  

using namespace std;  

int main()  

{  

int f,s,n; //declaring 3 variables f for first smallest s for second smallest n is size.  

cin>>n;//taking input size of the array.  

if(n<2)//n should be greater than 2..  

cout<<"n should be greater than 2"<<endl;  

int a[n];// array of size n.  

for(int i=0;i<n;i++)  

{  

cin>>a[i];  

}  

f = s = INT_MAX;//initializing f and s with maximum value possible.  

for (int i =0; i <n;i ++)  

{  

if (a[i]<f)  

{  

s = f;  

f = a[i];  

}  

else if (a[i] < s && a[i] != f)  

s = a[i];  

}  

if (s == INT_MAX)  

cout<<"No second smallest exists"<<endl;  

else  

cout<<s;//it is the second smallest element...  

return 0;  

}

Explanation:

For Second largest:-

1. Declare 3 variables f, s and n. where n is size the array f is first largest and s is second largest.

2. Initialize f and s with minimum value possible.

3. Iterate over the array a and do the following steps:-

   1.If element at ith position is greater than f. Then update f and s.

   s=f and f=a[i].

   2.If the element is between s and f then update s as.

   s=a[i].

4. Print s because it is the second largest in the array.

For Second smallest:-

1. Declare 3 variables f, s and n. where n is size the array f is first smallest and s is second smallest.

2. Initialize f and s with minimum value possible.

3. Iterate over the array a and do the following steps:-

   1.If element at ith position is smaller than f. Then update f and s.

   s=f and f=a[i].

   2.If the element is between s and f then update s as.  

   s=a[i].  

4. Print s because it is the second smallest in the array.

You might be interested in
IS ANYONE ELSE JUST GETTING PPL PUTTING LINKS WHEN U ASK A QUESTION???
TiliK225 [7]
UGH IKR, LIKE JUST ANSWER THE FRICKIN QUESTION
4 0
2 years ago
Read 2 more answers
This Command to insert copied text anywhere in your document
harina [27]

Answer:

Paste

Explanation:

6 0
2 years ago
Read 2 more answers
In the Dognition_aggregated_by_DogID data set, what is consistent about the relationship between breeding group and number of te
snow_tiger [21]

Answer:

Toy dogs

Explanation:

Tableau is an application used for statistically analyzing and visualizing data. It is very popular for its ecstatic data visualization figures and works with all dataset types to create a tableau data format.

The Dognition_aggregated_by_DogID dataset holds data for the different dog types, breeding, and tests done on them. The consistent relation between the breeding group column and tests completed is the toy dogs.

3 0
3 years ago
You are troubleshooting a computer that is in the design phase. The problem you see is that the CPU is not receiving information
iogann1982 [59]

Answer:

Control bus

Explanation:

A control bus is a PC bus that is utilized by the CPU to speak with gadgets that are contained inside the PC. This happens through physical associations, for example, links or printed circuits.

The CPU transmits an assortment of control sign to parts and gadgets to transmit control sign to the CPU utilizing the control bus. One of the principle targets of a transport is to limit the lines that are required for communication

An individual bus licenses communication between gadgets utilizing one information channel. The control transport is bidirectional and helps the CPU in synchronizing control sign to inside gadgets and outer segments. It is included interfere with lines, byte empower lines, read/compose sign and status lines.

8 0
3 years ago
Which of the following actions should you take when turning left at an intersection?
fomenos
The answer is B Okay good luck and dont...mess it up
8 0
3 years ago
Other questions:
  • What was one of the first inventions that made it possible to communicate almost instantly?
    11·1 answer
  • What do character formats do for your document's message? A. Set how text aligns within a document B. Provide organization C. Pr
    6·2 answers
  • A franchise restaurant is attempting to understand if customers think their service is good day-to-day by summarizing a series o
    10·1 answer
  • Savings accounts usually offer _________ interest rates than checking accounts. It is _________ to access your money in a saving
    10·2 answers
  • What are the two types of formulas in excel
    9·1 answer
  • a. Fill in the blanks with suitable words: A software that controls and manages all the activities of the computer ... b. A soft
    13·1 answer
  • Truth or dare????????????
    5·1 answer
  • Select the examples that best demonstrate likely tasks for Revenue and Taxation workers. Check all that apply. Brenda works for
    11·2 answers
  • You have been asked to replace a cracked screen on a laptop. The replacement screen was delivered today, but it did not include
    7·1 answer
  • Question 7 (1 point)
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!