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
Which university first developed the use of email?
Arte-miy333 [17]

Answer:

Massachusetts Institute of Technology (MIT)

Explanation:

4 0
2 years ago
PLS HURRY!!<br> Look at the image below
Crank

The output will be 10.

The while loop runs until numb is equal to or less than 13.

25 - 5 = 20

20 - 5 = 15

15 - 5 = 10, which is less than 13 so the loop stops and 10 is printed to the screen.  

4 0
2 years ago
When activated, an Excel object has all the features of an Excel
Rainbow [258]
D. Workbook
Hope this helps
7 0
2 years ago
Read 2 more answers
Describe how MICR is used to read these details from the cheque​
KatRina [158]

Answer:

A MICR device reading the magnetic ink on a cheque. (the ink contains iron particles). This special ink is used to write customer account numbers on the bottom of cheques. The MICR device reads the account numbers then converts it to a form the computer can understand.

Explanation:

A MICR device reading the magnetic ink on a cheque. (the ink contains iron particles). This special ink is used to write customer account numbers on the bottom of cheques. The MICR device reads the account numbers then converts it to a form the computer can understand.

7 0
2 years ago
Explain the 3 types of control transfer structures in programming using flowchart symbols
Paul [167]

Answer:

Process Flowchart.

Data Flowchart.

Business Process Modeling Diagram.

5 0
2 years ago
Other questions:
  • A set of communication rules for the computer to follow is called, what?
    10·2 answers
  • When reading data across the network (i.e. from a URL) in Python 3, what string method must be used to convert it to the interna
    9·1 answer
  • Samantha has to create a video for her science project on the blossoming of a flower from a bud to one with fully opened petals.
    5·2 answers
  • Create a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop shou
    5·1 answer
  • Write a program to read a list of exam scores given as integer percentages in the range 0-100. Display the total number of grade
    10·1 answer
  • Choose the answer. Janice's IT department found that her computer had a program on it that was collecting her personal informati
    5·1 answer
  • 2.4 Code Practice: Question 2
    5·1 answer
  • Que es el sistema persona producto?​
    8·1 answer
  • Write c++ program to find maximum number for three variables using statement ?​
    13·1 answer
  • Data will zip through a 5G network much faster than through a 4G network because of the 10-fold difference in _____.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!