Firstly, it seems you have only one variable for storing frequency. So its nearly impossible to measure individual frequencies.
Secondly, your algorithm isnt working because everything gets caught up in the first part (>= 0). Because all your items are >= 0, everything is caught in the first iteration.
Please consider the code below:
#include<iostream>
using namespace std;
int main()
{
int arr[] = {20,23,15,17,29,6,16,11,21,27,19,20,13,16,28,21,20,23,14,26};
int *ptr=arr;
int f[] = {0,0,0,0,0,0};
for(int i=0; i<20; i++){
if(*ptr>=0&&*ptr<=4){
f[0]++;}
else if(*ptr>=5 && *ptr<=9){
f[1]++;}
else if(*ptr>=10&&*ptr<=14){
f[2]++;}
else if(*ptr>=15&&*ptr<=19){
f[3]++;}
else if(*ptr>=20&&*ptr<=24){
f[4]++;}
else if(*ptr>=25&&*ptr<=29){
f[5]++;}
else{}
ptr++; // so it reads the next element in the array
}
cout<<"Range\t\tFrequency"
<<"\n0-4\t\t"<<f[0] //output first range
<<"\n5-9\t\t"<<f[1] // second range
<<"\n10-14\t\t"<<f[2] // third range
<<"\n15-19\t\t"<<f[3] // fourth range
<<"\n20-24\t\t"<<f[4] // fifth range
<<"\n25-29\t\t"<<f[5]; // sixth range
system("pause>null");
return 0;
}
Please also note that your expected output should be 4 for the 25-29 range (29,27,28,26)