Hello,

I have an array with 10 random numbers ranging from 1 to 10. I'm trying to write a function that would create a new array which would contain the number of times a number apperead in the first array and return a pointer to it. For example: if the function gets this array {1,1,5,6,6,6,7,8,8,10}, the array it would create would be {2,0,0,0,1,3,1,2,0,1}. The funny thing is, when i compiled my program under linux with GCC it worked fine. However, when i ran it with devCpp, the new array contains some weird large numbers.

The code:

int* stocen(int ocene[], const int N)
{
	int* stevec=new int[10];
	int oc[]={1,2,3,4,5,6,7,8,9,10};
	for(int i=0; i<N; i++)
	{
		for(int j=0; j<10; j++)
		{
			if(ocene[i]==oc[j])
			{
				stevec[j]++;
			}
		}
	}
	return stevec;
}

and here is the function call in main():

case 4:
			{
				int *stevec; 
				stevec=stocen(ocene,N);
				for(int i=0; i<10; i++)
				{
					cout<<stevec[i]<<" ";
				}
				delete [] stevec;
				break;
			}

Any help would be much appriciated, thanks!

Recommended Answers

All 2 Replies

In stocen , initialize the array elements of stevec after new ing it.

Thanks! Worked like a charm.

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.