5.2: Case Study- Find The Largest Number

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    5.2: Case Study- Find The Largest Number

    Find the largest number from a set of numbers is a common task in computer programming. A set of numbers, such as test scores from SAT or household incomes from a metropolitan area or average rainfalls in the last twenty years in Fresno County, can be used as inputs to a C++ program and the program will display the largest number in the set.

    In this section, we will discuss how to code such program.

    Again, we will need to create an empty project and open a blank source file. Then type in the code of initial setting:

    L1.11-1.png

    Declare two variables:

    L2-6 (1).png

    We use the variable a for storing the number a user enter, and use the variable m for holding the largest number among the numbers the user has entered.

    If a user only enter one number, then this number will be the largest number, so we put this number into m directly:

    L2-6 (2).png

    If a user has two numbers to enter to the program, then we will put the second number into the variable a by using a set of cout and cin statements:

    L2-6 (3).png

    When we put the second number in the variable a, we are not sure if the second number is greater than the first one. If it is, we should put it in the variable m, otherwise, we can just keep the first number in the variable m. We use an if statement to accomplish this:

    L2-6 (4).png

    At this point, the variable m is holding the largest number the user entered so far. If a user just have two numbers to enter, then the we can stop the coding and put in a cout statement to display the value of the variable m, which is largest number from the set of the numbers that the user entered.

    If a user has three numbers to enter to the program, then we will use another pair of cout and cin statements to place the third number user entered in the variable a as we did with the second number user entered:

    L2-6 (5).png

    We are not sure if the third number user entered is the largest number user entered so far. We will need to compare. But we don't need to compare the third number with both the first and the second number user entered, all we need to do is to compare the third number with the value of the variable m because m is holding the largest among the first two numbers user entered.

    If the third number is greater than the value of m, the third number will be the largest, then we will put third number into m, otherwise, we simply leave the m as it is, so the m is still holding the largest number among the three numbers user entered so far. Again, we will use an if statement to accomplish this:

    preview_id25

    If a user has only three numbers to enter the program, then we can use a cout statement to display the value of the variable m at this point and we are done with program.

    If a user has another number to enter, the fourth number, then we will repeat line 15 through line 18:

    L2-6 (7).png

    The logic will be the same: If the fourth number is greater than the value of the variable m, then the fourth number will be the largest number user entered so far, we will use it to replace the number in m, otherwise we will leave the value of the variable m unchanged. Either way, m is holding the largest number user entered.

    Assume the user just need to enter four numbers, then we are done with input, and we can use a cout statement to display the value of m at line 24 and finish the coding as shown below:

    preview

    If we take a close look at the code of this program, we realize this program actually runs a group of statements three time:

    L2-6 (9).png

    Now, we copy the code in one of the red boxes to the clipboard. Then delete the code in all three red boxes:

    L2-6 (11).png

    Type the while loop we used in "Good Morning!" program:

    L2-6 (12).png

    Delete the cout statement in line 11:

    L2-6 (13).png

    Paste the code we have copied on the clipboard in the space where the statement cout<<"Good Morning!"<<endl; used to be (line 11)

    L2-6 (10)-2.png

    Here, we use a while loop instead of three blocks of the same code, and it will work exactly the same.

    However, this program only works if a user have four numbers to enter. If a user want to enter five numbers, then we have to change the looping condition from i<3 to i<4 to allow the user enter 5 numbers (first number goes to m directly and the rest of 4 numbers will be entered into a inside the loop). If a user has 15 numbers to enter, then we have to change the looping condition to i<14. But, when we are coding this program, we don't know how many numbers a user would like to enter. If you put i<14, what if a user wants to enter 20 numbers? If you put i<19, what if a user wants to enter 6 numbers? Thus, putting in any specific number in the looping condition seems improper.

    This what we are going to do:

    Declare a new variable b and put i<b for looping condition. Then put a set of cout and cin statements at the beginning of the program (line 7 and line 8) shown as below:

    L2.6 (14).png

    Now, this program will accommodate different needs of a user. It will first ask a user how many data he/she would like to enter, than use the user's input (the value of b) as part of looping condition to control the number of iterations and produce the largest number user entered.


    This page titled 5.2: Case Study- Find The Largest Number was last modified on Wed, 24 Dec 2025 20:37:20 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.