# Thread: Need Help with Basic Statistic program (array)

1. ## 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");

}```

2. 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).

3. This probably isn't immediately related to the problem you're seeing, but look at line 38 really carefully.

4. 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. 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?

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

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}};`
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?

7. 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
now Is there a simpler way to skip Same display values and not using the Nested if?

The nested if is used to SKIP the same display so it prevents something like PRIME NUMBERS 2,2,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.

9. 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?"

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.

11. 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. You are already storing the prime numbers: they are in the array, along with the composite numbers.

13. 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. 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

15. Thanks for the links iMalc! I have read it and now I understand my errors. Thank You for giving tips!