C++ Finding Maximum & Minimum Values (CFMMV)



AVAILABLE:    5

This discussion focuses on understanding the basic concept of COMPARISON, where we determine the maximum or minimum value among the input values. Check out the details below:

Source Code:

#include <iostream.h>
#include <conio.h>

/**
 * Finding Maximum and Minimum Values (simple)
 */

void main() {
    int number, count, max, min;

    cout << "2. Finding the largest and smallest values. (determine the count of numbers)" << endl;
    cout << endl;
    cout << "Enter the count of numbers: ";
    cin >> count;
    cout << endl;

    for (int i = 1; i <= count; i++) {
        cout << "Enter a number: ";
        cin >> number;

        if (i == 1) {
            min = number;
            max = number;
        } else if (min > number) {
            min = number;
        } else if (max < number) {
            max = number;
        }
    }

    cout << endl;
    cout << "Smallest value: " << min << endl;
    cout << "Largest value: " << max << endl;

    getch();
}

Hope this helps & happy learning!

Comments from Netizens

Comment 1:

ZHERGIUZ CHRISTIAN (14 Jan 2016, 11:05)
Excuse me, I’d like to ask: why, when finding the minimum value, it uses else if (min > number) and for the maximum it uses else if (max < number)? Shouldn't finding the minimum use "less than"? Could you explain, please?

Comment 2:

DIMAS YUBA (28 Mar 2016, 21:46)
Does this mean it uses the MIN function? Thank you.

Comment 3:

MUHAMMAD ULYA (9 Nov 2016, 18:45)
What’s the purpose of the else above? It doesn’t contain any statement.

Comment 4:

FRANSISKUS INDRA (23 Nov 2016, 01:54)
*How can we count the number of occurrences of the max and min values? For example, input 1,1,2,3,4,5,5,5,5 results in:

  • max count = 4 (5 appears 4 times)
  • min count = 2 (1 appears 2 times)
    Can you help with this?*

Comment 5:

JUNED REKSONA DOCUMENTARY (15 Dec 2016, 21:26)
How can I sort 100 integers within one variable, arranging them from smallest to largest?

Comment 6:

MUHAMMAD AZWAR (10 Apr 2017, 17:59)
Why does #include throw an error? I’m a beginner.

Comment 7:

NOTO BAYU PRIAMBODO (19 Jun 2017, 18:07)
From the above code, how can we add a function to calculate the difference between the max and min values? Could you help with this? Thank you.

Comment 8:

ROY CANDRA (28 Oct 2017, 00:41)
How can I write code to display the most frequently and least frequently bought items, like a merchandiser in a supermarket would?

Responses:

To ZHERGIUZ CHRISTIAN:

The logic is as follows: number is a dynamic variable that stores any value (any number). On the other hand, min and max are variables that hold the previously selected values.

Example: When entering 5 random numbers, the loop runs 5 times. If the input numbers are 2, 5, 3, 7, and 1:

  • Iteration 1: Input is 2 → min and max both set to 2.
  • Iteration 2: Input is 5 → max updates to 5 as max < number.
  • Iteration 3: Input is 3 → no change.
  • Iteration 4: Input is 7 → max updates to 7.
  • Iteration 5: Input is 1 → min updates to 1.

To DIMAS YUBA:

The above program does not use functions, but you can create a function for reusability. In the example, min is just a variable name to store the minimum value.

To MUHAMMAD ULYA:

The else is optional here since it does nothing. If you wish, you can add a closing statement, such as a farewell message.

To FRANSISKUS INDRA:

To count occurrences, use separate counters for max and min. Increment these counters whenever the input matches the current max or min.

To JUNED:

To sort 100 integers, learn sorting algorithms like Bubble Sort or use the built-in sort functions available in modern C++.

To MUHAMMAD AZWAR:

This error might be due to your compiler. If you're using Dev-C++, omit the .h extension in #include.

To NOTO BAYU PRIAMBODO:

To calculate the difference, declare another variable (difference) and compute as:

int difference = max - min;
cout << "Difference: " << difference << endl;

To ROY CANDRA:

For tracking popular and less popular items, you can use an array or a map to store the count of each item's occurrences, then sort or compare their counts.

