Like Tree1Likes
  • 1 Post By iMalc

Need Help with Basic Statistic program (array)

This is a discussion on Need Help with Basic Statistic program (array) within the C++ Programming forums, part of the General Programming Boards category; Hey Guys, I'm back again and in need of your help again. Here is my code and the problem is ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15

    Question Need Help with Basic Statistic program (array)

    Hey Guys, I'm back again and in need of your help again.
    Here is my code and the problem is that my program crashes and I don't know why. Can anyone help me debug it?
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<iomanip>    
    using namespace std;
    
    int main(){
        int array[15],cnt=0,hold,inloop,sort,mode,modetemp,modecnt;
        int cnt2=0,modecount[15][2],primecount=0,primedivisor,row=0,col=0;
        float mean,sum=0;
        cout<<"Enter Array of 15 Integers: "<<endl<<endl;
        //INPUT AND SUM
        for(inloop=1;inloop<=15;inloop++)
        {
            cout<<"Value of Array ["<<inloop<<"] - ";
            cin>>array[cnt];
            sum=array[cnt]+sum;
            cnt++;
        }
        //LOOP TO SORT ARRAY(ASCENDING)
        for(cnt=1;cnt<=15;cnt++)
        {
            for(sort=0;sort<15-1;sort++)
            {
                if(array[sort]>array[sort+1])
                {
                    hold=array[sort];
                    array[sort]=array[sort+1];
                    array[sort+1]=hold;
                }
            }
        }
        //FINDING THE MEAN AND DISPLAY    
        mean=sum/cnt;
        cout<<"Mean\t\t\t\t"<<fixed<<showpoint<<setprecision(1)<<mean<<endl;
        //INITIALIZING THE VALUE OF 2DARRAY MODECOUNT
        for(row=0;row<15-1;row++)
        {
            if(row=13)
            {
                row++;
                for(col=0;col<2;col++)
                    modecount[row][col]=0;    
                row--;
            }
            for(col=0;col<2;col++)
            {
                modecount[row][col]=0;    
            }
        }
        //modecount[0][0]=1;
        //LOOP TO FIND MODE
        for(cnt=0;cnt<15;cnt++)
        {
            for(cnt2=0;cnt2<15;cnt2++)
            {
                if(array[cnt]==array[cnt2])
                {
                    ++modecount[cnt][0];
                    modecount[cnt][1]=array[cnt2];
                //    modecount[cnt][0]++;
                }
                else if(array[cnt]!=array[cnt2])
                    modecount[cnt][0]=0;
            }
        }
        modetemp=1;
        cout<<"modecount = "<<modetemp<<endl;
           for(cnt=0;cnt<15;cnt++)
           {
               if(modecount[cnt][0]>modetemp)
               {
                modetemp=modecount[cnt][0];
                modecnt=cnt;
            }
           }
           cout<<"modecnt = "<<modecnt<<endl;
           cout<<"modecount[14][1] = "<<modecount[14][1]<<endl;
        mode=modecount[modecnt][1];
        cout<<"Median\t\t\t\t"<<array[7]<<endl;
        cout<<"Mode\t\t\t\t"<<mode<<endl;
        cout<<"Min\t\t\t\t"<<array[0]<<endl;
        cout<<"Max\t\t\t\t"<<array[14]<<endl;
        //FIND THE EVEN NUMBERS
        cout<<"Even Numbers\t\t\t";
        for(cnt=0;cnt<15;cnt++)
        {
            if(array[cnt]%2==0)
            {
                if(array[cnt]==array[cnt+1])
                {
                    cnt++;
                    if(array[cnt]==array[cnt+1])
                    {
                        cnt++;
                        if(array[cnt]==array[cnt+1])
                        {
                            cnt++;
                            if(array[cnt]==array[cnt+1])
                                cnt++;
                            else if (array[cnt]!=array[cnt+1])
                                cout<<array[cnt]<<",";
                        }
                        else if (array[cnt]!=array[cnt+1])
                            cout<<array[cnt]<<",";
                    }
                    else if (array[cnt]!=array[cnt+1])
                        cout<<array[cnt]<<",";
                }
                else if (array[cnt]!=array[cnt+1])
                    cout<<array[cnt]<<",";
            }
        }
        cout<<"\b \b";
        //FIND THE ODD NUMBERS
        cout<<endl<<"Odd Numbers\t\t\t";
        for(cnt=0;cnt<15;cnt++)
        {
            if(array[cnt]%2!=0)
            {
                if(array[cnt]==array[cnt+1])
                {
                    cnt++;
                    if(array[cnt]==array[cnt+1])
                    {
                        cnt++;
                        if(array[cnt]==array[cnt+1])
                        {
                            cnt++;
                            if(array[cnt]==array[cnt+1])
                                cnt++;
                            else if (array[cnt]!=array[cnt+1])
                                cout<<array[cnt]<<",";
                        }
                        else if (array[cnt]!=array[cnt+1])
                            cout<<array[cnt]<<",";
                    }
                    else if (array[cnt]!=array[cnt+1])
                        cout<<array[cnt]<<",";
                }
                else if (array[cnt]!=array[cnt+1])
                    cout<<array[cnt]<<",";
            }
        }
        cout<<"\b \b";
        //FIND THE PRIME NUMBERS
        cout<<endl<<"Prime Numbers\t\t\t";
        for(cnt=0;cnt<15;cnt++)
        {
            primecount=0;
            for(primedivisor=1;primedivisor<=999;primedivisor++)
            {
                if(array[cnt]%primedivisor==0)
                    primecount++;
            }
            if(primecount<=2)
            {
                if(array[cnt]==array[cnt+1])
                {
                    cnt++;
                    if(array[cnt]==array[cnt+1])
                    {
                        cnt++;
                        if(array[cnt]==array[cnt+1])
                        {
                            cnt++;
                            if(array[cnt]==array[cnt+1])
                                cnt++;
                            else if (array[cnt]!=array[cnt+1])
                                cout<<array[cnt]<<",";
                        }
                        else if (array[cnt]!=array[cnt+1])
                            cout<<array[cnt]<<",";
                    }
                    else if (array[cnt]!=array[cnt+1])
                        cout<<array[cnt]<<",";
                }
                else if (array[cnt]!=array[cnt+1])
                    cout<<array[cnt]<<",";
            }
        }
        cout<<"\b \b";
        return 0;
        system("pause");
        
    }
    Thanks in Advance Guys

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    It is good that you are using local variables, but unfortunately you have allowed your main function to grow to some 180 lines. Rather, you should break it up into smaller functions that do one thing and do it well. Here's a hint: those sections of code that you demarcate by preceding it with a comment are likely to be good candidates for separate functions.

    Once you have broken up your code into smaller functions, it will become easier to isolate parts of your code for debugging. You could use a debugger to step through your code until the point of the crash. The error is likely to lie somewhere before that point (but not necessarily immediately before the point of the crash).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    This probably isn't immediately related to the problem you're seeing, but look at line 38 really carefully.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    We haven't discussed Functions in C++ in our class yet so sadly I can't implement that in my code yet. after further analyzing I have come to a conclusion that my Initialization of 2d Array is not working because It displays trash values in it. what could be wrong in the initialization?
    Code:
        //INITIALIZING THE VALUE OF 2DARRAY MODECOUNT
        for(row=0;row<15-1;row++)
        {
            for(col=0;col<2;col++)
            {
                modecount[row][col]=0;    
                if(row=13)
                {
                    row++;
                    for(col=0;col<2;col++)
                        modecount[row][col]=0;    
                    row--;
                }
            }
        }
    I have put the if(row=13) because if I have my for(row=0;row<15;row++)
    my program seems to crash. any suggestions with that sir?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    You probably mean to use the equality operator (==) instead of the assignment operator (=).

    Code:
    cout<<"modecnt = "<<modecnt<<endl;
    Since you didn't provide us with the input you're entering into the program, can you at least tell us what value this (line 76) is printing to the screen?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by OmnibladeZ
    We haven't discussed Functions in C++ in our class yet so sadly I can't implement that in my code yet.
    What is this I don't even

    Quote Originally Posted by OmnibladeZ
    after further analyzing I have come to a conclusion that my Initialization of 2d Array is not working because It displays trash values in it. what could be wrong in the initialization?
    It looks too complicated. I would expect:
    Code:
    for (row = 0; row < 15; row++)
    {
        for (col = 0; col < 2; col++)
        {
            modecount[row][col] = 0;
        }
    }
    Actually, you don't need to do this looping to assign 0 if you want to zero initialise. Just change the declaration to:
    Code:
    int modecount[15][2] = {{0}};
    Quote Originally Posted by OmnibladeZ
    I have put the if(row=13) because if I have my for(row=0;row<15;row++)
    my program seems to crash. any suggestions with that sir?
    Well, the thing is that you are assigning 13 to row, and then the if statement always evaluates to true. However, if you're doing this to fix a crash, then there is another problem that lies somewhere before this code, but which is somehow "hidden" by this bogus code.

    Just wondering: did you program by writing a small piece of code, then compile and test, or did you try to write everything at one go and then compile and test?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    I have solved the crash problem and proud to report that the program is now well and working, I have coded everything in one go, and it was my mistake.
    I now have a simple concern about my code and need your suggestions on how to simplify a bit of it. specially this part
    Code:
    //FIND THE PRIME NUMBERS
        cout<<endl<<"Prime Numbers\t\t\t";
        for(cnt=0;cnt<15;cnt++)
        {
            primecount=0;
            for(primedivisor=1;primedivisor<=999;primedivisor++)
            {
                if(array[cnt]%primedivisor==0)
                    primecount++;
            }
            if(primecount<=2)
            {
                if(array[cnt]==array[cnt+1])
                {
                    cnt++;
                    if(array[cnt]==array[cnt+1])
                    {
                        cnt++;
                        if(array[cnt]==array[cnt+1])
                        {
                            cnt++;
                            if(array[cnt]==array[cnt+1])
                                cnt++;
                            else if (array[cnt]!=array[cnt+1])
                                cout<<array[cnt]<<",";
                        }
                        else if (array[cnt]!=array[cnt+1])
                            cout<<array[cnt]<<",";
                    }
                    else if (array[cnt]!=array[cnt+1])
                        cout<<array[cnt]<<",";
                }
                else if (array[cnt]!=array[cnt+1])
                    cout<<array[cnt]<<",";
            }
        }
    The nested if is used to SKIP the same display so it prevents something like PRIME NUMBERS 2,2,2,5,7
    instead prints PRIME NUMBERS 2,5,7
    now Is there a simpler way to skip Same display values and not using the Nested if?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by OmnibladeZ
    The nested if is used to SKIP the same display so it prevents something like PRIME NUMBERS 2,2,2,5,7
    instead prints PRIME NUMBERS 2,5,7
    now Is there a simpler way to skip Same display values and not using the Nested if?
    Just check if the current number is prime. If it is, display it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    the part i need help with is the "skip".
    in this code the upper for is used to check if the number is prime so thats settled.
    The program checks each number in the array now there are multiple same number entries like 1,1,1,2,3,1,2,5,7,5,7 in this entry the prime numbers are 2,2,5,7,5,7 without skip.
    The question is "is there another way to skip multiple same number entries and just print 1 of each like 2,5,7? Other than my solution which is the nested-ifs?"

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by OmnibladeZ
    The program checks each number in the array now there are multiple same number entries like 1,1,1,2,3,1,2,5,7,5,7 in this entry the prime numbers are 2,2,5,7,5,7 without skip.
    The question is "is there another way to skip multiple same number entries and just print 1 of each like 2,5,7? Other than my solution which is the nested-ifs?"
    Ah. In that case, check if the number already appears in the array by using a loop. If it does not, display it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    Do I have to make an array to store the prime numbers sir? or can it be implemented on the code and just omit the if and else-ifs?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    You are already storing the prime numbers: they are in the array, along with the composite numbers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15

    Thumbs up

    Oh, I saw it, Thanks sir laserlight
    I used this bit and replaced all nested ifs in my code and a huge cut on lines from 170++ to 109.
    Code:
        if(array[cnt]==array[cnt+1])
              continue;
        else if(array[cnt]!=array[cnt+1])
              cout<<array[cnt]<<",";
    Thanks for all the tips and helpful suggestions. I'll be back again soon.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    With your use of if..else-if there, you better read the following: But...Anything Can Happen! - The Daily WTF

    And certainly have a read of this too: Sieve of Eratosthenes - Wikipedia, the free encyclopedia
    Matticus likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    Thanks for the links iMalc! I have read it and now I understand my errors. Thank You for giving tips!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic array help
    By shouse in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 09:58 PM
  2. Statistic problem
    By bnkslo in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2009, 12:35 PM
  3. Basic Program but I need Help
    By michigan353 in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2005, 08:03 AM
  4. Some basic array or structure program
    By kimimaro in forum C Programming
    Replies: 15
    Last Post: 03-10-2005, 10:55 AM
  5. Basic Array help
    By diddy02 in forum C Programming
    Replies: 21
    Last Post: 08-03-2002, 03:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21