直方图计算程序

时间:2011-09-15 09:27:08

标签: c++ histogram

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int arraylength;
    int lastbig = 0;
    int lastsmall = 0;
    int temp = 0;
    int numofgroups = 0;
    double gg = 0;
    cout<<"Enter the number of numbers you are going to enter "<<endl;
    cin>>arraylength;
    int data[arraylength];
    for(int ahmet = 0;ahmet < arraylength;ahmet++)
    {
         cout<<"Enter the num no."<<ahmet+1<<endl;
         cin>>data[ahmet];
    }
    for(int bbm = 0;bbm < arraylength;bbm++)
    {
            if(data[bbm]>lastbig)
            {        
                     lastbig = data[bbm];
            }
    }
    cout<<"Biggest "<<lastbig<<endl;
    for(int ddr = 0;ddr < arraylength;ddr++)
    {
            if(data[ddr]<lastbig && lastsmall == 0)
            {
                 lastsmall = data[ddr];
            }
            else if(data[ddr]<lastsmall)
            {
                 lastsmall = data[ddr];
            }
    }
    cout<<"smallest "<<lastsmall<<endl;
    temp = lastbig-lastsmall;
    cout<<"Enter the number of groups you want"<<endl;
    cin>>numofgroups;
    gg = (double)temp/numofgroups;
    cout<<"gg ="<<gg;
    gg = ceil(gg);
    cout<<"gg ="<<gg<<endl;
    int z = 0;
    int lastnumleft = 0;
    struct groups {
           int min;
           int max;
           int membercount;
           }group[numofgroups];
           int tmp = lastsmall;
    for(int dinghy = 0;dinghy<numofgroups;dinghy++)
    {
        if(dinghy == 0)
        {
             group[dinghy].min = tmp;
             group[dinghy].max = tmp + ((int)gg - 1);
             tmp = tmp + (int)gg;
        }
        else{
                 group[dinghy].min = tmp;
                 group[dinghy].max = tmp+((int)gg-1);
                 tmp = tmp + (int)gg;
                 }
    }
    for(int jpn = 0;jpn<numofgroups;jpn++)
    {
        for(int mtr = 0;mtr<arraylength;mtr++)
        {
            if(data[mtr]>group[jpn].min&&data[mtr]<group[jpn].max)
            {
                group[jpn].membercount++;
            }                                                                 
        }
    }
    for(int dingil = 0;dingil<numofgroups;dingil++)
    {
        if(!group[dingil].membercount){
        group[dingil].membercount = 0;
        }
    }
    for(int xyz = 0;xyz<numofgroups;xyz++)
    {
            cout<<group[xyz].min<<" - "<<group[xyz].max<<" "<<group[xyz].membercount<<endl;
    }
    cin.ignore();
    cin.get();
    return 0;
}

这个程序实际上进行了直方图成员计数组和最小数字所需的计算,但我不能将这些数字分组,你可以帮助我:))

1 个答案:

答案 0 :(得分:2)

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int arraylength;
    int lastbig = 0;
    int lastsmall = 0;
    int temp = 0;
    int numofgroups = 0;
    double gg = 0;
    cout<<"Enter the number of numbers you are going to enter "<<endl;
    cin>>arraylength;
    int *data = new int[arraylength];
    for(int ahmet = 0;ahmet < arraylength;ahmet++)
    {
        cout<<"Enter the num no."<<ahmet+1<<endl;
        cin>>data[ahmet];
    }

    for(int bbm = 0;bbm < arraylength;bbm++)
    {
        if(data[bbm]>lastbig)
        {        
                 lastbig = data[bbm];
        }
    }
    cout<<"Biggest "<<lastbig<<endl;

    for(int ddr = 0;ddr < arraylength;ddr++)
    {
        if(data[ddr]<lastbig && lastsmall == 0)
        {
             lastsmall = data[ddr];
        }
        else if(data[ddr]<lastsmall)
        {
             lastsmall = data[ddr];
        }
    }

    cout<<"smallest "<<lastsmall<<endl;
    temp = lastbig-lastsmall;
    cout<<"Enter the number of groups you want"<<endl;
    cin>>numofgroups;
    gg = (double)temp/numofgroups;
    cout<<"gg ="<<gg;
    gg = ceil(gg);
    cout<<"gg ="<<gg<<endl;
    int z = 0;
    int lastnumleft = 0;

    struct groups {
           int min;
           int max;
           int membercount;
    }*group;

    group = new groups[numofgroups];
    int tmp = lastsmall;

    for(int dinghy = 0;dinghy<numofgroups;dinghy++)
    {
        if(dinghy == 0)
        {
            group[dinghy].min = tmp;
            group[dinghy].max = tmp + ((int)gg - 1);
            tmp = tmp + (int)gg;
        }
        else
        {
             group[dinghy].min = tmp;
             group[dinghy].max = tmp+((int)gg-1);
             tmp = tmp + (int)gg;
        }
    }

    for(int jpn = 0;jpn<numofgroups;jpn++)
    {

 //need to initialize as it has some garbage value and that is what it is printing out.  

        group[jpn].membercount = 0;

        for(int mtr = 0;mtr<arraylength;mtr++)
        {

 // note the equalities as you need to include the first and the last numbers

            if(data[mtr]>=group[jpn].min&&data[mtr]<=group[jpn].max)
            { 
               group[jpn].membercount++;
            }                                                                 
        }
    }
    for(int dingil = 0;dingil<numofgroups;dingil++)
    {
        if(!group[dingil].membercount){
            group[dingil].membercount = 0;
        }
    }

    for(int xyz = 0;xyz<numofgroups;xyz++)
    {
        cout<<group[xyz].min<<" - "<<group[xyz].max<<" "<<group[xyz].membercount<<endl;
    }
    cin.ignore();
    cin.get();
    return 0;
}

它现在可以正常工作:)