Hi! I am trying to make a simple program that would determine the number of occurrence or frequency of 20 given array elements according to their respective range. My output should be like this:
Range Frequency
0-4 0
5-9 1
10-14 3
15-19 5
20-24 7
25-29 3
However, I don't know how to determine the range of the numbers and display its frequency. I tried to use if-else statement and used another variable to store the frequency but it's no good. Can someone enlighten me about this concept? This is what I have so far:
#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;
for(int i=0; i<20; i++){
if(*ptr>=0||*ptr<=4){
f++;}
else if(*ptr>=5||*ptr<=9){
f++;}
else if(*ptr>=10||*ptr<=14){
f++;}
else if(*ptr>=15||*ptr<=19){
f++;}
else if(*ptr>=20||*ptr<=24){
f++;}
else if(*ptr>=25||*ptr<=29){
f++;}
else{}
}
cout << "Range\t\tFrequency"
<< "\n0-4\n5-9\n10-14\n15-19\n20-24\n25-29" ;
for(int i=0; i<6; i++){
cout << f << endl;}
system("pause>null");
return 0;
}