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
Naya [18.7K]
3 years ago
15

Second Largest, Second Smallest Write a program second.cpp that takes in a sequence of integers, and prints the second largest n

umber and the second smallest number. Note that in the case of repeated numbers, we really mean the second largest and smallest out of the distinct numbers (as seen in the examples below). You may only use the headers: and . Please have the output formatted exactly like the following examples: (the red is user input) Enter integers (Q to quit): 1 2 1 8 8 9 Q Second largest: 8 Second smallest: 2 Enter integers (Q to quit): -7 -101 0 -2 17 Q Second largest: 0 Second smallest: -7
Computers and Technology
1 answer:
Ulleksa [173]3 years ago
3 0

Answer:

The program in C++ is as follows:

#include <vector>

#include <iostream>

using namespace std;

int main(){

   vector <int> my_num;

   string sentinel;

   int n = 0;

   cout<<"Enter integers (Q to quit): ";

   cin>>sentinel;

   while(sentinel != "Q"){

       my_num.push_back(stoi(sentinel));

       n++;

       cin>>sentinel;    }

      int n1, n2;

      n1 = my_num.at(0);      n2 = my_num.at(1);

      if(my_num.at(0)<my_num.at(1)){     n1 = my_num.at(1);  n2 = my_num.at(0);   }

      for (int i = 2; i< n ; i ++) {

          if (my_num.at(i) > n1) {

              n2 = n1;

              n1 = my_num.at(i);     }

           else if (my_num.at(i) > n2 && my_num.at(i) != n1) {

               n2 = my_num.at(i);     }  }

       cout<<"Second Largest: "<<n2<<endl;

       n1 = my_num.at(1);       n2 = my_num.at(0);

       if(my_num.at(0)<my_num.at(1)){ n1 = my_num.at(0);  n2 = my_num.at(1);   }

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

           if(n1>my_num.at(i)) {  

               n2 = n1;

               n1 = my_num.at(i);            }

           else if(my_num.at(i) < n2){

               n2 = my_num.at(i);     }  }

 cout<<"Second Smallest: "<<n2;

 return 0;

}

Explanation:

See attachment for explanation

Download cpp
You might be interested in
Draw a flowchart or write pseudocode to represent the logic of a program that allows the use to enter values for the width and l
DIA [1.3K]
In psuedocode it would be:

length = input()
width = input()

area = length * width

print(area + " sq ft");

3 0
3 years ago
cell d1 contains the value 7.877 you want cell d1 to display this value as 7.9 how can you accomplish
Georgia [21]

Answer:

You round?

Explanation:

6 0
3 years ago
Which steps allow a user to change the column layout in the Handout Master?
bonufazy [111]

Answer:

D. Click the View tab, select Handout Master, and choose the number of columns in the Slides Per Page drop-down list.

Explanation: correctEdg

3 0
2 years ago
Draw a rose and sunflower using the turtle code
insens350 [35]

Answer:

funTurtle.forward

8 0
2 years ago
Which type of ethernet framing is used for TCP IP and DEC net​
umka21 [38]
Ethernet II
Trust me u will get it right
6 0
3 years ago
Other questions:
  • What are the names of the two main characters in pirates of silicon valley
    5·1 answer
  • How many 16ths are in 3/8 of an inch
    12·1 answer
  • This exspansion slot essentially replaced PCI and AGP slots
    14·1 answer
  • Which statement pertaining to system reliability is FALSE? Select one: A. Having the latest version of software products can mak
    15·1 answer
  • Justine was interested in learning how to play the piano. Before she was allowed to even play the piano, she has had to learn
    15·2 answers
  • Which of the following situations would not require knowledge of networking?
    11·2 answers
  • Computer ethis are crime done using computer technology (true )and(false)​
    14·1 answer
  • An attacker has obtained the logon credentials for a regular user on your network. Which type of security threat exists if this
    9·1 answer
  • A draft has the following:<br> Plot<br> Details<br> Transition Words<br> Important events in order
    14·1 answer
  • How many terabytes is a 128 gigabyte SD memory card
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!