A LITTLE PROBLEM....REQUIRE VIEW

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?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>

using namespace std;

int main()
{
srand(time(NULL));
{int array[5], i;

for(i = 0; i < 5; i++)

{array[i] = (rand() % 35)+1;

printf("%d\n", array[i]);}
{
float p;
for(int p=0; p<5;p++)
if((array [p] = 7, 15, 22, 27 ,31)== array [i]);

while ( ! (p < i) )
i++; }
}


system("PAUSE");
return 0;
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
    srand(time(NULL));
    {
        int array[5], i;

        for(i = 0; i < 5; i++)
        {
            array[i] = (rand() % 35) + 1;
            printf("%d\n", array[i]);
        }
        {
            float p;
            for(int p = 0; p < 5; p++)
            if((array[p] = 7, 15, 22, 27 ,31) == array [i]);

            while (!(p < i))
            i++;
        }
    }


    system("PAUSE");
    return 0;
}


EDIT: Also, please clarify your question. I don't get what your asking.
Last edited on
what i was saying is i would like the output display of the numbers horizontally.
exactly like this:

7, 15, 22, 27, 31

And also if it generates random then do not allow this particular to sequence as above to ever generate.
Here are some things I noticed:

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.

5.) Line 15 is totally wrong for several reasons.

6.) Lines 17 & 18..........???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <ctime>

int random(int min, int max) {
	static bool random=false;
	if(random==false) {
		srand(time(0));
		random=true;
	}
	return (min+(rand()%max+min+1));
}

int main(int argc, char* argv[]) {
	const unsigned short size = 5;
	const unsigned short bad[size] = {7, 15, 22, 27, 31};

	unsigned short numbers[size] = {0};

	for(unsigned short 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(unsigned short 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;
}
Last edited on
Topic archived. No new replies allowed.