Write a program second.cpp that takes in a sequence of integers, and prints the second largest number 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)

Answers

Answer 1

Answer:

The program in C++ is as follows:

#include <iostream>

#include <vector>

using namespace std;

int main(){

   int n;

   cout<<"Elements: ";

   cin>>n;

   vector <int>num;

   int input;

   for (int i = 1; i <= n; i++){        cin>>input;        num.push_back(input);    }

   int large, seclarge;

   large = num.at(0);      seclarge = num.at(1);

  if(num.at(0)<num.at(1)){     large = num.at(1);  seclarge = num.at(0);   }

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

     if (num.at(i) > large) {

        seclarge = large;;

        large = num.at(i);

     }

     else if (num.at(i) > seclarge && num.at(i) != large) {

        seclarge = num.at(i);

     }

  }

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

  int small, secsmall;

  small = num.at(1);       secsmall = num.at(0);

  if(num.at(0)<num.at(1)){ small = num.at(0);  secsmall = num.at(1);   }

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

     if(small>num.at(i)) {  

        secsmall = small;

        small = num.at(i);

     }

     else if(num.at(i) < secsmall){

        secsmall = num.at(i);

     }

  }

  cout<<"Second Smallest: "<<secsmall;

  return 0;

}

Explanation:

See attachment for explanation


Related Questions

Given positive integer n, write a for loop that outputs the even numbers from n down to 0. If n is odd, start with the next lower even number.

Answers

Answer:

if(n % 2 == 0){

   for(int i = n; i >= 0; i-=2){

        System.out.println(i);

    }

}

else{

     for(int i = n - 1; i >= 0; i-=2){

        System.out.println(i);

     }

}

Sample output

Output when n = 12

12

10

8

6

4

2

0

Output when n = 21

20

18

16

14

12

10

8

6

4

2

0

Explanation:

The above code is written in Java.

The if block checks if n is even by finding the modulus/remainder of n with 2.  If the remainder is 0, then n is even. If n is even, then the for loop starts at i = n. At each cycle of the loop, the value of i is reduced by 2 and the value is outputted to the console.

If n is odd, then the else block is executed. In this case, the for loop starts at i = n - 1 which is the next lower even number. At each cycle of the loop, the value of i is reduced by 2 and the value is outputted to the console.

Sample outputs for given values of n have been provided above.

A pharmaceutical company is going to issue new ID codes to its employees. Each code will have three letters followed by one digit. The letters and and the digits , , , and will not be used. So, there are letters and digits that will be used. Assume that the letters can be repeated. How many employee ID codes can be generated

Answers

Answer:

82,944 = total possible ID's

Explanation:

In order to find the total number of combinations possible we need to multiply the possible choices of each value in the ID with the possible choices of the other values. Since the ID has 3 letters and 1 digit, and each letter has 24 possible choices while the digit has 6 possible values then we would need to make the following calculation...

24 * 24 * 24 * 6 = total possible ID's

82,944 = total possible ID's

Other Questions
Why are world records important to the city of Dubai? Valorie was on vacation in sunny California. She forgot to put on sunscreen, as a result she got a sunburn. Which body system does a sunburn affect?1. Excretory 2.endocrine 3. Integumentary Choose the inequality that represents the following graph Was Joseph A. Califano Jr. a liberal or conservative? What is the home page on a website?A. a video streaming page where the content playsB. the opening page of the websiteC. the page with contact detailsD. the page where you can log inE. an organization's page listing job openings O is the center of the circle. Assume that lines that appear to be tangent are tangent. What is the value of x? -The table shows a proportional relationship between x and y. Which is k, the constantof proportionality, such that y = kx?. x 0 03. 56. 10 9 1512 20 5% equals what fraction, in lowest terms? How did the polar bears get their white fur?A.Bears have various alleles for fur color ranging from dark to light. Those bears with the lighter fur had a better chance of survival in the biome, so they lived and reproduced passing on their light colored gene to their offspring. As generations passed, white fur became more common.B.As a polar bear grows up, its fur gradually gets lighter, so by the time they are a teenager, they have fully white fur and are then best suited to survive in their environment.C.Polar Bears migrated from the Pacific Northwest up to the Tundra and Arctic because they knew that they would have a better chance of surviving in this cold region. The American Civil Rights movement was marked by key events that changed the course of history. Choose ALL statements that correctly describe some of these events. Please answer this math question for me Simran has a bag containing white and yellow marbles. Simran randomly selects one marble from the bag,records the result, and returns the marble to the bag. The results of the first 65 selections are shown below. A white marble was selected 41 times. A yellow marble was selected 24 times.Based on these results, what is the probability that the next marble Simran selects, rounded to the nearest Solve for the value of a in the triangle below. Round your answer to the nearest tenth freedom of expression is limited in democratic society What is the total area of the figure below? (The Things They Carried)"In August of 1978 his mother sent me a brief note explaining what had happened. Hed been playing pickup basketball at the Y; after two hours he went off for a drink of water; he used a jump rope."Group of answer choicesTrueorFalse Find the exact value of sin A in simplest radical form. Choose the example that is a simile.1) it is raining cats and dogs.2) It is raining like cats and dogs.3) It is raining hard.4) It is raining outside. Charles says that the number line shows all the values that make the inequalityn2-1 true. Do you agree with Charles? Explain.4 5-3 -2 -10 1 2 3 What must occur in order for plants to grow?A. The rate of photosynthesis must be greater than the rate of respiration.B. The rate of respiration must be greater than the rate of photosynthesis.C. The rate of photosynthesis must be equal to the rate of respiration.D. Alternating rates of photosynthesis and respiration.