简体   繁体   中英

Maximum value and the number of occurrences in an array

I'm having issues in a basic program about the maximum value of an array and its number of occurrences. I don't get how the program is getting compiled. I've tested it on paper and (compiled in Visual Studio), if I give at most ten values it works fine, but if I exceed ten numbers in the array, the count is acting crazy.

Here is the code.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int n, i, v[50], max, count = 0;
    cout << "Number of elements = "; cin >> n;
    for (i = 0; i < n; i++) {
        cout << "v[" << i << "]=";
        cin >> v[i];
    }

    max = v[0];
    for (i = 0; i < n; i++)     
        if (max < v[i]) {
            max = v[i];
            count = 0;
        }
        else 
            ++count;
    cout << endl;
    cout << "Maximum value, " << max << ", occurs " << count << " times.";
    cout << endl;
    system("pause");
    return 0;
}

First, you should check that n is less than 50. You should also add braces around single statements to avoid bugs.

You also have an issue as count should be set to 1 , not 0 , and you should only increase it if you encounter the same number.

The C++ way: max = *std::max_element(v, v+n); and count = std::count(v, v+n, max); .

In the second for loop you check if the current item in the array is bigger than the current biggest; if it's true you make the new maximum the current item and set the count to 0 - you should actually set it to 1. If it isn't bigger you increment the count - that's another problem. If the current number is less than the maximum and not equal to it you shouldn't incement the count variable, but that's what your code does.

Here is an easy fix that should work:

int main()
{
    int n, i, v[50], max, count = 0;
    cout << "Number of elements = "; cin >> n;
    for (i = 0; i < n; i++) {
        cout << "v[" << i << "]=";
        cin >> v[i];
    }

    max = v[0];
    for (i = 0; i < n; i++)     
        if (max < v[i]) {
            max = v[i];
            count = 1;
        }
        else if(max == v[i])
            ++count;
    cout << endl;
    cout << "Maximum value, " << max << ", occurs " << count << " times.";
    cout << endl;
    system("pause");
    return 0;
}
#include "stdafx.h"

#include <iostream>
#include <map>

using namespace std;

int main()
{
    int n, i, v[50], max, count = 0;
    cout << "Number of elements = "; cin >> n;
    for (i = 0; i < n; i++) {
        cout << "v[" << i << "]=";
        cin >> v[i];
    }

    map<int, int> m;
    for (int i = 0; i < n; ++m[v[i++]]);

    if (!m.empty())
    {
        const pair<int, int> p = *m.rbegin();

        cout << endl;
        cout << "Maximum value, " << p.first 
             << ", occurs " << p.second << " times.";
        cout << endl;
    }

    system("pause");
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM