#include<iostream.h>
int main()
{
    int num,j,c=0,limit,i,k;
    int*freq=new int[];
    int*munam=new int[];
    cout<<"How many times u want to enter the numbers\n";
    cin>>k;
    cout<<"tell ur range in which u will enter\n";
    cin>>limit;
    for( i=0; i<k; i++) {
        cout<<"Enter number\t"<<i+1;
        cin>>munam[i];
        if(num>limit) {
            cout<<"invalid num";
            return 0;
        }
        for( j=0; j<limit; j++) {
            freq[j]=0;
        }
        ++freq[munam[i]];
    }
    cout<<"Which no u wanna check :P";
    cin>>num;
    cout<<"You entered "<<num<<freq[num]<<" times";
    delete[]munam;
    delete[]freq;
    return 0;
}

Dani AI

Generated

Good progress in this thread: moved from the broken initial draft to a working version, and correctly pointed out allocation and initialization problems. A few targeted, practical suggestions will make the program safer and easier to maintain.

Prefer RAII over raw new/delete: use std::vector and map the input values into the vector (subtract the starting range) so you avoid off‑by‑one and indexing bugs. Validate each input before incrementing the frequency to avoid out‑of‑bounds access.

std::vector<int> freq(range - start + 1, 0);
for (int v : inputs) {
    if (v < start || v > range) { /* reject or retry */ }
    else ++freq[v - start];
}
int occurrences = freq[query - start]; // only after validating query in range

If the allowed values are sparse or unknown, use a hash map instead of a large vector; it handles negative keys and huge ranges without wasting memory.

std::unordered_map<int,int> freq;
for (int v : inputs) ++freq[v];
int occurrences = freq[query]; // zero if not present

Other practical tips: always check input with if(!(cin >> x)) and clear the stream on bad input; use size_t or explicitly signed/unsigned types for sizes; test edge cases (start==end, single entry, query out of range). Avoid mixing 1-based and 0-based loops—pick one and be consistent. These small practices prevent the common runtime errors seen earlier in the thread.

Recommended Answers

All 8 Replies

Dunno why this code is having issues...i want that the code asks the user how many numbers he/she wants to enter ....then tell the range in which the numbers are supposed to be entered then it should tell how many TIME a certain number say (4) was entered if 4 lies in the range...PLZ HELP

First, always put your code in CODE tags...(and try not to post things in languages other than english)

Well, with a quick glance, here are a few things that i can see:

1) I don't think you can use new[] without specifying any size. You'll probably want to do something like

// using your variable names
int *freq = new int[limit];
int *munam = new int[k];

2) You have to initialize the freq array (the thing you did with the second for loop (with variable j)) first and NOT inside the main for loop (having variable i)

Implement these changes and let us know more...(but next time you post, please use code-tags)...
Also, indent your code properly and don't use single letters and/or your name as variables.

#include<iostream.h>


int main()

{



int num,j,limit,i,k;





cout<<"How many times u want to enter the numbers\n";

cin>>k;

cout<<"tell ur range in which u will enter\n";

cin>>limit;

 


int*freq=new int[limit];
int*munam=new int[k];

 for( j=0;j<limit;j++)
 {
	 freq[j]=0;
 }

for( i=0;i<k;i++)
{

	cout<<"Enter number\t"<<i+1;
	
cin>>munam[i];
	

if(num>limit)
{
	cout<<"invalid num";
	return 0;
}


	
	++freq[munam[i]];

	


}






cout<<"Which no u wanna check :P";

cin>>num;

cout<<"You entered "<<num<<freq[num]<<" times";

delete[]munam;

delete[]freq;
return 0;

}

[/qoute]

Problem solved thankss a lot
I have posted the right code above...iam also gonaa make a few adjustments in it to make it more awesome...:D thank u :P

my pleasure...

on a side note, don't start putting everything under CODE tags...:P

ryt...actually iam new on this forum...so thats why :P :D

a more modified version

#include<iostream>

using namespace std;


int main()

{



int num,j,range,start,i,k;


cout<<"How Many Times You Want to Enter the Numbers\n";

cin>>k;

cout<<"\n\nEnter starting range of entries/numbers Sir ";

cin>>start;

cout<<"\nEnter Ending limit Sir\n";

cin>>range;

 
int*freq=new int[range+1];

int*munam=new int[k+1];

for( j=start;j<=range;j++)
{


	 freq[j]=0;
 
}

for( i=1;i<=k;i++)
{

	cout<<"Enter number  "<<i<<" ";
	
cin>>munam[i];
	

if((munam[i]>range)||(munam[i]<start))
{
	cout<<"\nInvalid Number(Out of Range)\n";

	i--;

	continue;
	
}


	
	++freq[munam[i]];

	


}


cout<<"\n\nWhich No You Want to Check\n";

cin>>num;

cout<<"\n\nYou entered This Number  "<<freq[num]<<" times\n";

delete[]munam;

delete[]freq;
return 0;

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