I need help with rand();. This program produces random number without repeating.
In my error list there are no errors.

Unhandled exception at 0x00414742 c++.exe: 0xC0000094: Integer division by zero.

int *numbers,range;


void randomize(unsigned numberToSelect){


for(unsigned i = 0; i < numberToSelect; i++){
   const int selectedElement = rand()%(range - i);
   const int temp = numbers[selectedElement];
   numbers[selectedElement] = numbers[range - i - 1];
   numbers[range - i - 1] = temp;
}


}

You're doing a division in line 8. Can you prove that the divisor is never zero?

No. Can you solve this problem?

He did. What happens when (range == i)?

How does your code recognize and address that situation? It doesn't, but it needs to.

I found another problem.
Unhandled exception at 0x00401022 in Reading a file.exe: 0xC0000005: Access violation reading location 0x00000024.

int *numbers;


void randomize(unsigned numberToSelect,unsigned range){


for(unsigned i = 0; i < numberToSelect; i++){

	const int selectedElement = rand()%(range - i);
	const int temp = numbers[selectedElement];
	numbers[selectedElement] = numbers[range - i - 1];//error
	numbers[range - i - 1] = temp;
}

}

That's because you still have the same basic problem.

As mentioned before, you need to find a way to GUARANTEE that the value of the caluculation (range - i) is NEVER less-than-or-equal-to zero (0). If it's zero or less when you hit that line, you are attempting to access array elements that have negative indexes. Such indexes neither exist nor are legal to access.

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.