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
The angles of a quadrilateral are in the ratio 1:2:3:4 what is the largest angle​
zheka24 [161]

Answer:

144°

Explanation:

The sum of the measures of interior angles of a quadrilateral is 360°

The ratio of angle measures is 1:2:3:4

Total ratio = 1+2+3+4=10

The angles will be :

1/10 * 360°= 36°

2/10*360°=72°

3/10*360°=108°

4/10*360°=144°

The largest angle is 144°

7 0
3 years ago
What are all the physical components called?
Alecsey [184]

Physical Components to a computer are called hardware.

3 0
3 years ago
Text box linking is for two or more text boxes in a document, when the text overflows from the first box. Which scenario is the
Julli [10]

Answer:

Explanation:

use the next box i think

8 0
3 years ago
Read 2 more answers
Which term is used to define the wires on a motherboard that move data from one part of a computer to another?
mafiozo [28]
The answer to this question is the term bus. The term bus in computers is a communication system that transfers data and information in the computer to another computer. The bus are parallel wires that can be either optical or fiber that are connected in multiple switched hubs.
4 0
3 years ago
A copy of the copyrighted work must be exactly the same as the original to infringe a copyright.
nordsb [41]

Answer:

true

Explanation:

7 0
3 years ago
Other questions:
  • What will be the biased exponent of 1,100.1? <br> A:130<br> B:127<br> C:-127<br> D:2^3
    12·2 answers
  • Write a program in C which will open a text file named Story.txt. You can create the file using any text editor like notepad etc
    9·1 answer
  • Which word most strongly appeals to pathos?
    15·1 answer
  • In Excel, what happens to the cell contents when you click and drag a cell into multiple cells?
    14·1 answer
  • Identify the primary responsibilities associated with each of the following web design roles: content writer/editor, artist/grap
    13·1 answer
  • Can you clone apps form your PC and to other PC?<br>Yes or No
    13·2 answers
  • How long does it take to be placed in a class on flvs?
    14·1 answer
  • The database must be carefully planned to allow useful data manipulation and report generation.
    10·1 answer
  • For each of the following application areas state whether or not the tree data structure appears to be a good fit for use as a s
    8·1 answer
  • Please answer my question y'all!
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!