0

So basically I am trying to write a program which accepts an array of integers and then outputs the Max Min and smallest Mode and how many times it occurs. I have been able to find both max and min and mode, but instead of the smallest mode my code outputs the one that occurs first. And i am not sure how to handle an input with more than one mode. Below i’ll post my code: #include using namespace std;

   int main() {
  int p,max,min,mode;


    cout << "Enter the number of postive integers:"<< endl;
    cin >> p;
    int pos[p];
    cout << "Now enter postive integers!" <<endl;
    for(int i=0; i<p; i++) {
        cout << "Positive integer " << i+1 << ":";
        cin >> pos[i]; }
     max =pos[0];
     min =pos[0];
    for( int i=0; i<p; i++){
        if(pos[i]> max) max=pos[i];
        if(pos[i]< min) min=pos[i];
    }
    cout << "Max=" << max << endl;
    cout << "Min=" << min <<     mode= pos[0];
    int count[20];
    int t=0;
        for(int c=0;c<p; c++)
        {
            for(int d=0;d<p;d++)
            {
                if(pos[c]==pos[d])
                {
                    count[c]++;
                    t++;
                }
            }

    int modepos, maxno=count[0];
            for(int e=1;e<p;e++)
            { 
                if(maxno<count[e])
                {
                    maxno=count[e];
                    modepos=e;
    }
    }
    mode=pos[modepos];
            if(t==1) {
                cout << "There is no positive integer occuring more       
    than once." << endl;

            }
            else {

            cout <<"The most occuring positive integer is:"<< mode;
            cout << "\nIt occurs " << t << " times." << endl;

            } return 0; }

there may be simpler and better ways to code this but since i’m a beginner and have only learned loops/conditionals/arrays/variable declaration etc I can only use them in the program, any help will be appreciated.

apkr x
  • 11
  • 3
  • for mode you could use a map and incr the second value every time you find one, then scan once for the max... min and max you just compare and store – Grady Player Mar 15 '18 at 02:27
  • 2
    `int pos[p];` -- This is not valid C++. Arrays in C++ must have their sizes declared using a compile-time constant expression, not a variable. You say you're a beginner, well dive into the world of `std::vector pos(p);` if you want to write valid C++ code. (Yet again, `gcc` or `clang` has led another beginner programmer down the wrong path by allowing this non-standard syntax by default.) – PaulMcKenzie Mar 15 '18 at 02:28
  • 2
    A side note or two: please use longer, better identifier names, and adopt a regular indentation style. Both will help you understand your own code, and more importantly, will help us too. – Ken Y-N Mar 15 '18 at 02:34
  • It will serve you best if you debug the code yourself and figure out what's happening. Learn how to use a debugger. You probably don't want to stop everything you're doing and master a debugger this minute, so use `cout` as your temporary stand-in. At the top of every `for()`, `if()`, and `else`, `cout` your variables, then watch the console when you run the code, and see how your program is behaving. – SaganRitual Mar 15 '18 at 02:52
  • 1
    Easiest way is to just sort it. Then min = array[0]. max = array[size - 1] and then just traverse through the sorted array and count how many of each identical number there is and keep the total running and then compare the smallest number of times one was there. – Omid CompSCI Mar 15 '18 at 03:08
  • Just for reference, if you want a great library for measuring and accumulating statistical information, check out Boost Accumulators. http://www.boost.org/doc/libs/1_66_0/doc/html/accumulators.html – msmith81886 Mar 15 '18 at 03:19
  • @OmidCompsSCI I actually tried that way out too, could find min max but still having problems with how to code the logic of smallest mode. Could you give me an example/idea on how to code it out? – apkr x Mar 15 '18 at 04:59

2 Answers2

0

Do you learn about std::map? The algorithm for counting how many times of an element on an array is very simple with std::map

std::map<long, long > mapFreq;// first: for store value of array pos, second for store value's counter
mapFreq.insert(std::pair<long, long>(pos[0], 1));
for(int i = 1; i < dsize; i++)
{
    auto &it = mapFreq.find(pos[i]);
    if(it != mapFreq.end())
    {
        it->second++;
    }
    else
    {
        mapFreq.insert(std::pair<long, long>(pos[i], 1));
    }
}

Then you can loop through map Freq for what you need:

int number, counter;
for(auto it : mapFreq)
{
    if(it.second < counter)
    {
        number = it.first;
        counter = it.second;
    }
}
Le Ngoc Thuong
  • 279
  • 2
  • 8
  • I haven’t learned that actually, so i have no idea what your code is doing, would there be any way with just for loops and all? – apkr x Mar 15 '18 at 03:26
  • 1
    If you just only use array, you can ref this link: https://stackoverflow.com/questions/25143224/count-the-number-of-times-a-number-appears-in-an-array – Le Ngoc Thuong Mar 15 '18 at 04:35
  • 1
    You can even simplify to: `for(int i = 0; i < dsize; i++) { ++mapFreq[pos[i]];}`. – Jarod42 Mar 15 '18 at 09:49
0

Maybe you can try doing this:

#include <iostream>
#include <algorithm> //for sorting
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[n];
sort(a,a+n);
int b[a[n-1]];
for(int i=0;i<a[n-1];i++) b[i]=0;
for(int i=0;i<n;i++) b[a[i]]++;
cout<<"Largest number = "<<a[n-1]<<endl;
cout<<"Smallest number = "<<a[0]<<endl;
int rep=0;//repetition
int mode=0;
for (int i=0;i<a[n-1];i++){
  if(b[i]>rep){
    rep=b[i];// set times of repetition
    mode=i;// set new mode
  }
}
cout<<"Mode = "<<mode<<endl;
}
  • Congratulations on posting your first answer on Stackoverflow. Your answer currently is a code-only answer. Please also explain in your answer how your code example works and how this answers the question. See here for one of many discussions why code-only answers are not the best type of answers https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers – NOhs Mar 15 '18 at 12:07