i wrote this code but don't know if the numbers i array for p will generate in that order. Can you help in how to get 5 random numbers horizontally and insure that 7,15, 22,27,31 will not generate in that exact sequence?
Here's the properly formatted version of this code. I have too little time to point out all the errors myself. So I hope other people can point out the mistakes instead.
(By the way, thread creator... See how much easier it is to notice your mistakes when you format it properly? I'd recommend you to format it like this. The code here means exactly the same as the code you gave me.)
1.) Opening brace on line 4 is not needed / closing brace on line 20 is not needed.
1.5.) Braces on lines 12 and 19?
2.) It would be wise to have a constant variable, which holds the size of the array. Hardcoding values is bad practice.
3.) float p is uninitialized, and used on line 17.
4.) Since you declared an int by the same name p, the more localized one will be used in the for loop and the line following it, because you didn't encapsulate the for loop body with braces.
#include <iostream>
#include <ctime>
int random(int min, int max) {
staticbool random=false;
if(random==false) {
srand(time(0));
random=true;
}
return (min+(rand()%max+min+1));
}
int main(int argc, char* argv[]) {
constunsignedshort size = 5;
constunsignedshort bad[size] = {7, 15, 22, 27, 31};
unsignedshort numbers[size] = {0};
for(unsignedshort i=0; i<size; ++i) {//populate the numbers array
numbers[i] = random(0, 35);
std::cout << "numbers[" << i << "]:\t" << numbers[i] << std::endl;
}
bool flag = true;
for(unsignedshort i=0; i<size; ++i) {//check to see if the numbers match the sequence
if(numbers[i]!=bad[i]) {
flag = false;
break;
}
}
//if flag is true, then the numbers match the sequence.
//below this line, you can do whatever you want to do based on whether that sequence was generated.
std::cout << "That particular sequence of numbers was " << (flag?"":"not") << " generated." << std::endl;
std::cin.get();
return 0;
}