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;
	}
Yugatha commented: Clear, Concise, to the point. No messing around and no verification needed on original post. +1

Recommended Answers

All 4 Replies

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)

commented: excellent +1

I'm not too sure why you used a pointer, especially if you said you wanted it simple. A much simpler method is:

#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 f[] = {0,0,0,0,0,0};
     
    for(int i=0; i<20; i++){
    if (arr[i]<0) continue;
    if(arr[i]>=0&&arr[i]<=4){
    f[0]++;}
    else if(arr[i]<=9){ // We can not have a lower limit because if it was lower, the previous statement would pick it up
    f[1]++;}
    else if(arr[i]<=14){ // Same as above
    f[2]++;}
    else if(arr[i]<=19){ // etc
    f[3]++;}
    else if(arr[i]<=24){ //etc
    f[4]++;}
    else if(arr[i]<=29){ //etc
    f[5]++;}
    else{}
    }
     
    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;
    }

Or, if you want to be tricky, you can use this:

#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 f[] = {0,0,0,0,0,0};
     
    for(int i=0; i<20; i++){
        if (arr[i] < 0 || arr[i]>=30) continue;
        else
            f[arr[i]/5]++; // Divide the number by 5, then increase the range accordingly
    }
     
    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;
    }

the reason this code works is because if it is outside the range, it ignores it. Otherwise, it divides the number by 5, taking only the whole number result, and increases that number in the array accordingly. Eg 0-4 / 5 = 0, 5-9 / 5 = 1, 10-14 / 5 = 2 etc.

I mean, seeing as there is no point of using the pointer if you are using a limited number For loop.

I will leave it up to you to decide which code you prefer.

Man thank you for giving light to this concept. I didn't know it was that simple, I learned more than enough from your posts. Thanks a lot :)

Man thank you for giving light to this concept. I didn't know it was that simple, I learned more than enough from your posts. Thanks a lot :)

No worries, glad I could help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.