C++ Max, Min Notifications

This discussion focuses on the basic concept of comparison algorithms, where we determine the largest or smallest value from the input data. The implementation of comparisons here isn't like pure mathematics but rather falls under propositional logic. For more details, carefully review the discussion below:

Source Code:

#include<iostream.h>
#include<conio.h>

/**
 * Finding Max and Min Values with Notifications
 */
void main() {
    int i = 1, number, max, min;
    char response;

    cout << "2. Finding the largest and smallest values." << endl;
    cout << endl;

    do {
        cout << "Enter a number         : "; cin >> number;

        if (i == 1) {
            min = number;
            max = number;
        } else if (min > number) {
            min = number;
        } else if (max < number) {
            max = number;
        }

        i++;

        cout << "Input another number (y/n): "; cin >> response;

    } while (response == 'y');

    cout << endl;
    cout << "Smallest value           : " << min << endl;
    cout << "Largest value            : " << max << endl;

    getch();
}

Hope it helps, and happy learning!

Comments

  1. GALANG ADHI WIRASTO (17 May 2015, 17:14): Thanks a lot! This really helped me with my assignment 😃
  2. RYAN MAULDIY ACHBAR (15 Dec 2016, 15:12): Bro, why does it fail initially?
  3. UNKNOWN (10 Jan 2017, 19:38): What if the program is limited to 10 iterations?
  4. MUHAMMAD AZWAR (10 Apr 2017, 18:28): Same issue here, including <iostream.h> gives an error.

Response 1

  • You're welcome, Galang! Happy learning and good luck. 😊
  • Hi Ryan, what kind of failure are you encountering? Could you provide a screenshot of the error?

If you want to bypass the "input another number?" dialog, you can modify the loop as follows:

// First, add a variable for the 10 iterations, let's call it 'y'
int i = 1, y = 10, number, max, min;

do {
    cout << "Enter a number         : "; cin >> number;

    if (i == 1) {
        min = number;
        max = number;
    } else if (min > number) {
        min = number;
    } else if (max < number) {
        max = number;
    }

    i++;
    y--; // Replace user prompt with this decrement

} while (y >= 1);

For DevC++, omit the .h extension:

#include <iostream>
#include <conio>

Collection of C++ Programs for Semester 1

  1. Average
  2. Average with Predefined Count
  3. Maximum and Minimum
  4. Max and Min with Predefined Count
  5. Counter to 1000
  6. Determine Integer as Even, Odd, or Zero
  7. Determine Integer as Positive, Negative, or Zero
  8. Largest Value (Max)
  9. Largest Value from 3 Inputs
  10. Swap
  11. Shopping Receipt
  12. Installment Plan
  13. Installment Plan with Predefined Count



Questions

  1. AHMAD IZZINNAHDI (12 Mar 2015, 18:42): Any tutorials on how to create one of these programs?
  2. FATHUR ORBIT (31 Mar 2015, 08:27): Can I get the "shopping receipt" tutorial?
  3. SITI KHAIRUNNISA FITRI (22 May 2015, 06:15): How to create a program to calculate the mean, median, and mode from random data?
  4. ANITA RETNO (10 Oct 2015, 17:51): Could you create a program to determine the month? For example, pressing 'a' outputs January, and so on through December?
  5. MUHAMMAD MUCHSIN (31 Oct 2015, 19:25): Why doesn't the output for some variables show correctly? Also, how should else be placed in nested if conditions?

Responses

Hi Ahmad, feel free to request one of the programs, and I'll make it for you. Just download the code first so you can follow along while watching the tutorial video. Then try practicing it on your own without referencing the video or source code.

Hi Siti, to calculate the mean, median, and mode:

  1. Create formulas using appropriate variables and data types.
  2. Design an interface for inputting data.
  3. Design an output display for the calculated results.
Hi Anita, that's a simple program. Use loops and conditional statements like this example program for loops and branching in C++.

Hi Muhammad, if your variables don’t output correctly, it could be due to case sensitivity. Analyze the program step by step, break it into smaller parts, and debug each part before integrating them. Consider learning about Flowchart Diagrams to help visualize the algorithm.

Post a Comment

Previous Next

نموذج الاتصال