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