Question

Develop a C++ program that determines the largest and second largest positive values in a collection...

Develop a C++ program that determines the largest and second largest positive values in a collection of data

Prompt the user to enter integer values until they enter any negative value to quit

  • You may presume the user will input at least two valid integer values

Create a loop structure of some sort to execute this input cycle

Maintain the largest and second largest integer as the user inputs data

  • This logic should be placed inside your loop structure
  • Arrays are not needed to solve the problem!

Display the largest and second largest values entered by the user

Homework Answers

Answer #1

[C++ CODE]

#include<iostream>
#include<stdio.h>

using namespace std;

int main()
{
        int i, high, high_2, count=0, num;              //high is the largest value and high_2 is the second largest 
        cout<<"Enter the integers value and enter -ve value to quit : "<<endl;
        do{
             cin>>num;
             count++;
             if(count==1)                       //if first integer, high and second highest will be same 
             {
                  high = num;
                  high_2 = num;
             }
             else if(num < 0)  // if num is negative it will break out 
             {
                 break;
             }
             else if(num>high)
             {
                  high_2 = high; 
                  high = num; 
             }
             else if(num>high_2)
             {
                  high_2=num;
             }
         }while(num > 0);
        cout<<endl<<"Largest value is "<<high<<endl<<"Second largest value is "<<high_2;
        return 0;
}

SCREENSHOT:

OUTPUT:

EXPLANATION:

Two integer variables high and high_2 are taken to store the largest and second-largest values.

Integer value taken by the user is stored in num variable.

the count is the number of integers taken by the user.

At first, when count=1, then high and high_2 are assigned by num value and in second input it is checked if it is greater than high then high is shifted to high_2 and num is shifted to high.

If num is greater than high_2 and less than high than num value is shifted to high_2.

And this process continues till the end of the loop.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ PROBLEM: Create a program that calculates the hyperfactorial of any positive integer n, where the...
C++ PROBLEM: Create a program that calculates the hyperfactorial of any positive integer n, where the hyperfactorial is equivalent to value obtained by the operation: 1^1*2^2*3^3*…..n^n . If user inputs the value that is not a positive value, display a message informing the user that the input is not valid and request a new input. Calculate the hyperfactorial first by creating a program that uses only nested “For loop” structure.
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
Write a C++ program to find least common multiple (LCM) of two, three and four integer...
Write a C++ program to find least common multiple (LCM) of two, three and four integer values. The integer values are entered from the keyboard and the outputs are printed to the console. The LCM of two, three and four integer values are computed using Prime factorization method. You have to use arrays to hold input values and use functions/methods to get data from the keyboard, display output to the console, calculate LCM using Prime factorization method. Your program should...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
Vectors of structs containing vectors, searching, and sorting. This program will serve to keep track of...
Vectors of structs containing vectors, searching, and sorting. This program will serve to keep track of people’s purchase records. Specifically: Create a struct type that will represent a purchase record , where each PurchaseRecord has a name (a string that may contain blank spaces), the number of purchases (1-8), the total cost of all of the purchases combined, and a vector of the individual costs for each purchase (maximum of 8 doubles). Create a vector of an unknown number of...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm to work along with a worked exercise from Chapter 5. Use two different looping strategies for different purposes. Prior Task Completion: 1. Read Chapter 05. 2. View Chapter 05’s video notes. 3. As you view the video, work along with each code sample in PyCharm using the Ch 5 Iteration PPT Code Samples.zip. Important: Do not skip the process of following along with the...