generating array elements randomly with specific parameters

Greetings all!

I know this is the first time I've posted here, however any help on the issue I'm having would be massively appreciated, I've been trying for a while to crack this now but with limited success!!

I am essentially trying to implement code that will generate an array of random integer values (S[ind]) for 'co-use' with the values of an existing array A[a] so that these random values can be used as references in forming array B[a] (I shall try to explain this better below if it seems a bit confusing). In this case, 'ind' and 'a' are parameter inputs selected by the user (ind<a as a rule).

It should check the randomly produced values so that [this is the part Im having problems with I believe];
- the same random value doesn't occur twice within S[ind] for the individual array element in A[a] that 'S' is being generated for i.e. S[0] != S[1, 2...(ind-1)] and S[1] != S[0, 2...(ind-1)] and so on.. [Condition 1, see code below]
- the random array 'S', that is generated for each value of B[a] doesnt contain a value that is the same as the reference of the 'B' element that we are generating for. i.e. for B[1], 1 doesnt come out in S[ind] [Condition 2]

The idea is that the randomly generated values are then used as array references for A[a] in generating B[a]. The latter is done by adding up all the numerical values of A[a] that are referenced by our random values then checking against certain conditions (not necessary to mention here), and then at the end of the program B[a] = A[a] so that the cycle continues etc

Here's what I've done upto now, I've been writing this in a seperate source file from the rest of my code to make sure it's right before cutting/pasting etc.


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
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime> 
#include <windows.h> 


using namespace std;

int main()
{
    
int a = 7;
int ind = 3;
int S[ind], B[a]; 
int A[7] = {1, 1, 1, -1, -1, 1, -1};   

srand(time(NULL));
    
for (int g=0;g<ind;g++)
    {
    S[g] = rand()%(a-1);
    
    do {
    S[g] = rand()%(a-1);
    cout<<"2nd\n";
       } while ((A[(S[g])]) == A[g]);       //Condition 2(?)
    for (int j=0;j<ind;j++)               
        {     
         if (g != j)          
            {   
                do {
                S[g] = rand()%(a-1);
                cout<<"1st\n";
                   } while (S[g] = S[j]);   //Condition 1 - checks all values 
            }                                //against one-another(?)
        }
    }
        
for (int r=0;r<ind;r++)     //Prints formed array of random values 
    {                          //(it doesnt get to this stage)
    printf("\n", S[r]);    
    }
 
cin.get();
}



In this case, the cout's are used as indicators when I run the program of what recurrence is happening. Is the way Im testing the generated values correct? The program compiles fine, however I get an infinite loop of cout's when this is not expected, possibly because one or both of the conditional statements are met all the time? I think this is just down to limitations of my understanding of C++ as it stands (hopefully to be changed with enough time and patience haha)

Just to clarify, acceptable random array's may be;
(for B[0]) S[] = {1, 2, 4, 7} (ind = 4, a>7)
(for B[1]) S[] = {0, 3, 5, 6}

Not acceptable;
(for B[2]) S[] = {0, 2, 4, 5} or S[] = {1, 1, 3, 5}

These references are then used in this manner (syntax may be incorrect);
B[0] = A[S[0]] + A[S[1]] ...A[S[ind-1]]...(is this a good way of adding up the values, by using a for loop?)

Many thanks in advance for any help on this, it's been a bit of a nightmare (Im sure gettin to grips with new coding techniques always is haha)
Regards, Ryan

Compiler: Dev-C++
OS: XP Pro

Last edited on
Line 16: Variable-sized stack arrays are not allowed by the standard.
Line 36: Assignment operator instead of equality operator.

This is not an error, but it makes it harder to read: (A[(S[g])])
An equivalent form would be A[S[g]]
Last edited on
Hey helios,
Thanks for the reply, do you think this is all good aside from the 2 things you mentioned then? could you possibly explain in more detail the difference between the assignment and equality operators please? as far as I know line 36 has the assignment operator already...? Are variable sized stack arrays going to trip this program up? in what way? sorry about the 20 questions, Ive just gotta figure this out so I can properly get on with my work. cheers
I meant you're using the assignment operator, which assigns the right hand operand to the left hand operand, inside a conditional. This is not necessarily an error, but it's in most cases a typo, as what's really meant is to use the equality operator, which evaluates to true if the operands are equal. This seems to be one of those cases, as your comment suggests.
By the way, the assignment operator evaluates to the value of the right hand operand, such that (a=4)==4, and (a=0)==0.
Last edited on
Ah yea that's better, no infinite loop now. my output typically looks like this now (varies from run to run as desired, the ratio of each 'cout' being dependent on the ratio of 'ind' to 'a'. but typically);

1st (expected cout's)
2nd
2nd
1st
1st
2nd
2nd
2nd...
(until the values of S come out)
1
1
4
In many cases there are recurrent values which is problematic. any ideas why? could I possibly place the conditionals into the same do-while loop so that both conditions are checked simultaneously?
thanks for the help helios
Topic archived. No new replies allowed.