Hello!!

I have an array filled with figures stored in an input file. My program has read the input file, and filled the array with the contents.

I also have a random number generator. I have made this make 2 random numbers between 0 and 1 at a time. I use a seed file to ensure the different runs each time.

My array looks like this:

1 0 0
2 0 0
3 1 2
4 1 2
5 3 0

I would like to have a 4th and 5th column to my array, and in the spaces next to my previous data, store the 2 random numbers my generator makes. So for example, it may look something like this:

1 0 0 0.5 0.9
2 0 0 0.1 0.2
3 1 2 0.3 0.8
4 1 2 0.7 0.2
5 3 0 1.0 0.7

Could anyone give me a hand with the code?

Thanks in advance!!

Recommended Answers

All 32 Replies

Hmm. I thought I had posted a few minutes ago, but I don't see it. I'll post again. You have a mix of doubles and integers, so the 2-D array from the last problem will no longer do the job. I'd create a struct of three integers and two doubles like this:

struct rowData
{
     int data[3];
     double randomNum[2];
};

You'll have a one dimensional array of type rowData. Something like:

rowData rows[100];

since you have a maximum of 100 rows of data.

Well, I guess when it comes to the end of it, I don't actually want to store these decimals in the array. I want each cell in the array to store:

1 if random number is >=0.6
0 if random number < 0.5

Does this make a difference? This was going to be my next question actually.

Well if the data looks like this:

1 0 0 0.5 0.9
2 0 0 0.1 0.2
3 1 2 0.3 0.8
4 1 2 0.7 0.2
5 3 0 1.0 0.7

If you are changing the above decimals to either zeroes or ones, keep the 2-D array. You'll have a 100 x 5 two dimensional array of integers. Don't use the struct that I set out in the last post if this is the case. It doesn't sound like you are storing the decimal numbers from 0 to 1 permanently at all so you won't need an array for them. You'll still read the numbers in from the data file the exact same way. No need to change that since these decimals are not read in from the file.

What happens when 0.5 <= random number < 0.6?

Ok great. Yes, the only things I want to store in the array are either 1's or zero's. So I will need a 100x5 array. What I have so far reads the file, places it in a 10x3 array, and creates 2 random numbers between 0 and 1. The code for this is below:

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int j;
int matrix[101][3] = {0};
int numCols = 3;
int numRows = 0;
void SetSeed();

main () {

  float value;
  int counter;
  const int RANGE = 1.1;

  SetSeed();

  for (counter = 1;counter <= 2;counter++){

    value = (float) rand() * RANGE / RAND_MAX;

    printf("Random Number between 0 and %d: %.1f \n", RANGE, value);
  }

  }

  void SetSeed() {

  FILE *seed_file;
  long int seed_value;

  seed_file = fopen("seed.txt","r");
  fscanf(seed_file,"%d",&seed_value);
  fclose(seed_file);
  srand(seed_value);
  seed_file = fopen("seed.txt","w");
  fprintf(seed_file,"%d",rand());
  fclose(seed_file);

  ifstream inFile;
  inFile.open("input.txt");

    if (!inFile) 
      {
        cout << "Unable to open input file";
        exit(1); // terminate with error
      }

  while (inFile >> matrix[numRows][0])
    {
      printf ("%d\t", matrix[numRows][0]);
      for (j = 1; j < numCols; j++)
        {
          inFile >> matrix[numRows][j];
          printf ("%d\t", matrix[numRows][j]);
        }
      numRows++;
      printf ("\n");
    }

  inFile.close ();
  }

If I change lines 6 and 7 to form an array of 5 rows, it just fills all of the rows with my infile, as follows:

1   0   0    2   0   
0   3   0    0   4
1   2   5    1   2
6   0   0    7   3  
4   8   5    6   9   
7   8   10  7   8

and I really dont want it to do this :(

For my file, I want the random number generator to generate 20 numbers (because there are 10 rows, with 2 columns needing filling. But, if there are 50 rows in my infile, I would need 100 random numbers generated!

As you can see, the input file has the numbers 1 to 10 listed in the first column. This is actually a family tree and the second 2 numbers relate to father and mother number. If for the people with 0 0 as their second two numbers, these people have unknown parents. For these people I want a '0' to appear if the random number is <=0.4 and I want a '1' to appear if the random number is >=0.5.

For the people that do not have 0 0 in their family tree code, I want a zero to appear if the random number is <=0.5 and a 1 to appear if the random number is >=0.6.

Sorry, I do realise this is a lot of work, but I'm struggling with the concept of generating the right amount of random numbers needed and translating them into zero's and 1's in the table. If you could help me out with any code at all that would be fantastic.

Thanks!!

Oh and for the example of my previous post:

1 0 0 0.5 0.9
2 0 0 0.1 0.2
3 1 2 0.3 0.8
4 1 2 0.7 0.2
5 3 0 1.0 0.7

I would want this to be finalised in my array as:

1 0 0 1 1
2 0 0 0 0
3 1 2 0 1
4 1 2 1 0
5 3 0 1 1

The decimals are translated to the numbers 0 and 1 depending on whether the individuals we are working with have either a 0 0 (first two lines) or not.

I assume by this:


If I change lines 6 and 7 to form an array of 5 rows, it just fills all of the rows with my infile, as follows:

1 0 0 2 0
0 3 0 0 4
1 2 5 1 2
6 0 0 7 3
4 8 5 6 9
7 8 10 7 8

and I really dont want it to do this :(

you are referring to this line of code that I suggested earlier?

int numCols = 3;

You are correct that you do not want to change this line from 3 to 5. I assume you mean "columns" above, not "rows"? numCols refers to the number of columns in your input file, not your matrix. You need to change the line that was originally above that line to this:

int matrix[101][5] = {0};

You should end up with this:

int matrix[101][5] = {0};
int numCols = 3;

That is not a typo. 3 and 5 are intentionally different. You have two more columns in your matrix than were in your file. You can rename numCols to numColsInFile if you want to be less confusing. Incidentally you should change the 101 to 100. Indexes in C++ start at 0, not 1. So you should refer to line 1 of your input file as index 0 and line 100 of your file (if it exists), as index 99. If you look at the code I originally gave you, notice that the numRows variable is incremented AFTER the data is read in, not before.

Here's what you can do. Add to this loop:

while (inFile >> matrix[numRows][0])
{
printf ("%d\t", matrix[numRows][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}


/* generate 2 random integers */


/* add code below to add random numbersto matrix.  Assume they are called rand1 and rand2 */

matrix[numRows][numCols] = rand1;
matrix[numRows]numCols + 1] = rand2;

/* end added code */



numRows++;
printf ("\n");
}

Looks like they're commenting on how to do random integers in the other thread, so I won't do it here.

Ok, I'm definitely getting somewhere now. I still can't get it to work, but I'm on the right track.

First of all, my random number generator generates 2 numbers at a time. These are instantly converted to either a 0 or a 1. The random numbers are stored in the float 'value', 2 at a time. But I just did an if statement to make it print either a 0 or 1 to the screen.

Therefore, the value float does not store the 1 and 0. It would store something like, 0.4 and 0.8:

float value;
int counter;
const int RANGE = 1.0;

SetSeed();

for (counter = 1;counter <= 2;counter++){

value = (float) rand() * RANGE / RAND_MAX;

if(value<0.31)
printf("\n0\n");

else
if(value>0.31)
printf("\n1\n");
}

}

So the question i, how do I store the two results as 1 and 0 separately in a variable?

Also, I only want this to work for the people who have 0 0 at the end of their line.

At the moment, my code stands as this:

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int j;
int matrix[100][4] = {0};
int numCols = 3;
int numRows = 0;
void SetSeed();

int main () {

float value;
int counter;
const int RANGE = 1.0;

SetSeed();

for (counter = 1;counter <= 2;counter++){

value = (float) rand() * RANGE / RAND_MAX;

if(value<0.31)
printf("\n0\n");

else
if(value>0.31)
printf("\n1\n");
}

}

void SetSeed() {

FILE *seed_file;
long int seed_value;

seed_file = fopen("seed.txt","r");
fscanf(seed_file,"%d",&seed_value);
fclose(seed_file);
srand(seed_value);
seed_file = fopen("seed.txt","w");
fprintf(seed_file,"%d",rand());
fclose(seed_file);

ifstream inFile;
inFile.open("input.txt");

if (!inFile)
{
cout << "Unable to open input file";
exit(1); // terminate with error
}

while (inFile >> matrix[numRows][0])
{
printf ("%d\t", matrix[numRows][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}


matrix[numRows][numCols] = value;
matrix[numRows][numCols + 1] = value;









numRows++;
printf ("\n");
}

inFile.close ();
}

This includes the new lines suggested in the previous post. However, the output is still the same three columns with the random 1 or 0 at the bottom.

Did this compile? I couldn't get it to compile. The code tags got a little messed up so maybe what posted isn't what compiled or something or maybe I got confused and cut and pasted the wrong code into Dev C++. At the least, it should give you an error when you try to access the variable called value in the SetSeed function:

void SetSeed() {

FILE *seed_file;
long int seed_value;

seed_file = fopen("seed.txt","r");
fscanf(seed_file,"%d",&seed_value);
fclose(seed_file);
srand(seed_value);
seed_file = fopen("seed.txt","w");
fprintf(seed_file,"%d",rand());
fclose(seed_file);

ifstream inFile;
inFile.open("input.txt");

if (!inFile)
{
cout << "Unable to open input file";
exit(1); // terminate with error
}

while (inFile >> matrix[numRows][0])
{
printf ("%d\t", matrix[numRows][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}

[B]
matrix[numRows][numCols] = value;
matrix[numRows][numCols + 1] = value;[/B]

numRows++;
printf ("\n");
}

inFile.close ();
}

I don't see where you declare it in that function and you don't declare it globally so this shouldn't compile as far as I can tell. Second, the whole approach to the random numbers puzzles me. Are you keeping the floats that you generate? If not, I don't see the need to even have a float rather than simply:

int rand1 = rand () % 2;
int rand2 = rand () % 2;

This gives you two random numbers of either 0 or 1, which is what I thought you were after. I don't understand what SetSeed is supposed to do or why you are reading the input file in SetSeed. Also, if you need a 5 column matrix of integers, this won't work:

int matrix[100][4] = {0};

The 4 needs to be changed to 5.

I need to call a 0 with probability of 0.4, and a 1 with probability of 0.6. Therefore, I randomly generated numbers between 0 and 1 and if they are 0.4 or less, I call it a zero, and if it is 0.5 or above I call it a 1.

Basically I just need to sort my code out so that separate random numbers are created which are then translated into a 0 or 1 (depending on the probabilities, right?) I then want to store these in separate variables and place them in the array next to the individuals that end 0 0.

Does this make more sense?

For example:

Output is:

1 0 0
2 0 0
3 1 2
4 1 2
5 3 0

with the random numbers 0.1 and 0.9 generated. The 0.1 translates to a 0 and the 0.9 to a 1. So I would want the first line to look like this:

1 0 0 0 1
2 0 0
3 1 2
4 1 2
5 3 0

As individual 2 ends in 0 0 I want to do the same for this one too.

And no, it doesn't compile for me, and yes I do need a [5], thanks for that... Ahh this is so complicated I don't know if I'm gonna get it done :(:(

Thanks for all your help though, seriously thank you. If you could provide me with any more help that would be fantastic... I'll post some functioning code later on.

Also, I don't think I want to run the input file through set seed ...

My code is getting jumbled Im sorry, Im not a great programmer (hence me needing all the help).

How would you order the code?

Basically I want my program to do the following:

1. Open input file
2. Place file into the array leaving two columns on the right free
3. Generate enough random numbers for every person that ends 0 0 in the list (2 each)
4. Translate these into 0's or 1's and place them into the right hand columns in the array

And that's pretty much it! If you have any suggestions of the altering or assortment of my code, feel free to tell me, i need to learn...

O.K. That makes sense. Let's break your project down into a few separate problems:

Part 1: Declaring the 2-dimensional integer array
Part 2: Reading the file into the 2-Dimensional array
Part 3: Seeding the random number generator.
Part 4: Getting random numbers of 0 and 1 with the probabilities specified.
Part 5: Storing those random numbers in the array.
Part 6: Displaying the array.

I'll bounce around a little. Parts 1 and 2 I think you have a handle on. You say you have part 4 working, at least as far as getting a random number into a float. I'm a little confused by your SetSeed function. It looks like you are doing a lot more than seeding the random number generator in that function, so I would rename it to be a little more descriptive. It looks like you are opening a file, reading a number from that file, closing it, seeding the random number generator, opening up the same file, creating a random number, and writing that number to that same file? I guess you could do that to seed the RNG, but normally I just do the following ONCE at the top to seed the RNG:

srand (time(NULL));
rand ();    // I'm not positive this line is needed, but it seems to help the randomness,
            // not sure why

You would need to include the ctime library to use the time function. You said you were using a really old compiler, so maybe you need to include the time.h library instead? I think they're actually the same library. I use ctime and it works, but I have an up-to-date compiler. That seems easier to me than what you are doing with the file. So that's Part 3.

Parts 4 and 5:

I need to call a 0 with probability of 0.4, and a 1 with probability of 0.6. Therefore, I randomly generated numbers between 0 and 1 and if they are 0.4 or less, I call it a zero, and if it is 0.5 or above I call it a 1.

So you have your random number in your float. I'm guessing your variable "value" is a float. You need them to be integers. So something like this:

for (int i = 0; i < 2; i++)
{
     // Put random number between 0 and 1 into value variable.
     if (value < 0.4)
          matrix[numRows][numCols + i] = 0;
     else
          matrix[numRows][numCols + i] = 1;
}

I didn't touch part 6 in this post, but hopefully this'll get you jump started.

Also, I don't think I want to run the input file through set seed ...

My code is getting jumbled Im sorry, Im not a great programmer (hence me needing all the help).

How would you order the code?

Basically I want my program to do the following:

1. Open input file
2. Place file into the array leaving two columns on the right free
3. Generate enough random numbers for every person that ends 0 0 in the list (2 each)
4. Translate these into 0's or 1's and place them into the right hand columns in the array

And that's pretty much it! If you have any suggestions of the altering or assortment of my code, feel free to tell me, i need to learn...

Hadn't noticed this post when I posted my earlier post about 15 minutes ago. Then I thought I had responded to THIS post but it didn't seem to, so if I double post, sorry. I'm guessing I hit "Preview" instead of "Submit" so it didn't post. Anyway, with regard to seeding the RNG through SetSeed, see my last post. I agree with you. I think that if you have a SetSeed function, all it should do is seed the RNG, not read the file. Regarding your point 3 above, I think you should generate the random numbers as you read in the file rather than read the whole file, count how many random numbers you need, generate them all, then stick them into the array, if that's what you were planning to do. It's easier to generate them as you read in the file. You don't need to keep track that way, plus you can display them as you go. My prior post's example assumes that you are generating the random numbers as you read in the file. Good luck!

Ok, I've been having a go at it for a couple of days now, and have managed to get the 1's and 0's into the array (thanks for all your help). the problem is, I want to place the numbers next to the individuals that end 0 0. However, despite me stating this in the code, it seems to place them a row down than they actually are.For example, it should look like this:

1 0 0 1 0
2 0 0 0 1
3 0 0 1 1
4 1 2
5 1 2
6 0 0 1 0
7 3 4
8 5 6
9 7 8
10 7 8

BUT, it actually looks like this:

1 0 0
2 0 0 1 0
3 0 0 0 1
4 1 2 1 1
5 1 2
6 0 0
7 3 4 1 0
8 5 6
9 7 8
10 7 8

Havn't got the faintest idea why it's doing this. Also, the numbers are ALWAYS the same for each individual. They are different on each run of the program, but they are always the same for each row, for example:

1 0 0 0 1
2 0 0 0 1
3 0 0 0 1
4 1 2
5 1 2
6 0 0 0 1
7 3 4
8 5 6
9 7 8
10 7 8

(except it is actually a row down than stated above).

I think they are always the same because the random number generator only creates a 0 or a 1 for the whole columns. I actually want it to create a random number for each cell in the array. This would give me different numbers for each cell.

Do you have any ideas? The below code has been with your help so thanks a lot. It's took a lot of playing around with though.

I'm getting closer :)

My code...

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int j;
int matrix[100][5] = {0};
int numCols = 3;
int numRows = 0;
float value1, value2;
float p,q;

void SetSeed();

int main () {

int counter;
const int RANGE = 1.0;

SetSeed();

for (counter = 1;counter <= 1;counter++){

value1 = (float) rand() * RANGE / RAND_MAX;
value2 = (float) rand() * RANGE / RAND_MAX;

}

ifstream inFile;
inFile.open("input.txt");

if (!inFile)
{
cout << "Unable to open input file";
exit(1); // terminate with error
}

while (inFile >> matrix[numRows][0])
{
printf ("%d\t", matrix[numRows][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}
p=0.4;
q=0.6;


   if (matrix[numRows-1][1] == 0 && matrix[numRows-1][2] == 0){
       if (value1 < 0.4){
      matrix[numRows][3] = 0;
     printf("\t%d", matrix[numRows][3]);
    }
    if (value1 > 0.4){
      matrix[numRows][3] = 1;
      printf("\t%d", matrix[numRows][3]);
      }
    if (value2 < 0.4){
      matrix[numRows][4] = 0;
      printf("\t%d", matrix[numRows][4]);
      }
      if (value2 > 0.4){
        matrix[numRows][4] = 1;
        printf("\t%d", matrix[numRows][4]);
        }
        printf("\n");
   }
    
  
  

numRows++;
printf ("\n");
}

inFile.close ();
}

void SetSeed() {

FILE *seed_file;
long int seed_value;

seed_file = fopen("seed.txt","r");
fscanf(seed_file,"%d",&seed_value);
fclose(seed_file);
srand(seed_value);
seed_file = fopen("seed.txt","w");
fprintf(seed_file,"%d",rand());
fclose(seed_file);
}

A few things. One, I notice that in some places you have "numRows -1" as an index and in others you have "numRows". That may be why the lines are off by one line some times. This is just one snippet. There are other instances too. It looked like you could have a -1 index in there at some point too since you may be subtracting 1 when numRows equals 0.

if (matrix[numRows-1][1] == 0 && matrix[numRows-1][2] == 0){
       if (value1 < 0.4){
      matrix[numRows][3] = 0;
     printf("\t%d", matrix[numRows][3]);
    }

The code below is before you read data in and nowhere else so the random numers will always be the same. You want to put it in the code block where you are implementing what occurs when the last two columns are both 0.

for (counter = 1;counter <= 1;counter++){

value1 = (float) rand() * RANGE / RAND_MAX;
value2 = (float) rand() * RANGE / RAND_MAX;

}

That's brilliant, it works perfectly! An example of the output so far:

1 0 0 0 0
2 0 0 1 1
3 0 0 0 1
4 1 2
5 1 2
6 0 0 1 0
7 3 4
8 5 6
9 7 8
10 7 8

I've ran the program numerous times and the exact proportion of 1's and 0's are appearing. Just as I want! I do need a bit of help for the next bit of the program though...

I will demonstrate the next part by using individuals 1 - 5 for simplicity:

1 0 0 0 0
2 0 0 1 1
3 0 0 0 1
4 1 2
5 1 2

The column on the left demonstrates individual number in a family tree (i.e. there are 5 people here). The second number represents the father number, and the third number represents mother number. So, individuals 4 and 5 are siblings (brothers and sisters or whatever). Their father is individual 1, and their mother is individual 2. Individual 3 is needed for the rest of the tree but is insignificant in this example.

I want one of the random numbers from their dad to be taken and placed in the 4th column. I then want one of the random numbers from their mum to be taken, and placed in column 5. In this example, the dad is 1 1, so a 1 must be taken, there is no other option. Also, their mum is 0 0 so a 0 must be taken. For this example, the output would be as follows:

1 0 0 0 0
2 0 0 1 1
3 0 0 0 1
4 1 2 0 1 (0 from dad, 1 from mum)
5 1 2 0 1 (0 from dad, 1 from mum)

My code so far is shown below. I am very confused on how to structure the code so that it picks numbers at random from their parents. (50:50 chance of each number, not like before where it was 40:60).

Do you have any ideas?

Thanks!

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int j;
int matrix[100][5] = {0};
int numCols = 3;
int numRows = 0;
float value1, value2;
float p,q;
void SetSeed();

int main () {
  
//Feed in the input file//
ifstream inFile;
inFile.open("input.txt");

if (!inFile)
{
cout << "Unable to open input file";
exit(1); // terminate with error
}

while (inFile >> matrix[numRows][0])
{
printf ("%d\t", matrix[numRows][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[numRows][j];
printf ("%d\t", matrix[numRows][j]);
}


int counter;
const int RANGE = 1.0;

SetSeed();

   if (matrix[numRows][1] == 0 && matrix[numRows][2] == 0){
   for (counter = 1;counter <= 1;counter++){
   value1 = (float) rand() * RANGE / RAND_MAX;
   value2 = (float) rand() * RANGE / RAND_MAX;
   }
   if (value1 <= 0.4){
   matrix[numRows][3] = 0;
   printf("\t%d", matrix[numRows][3]);
   }   
   if (value1 > 0.4){
   matrix[numRows][3] = 1;
   printf("\t%d", matrix[numRows][3]);
   }      
   if (value2 <= 0.4){
   matrix[numRows][4] = 0;
   printf("\t%d", matrix[numRows][4]);
   }      
   if (value2 > 0.4){
   matrix[numRows][4] = 1;
   printf("\t%d", matrix[numRows][4]);
   }
   printf("\n");
   }
    
numRows++;
printf ("\n");
}

inFile.close ();
}

void SetSeed() {

FILE *seed_file;
long int seed_value;

seed_file = fopen("seed.txt","r");
fscanf(seed_file,"%d",&seed_value);
fclose(seed_file);
srand(seed_value);
seed_file = fopen("seed.txt","w");
fprintf(seed_file,"%d",rand());
fclose(seed_file);
}

Well, in order to capture mom's and dad's random numbers, you need to know what row of the matrix mom and dad are in. This may be easy if the data in the first column is always in the nice clean order that it is in your file:

1
2
3
4
5


Thus person 1 is in the first row, person 2 in the second, person 3 in the third, etc. If this can be assumed, then finding the row that person 6 is in is simple. Person 6 is in row 6. Keeping in mind that your data has people starting with person 1 and C++ indexes start at 0, to find the matrix row index you would subtract 1 from 6 to get 5 as the matrix row index. So in your example, dad is person 1, mom is person 2. So if you create two integers called, say dadRowIndex and momRowIndex, dadRowIndex would be 0 and momRowIndex would be 1. To get mom and dad's random numbers, you'd have something like this:

int dadRandom1 = matrix[dadRowIndex][3];
int dadRandom2 = matrix[dadRowIndex][4];
int momRandom1 = matrix[momRowIndex][3];
int momRandom2 = matrix[momRowIndex][4];

You would use similar code as before to pick between dadRandom1 and dadRandom2, and to pick between momRandom1 and momRandom2. Obviously change the conditions from 0.4 to 0.5 since you want equal probability here. Once you pick one random number from mom and one from dad, in the child's row, put the one you picked from dad in the dad column (4th column, column index 3) and the one you picked from mom in the mom column (5th column, column index 4).

Obviously this assumes that both parents come before the kids in the data.

Ok, so I've put the dadRowIndex and momRowIndex as global integers at the top of the syntax like this:

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int j;
int matrix[100][4] = {0};
int numCols = 3;
int numRows = 0;
int dadRowIndex, momRowIndex;
float value1, value2, value3, value4;
void SetSeed();

int main () {

And I've put the dad/momRandom1/2 integers above the bit I worked out for the 0 0 people as follows:

int counter;
const int RANGE = 1.0;
int dadRandom1 = matrix[dadRowIndex][3];
int dadRandom2 = matrix[dadRowIndex][4];
int momRandom1 = matrix[momRowIndex][3];
int momRandom2 = matrix[momRowIndex][4];

SetSeed();

 if (matrix[numRows][1] == 0 && matrix[numRows][2] == 0){
   for (counter = 1;counter <= 1;counter++){
   value1 = (float) rand() * RANGE / RAND_MAX;
   value2 = (float) rand() * RANGE / RAND_MAX;
   }
   if (value1 < 0.4){
   matrix[numRows][3] = 0;
   printf("\t%d", matrix[numRows][3]);
   }   
   if (value1 >= 0.4){
   matrix[numRows][3] = 1;
   printf("\t%d", matrix[numRows][3]);
   }      
   if (value2 < 0.4){
   matrix[numRows][4] = 0;
   printf("\t%d", matrix[numRows][4]);
   }      
   if (value2 >= 0.4){
   matrix[numRows][4] = 1;
   printf("\t%d", matrix[numRows][4]);
   }
 }

Is this positioning correct?

After the above syntax, I'm presuming I have to state something like... when column 2 and 3 both do not equal 0, use the random number generator again to get value3 and value4. Something like:

if (matrix[numRows][1] != 0 && matrix[numRows][2] != 0){ 
for (counter = 1;counter <= 1;counter++){
   value3 = (float) rand() * RANGE / RAND_MAX;
   value4 = (float) rand() * RANGE / RAND_MAX;
}
if (value2 <= 0.5){
   matrix[numRows][3] = intDadRandom1;
   printf("\t%d", matrix[numRows][3]);
}
if (value2 > 0.5){
   matrix[numRows][3] = intDadRandom2;
   printf("\t%d", matrix[numRows][3]);
}
if (value3 <= 0.5){
   matrix[numRows][4] = intMomRandom1;
   printf("\t%d", matrix[numRows][4]);
}
if (value1 > 0.5){
   matrix[numRows][4] = intMomRandom2;
   printf("\t%d", matrix[numRows][4]);
}
}

However, when I use the above it does not compile and says:

'intDadRandom1' has not been declared
'intDadRandom2' has not been declared
'intMomRandom1' has not been declared
'intMomRandom2' has not been declared

Am I on the right tracks or am doing it completely wrong? :s

Thanks again for your continuing help...

You mentioned that you had a very old compiler, so possibly that will affect things as far as seeding random numbers, but I don't think so. You have your call to SetSeed inside the while loop which reads your data. That means you are calling SetSeed more than once. Normally you only use the "srand" command once. I think you are using it many times. SetSeed should be called only once, and before you start reading in data from the data file. I imagine that even a very old compiler would not need it to be called numerous times. It's not an error to seed it numerous times, but is unnecessary. So try putting SetSeed earlier in the program, before that big while loop. Same goes for this line:

const int RANGE = 1.0;

I don't see any need to declare it more than once, particularly since you are declaring it a constant. It's not an error to declare it inside the loop, but it's considered bad form. You do need to pick RANGE to be either an integer or a float and give it an appropriate value. Right now you are declaring RANGE to be an integer and assigning it a float value.

This code below is a typo?

matrix[numRows][3] = intDadRandom1;

You have the variable named "intDadRandom1". Is that what you want? There's no space between "int" and the rest of it, so if you were trying to call it "DadRandom1" and the "int" was to declare it as an integer, you need a space between "int" and "DadRandom1". It also wouldn't work, at least the way you have it in this line. I was thinking on the lines of this:

if (matrix[numRows][1] == 0 && matrix[numRows][2] == 0)
{
     // code to generate and assign random numbers to the row.
}
else
{
    // declare mom and dad variables here.
    // code to find mom and dad rows, extract the random
    // numbers from those rows, and place them in 4th and
    // 5th columns of the current/child row of matrix.
}

Ok, well I'll try to sort the set seed line out. And I did put a space between the int and the variable name, it was the error message that didnt. As for the below:

else
{
// declare mom and dad variables here.
// code to find mom and dad rows, extract the random
// numbers from those rows, and place them in 4th and
// 5th columns of the current/child row of matrix.
}

1. Declare mom and dad variables:

int dadRandom1 = matrix[dadRowIndex][3];
int dadRandom2 = matrix[dadRowIndex][4];
int momRandom1 = matrix[momRowIndex][3];
int momRandom2 = matrix[momRowIndex][4];

We're talking about these right?? Or is it these...

int dadRowIndex, momRowIndex;

2. code to find mom and dad rows, extract the random numbers from those rows, and place them in 4th and 5th columns of the current/child row of matrix.

I'm really struggling with this code. Sorry. Do you have any suggestions on how to set it out? I'm a little confused.

Thanks.

Ok, well I'll try to sort the set seed line out. And I did put a space between the int and the variable name, it was the error message that didnt. As for the below:

Re-read the code you posted earlier. The line I quoted is verbatim from that. You had two separate sections. In one section you had the spaces in there:

int dadRandom1 = matrix[dadRowIndex][3];
int dadRandom2 = matrix[dadRowIndex][4];
int momRandom1 = matrix[momRowIndex][3];
int momRandom2 = matrix[momRowIndex][4];

But in the other, you didn't:

if (value2 > 0.5){
   matrix[numRows][3] = intDadRandom2;
   printf("\t%d", matrix[numRows][3]);

1. Declare mom and dad variables:

int dadRandom1 = matrix[dadRowIndex][3];
int dadRandom2 = matrix[dadRowIndex][4];
int momRandom1 = matrix[momRowIndex][3];
int momRandom2 = matrix[momRowIndex][4];

We're talking about these right?? Or is it these...

int dadRowIndex, momRowIndex;

We're talking about all of them. You don't need them or use them anywhere else so you might as well declare them in the "else" loop.

Before you use these lines:

int dadRandom1 = matrix[dadRowIndex][3];
int dadRandom2 = matrix[dadRowIndex][4];
int momRandom1 = matrix[momRowIndex][3];
int momRandom2 = matrix[momRowIndex][4];

you need to specify values for dadRowIndex, etc. See the earlier post for a way to do that.

"Keeping in mind that your data has people starting with person 1 and C++ indexes start at 0, to find the matrix row index you would subtract 1 from 6 to get 5 as the matrix row index. So in your example, dad is person 1, mom is person 2. So if you create two integers called, say dadRowIndex and momRowIndex, dadRowIndex would be 0 and momRowIndex would be 1."

I do understand this, however I am finding putting this into actual code very difficult. At the moment, my else statement looks like this:

else{

int dadRowIndex; 
int momRowIndex;
int dadRandom1 = matrix[dadRowIndex][3];
int dadRandom2 = matrix[dadRowIndex][4];
int momRandom1 = matrix[momRowIndex][3];
int momRandom2 = matrix[momRowIndex][4];

for (counter = 1;counter <= 1;counter++){
rand3 = (float) rand() * RANGE / RAND_MAX;
rand4 = (float) rand() * RANGE / RAND_MAX;
}

}

I've added rand3 and 4 to this else statement as I thought extra random numbers wouldn't hurt. Is this ok?

Ok, so somehow my next step needs to be laying down the code to find mom and dads row numbers. So maybe start by saying, for dad:

if (matrix[numRows][1] == matrix[numRows][0]){

Meaning that if the number in column 2 matches the number in column 1, then bingo! That's your dads row number! And then similarly for mom:

if (matrix[numRows][2] == matrix[numRows][0]){

Meaning that if the number in column 3 matches the number in column 1, then that's your mums rown number!!

Am I on the right tracks?

Oh, and bear in mind that this family tree can be replaced by another one of a very different structure (however, they are always numbered from 1 -> onwards). So I can't JUST use this one and instantly say, well row 1 is definitely individuals 4 and 5's dads row number. Because it could be completely different for another family tree.

OK, so the first column is ALWAYS like this?

1
2
3
4
5
6
7

You'll never run into something like this for the first column?

6
3
5
9
1
2

If it is the former, finding the dad's and mom's row is easy. If the latter is possible, it's a little more involved. I'm getting that it IS the former. So let's take a row like this:

9 5 7

I'm interpreting that this means "Person 9 has a mom and a dad. Person 9's dad is person 5 and Person 9's mom is person 7". Is this the interpretation?

Assuming so, something like this:

int dad = matrix[numRows][1];    // 1 is "dad" column
int mom = matrix[numRows][2];   // 2 is "mom" column

int dadRowIndex = dad - 1;
int momRowIndex = mom -1;

No "if" statements are necessary. You can obviously subtract the 1 from the beginning and not even have mom and dad variables. I added them to try to expand on the thought process.

Ok, I see that now! And yes, it will always be going from 1 onwards. Which is great news as I'm guessing it's a simpler appraoch?

This is my else statement from what I can gather from your posts (I really am trying, honest!)

else{
int dad = matrix[numRows][1];    // 1 is "dad" column
int mom = matrix[numRows][2];   // 2 is "mom" column

int dadRowIndex = dad - 1;
int momRowIndex = mom -1;

for (counter = 1;counter <= 1;counter++){
rand3 = (float) rand() * RANGE / RAND_MAX;
rand4 = (float) rand() * RANGE / RAND_MAX;
}
if (rand3 < 0.5){
matrix[numRows][3] = [dadRowIndex][3];
printf("\t%d", matrix[numRows][3]);
}   
if (rand3 >= 0.5){
matrix[numRows][4] = [dadRowIndex][4];
printf("\t%d", matrix[numRows][4]);
}      
if (rand4 < 0.5){
matrix[numRows][3] = [momRowIndex][3];
printf("\t%d", matrix[numRows][3]);
}      
if (rand4 >= 0.5){
matrix[numRows][4] = momRowIndex][4];
printf("\t%d", matrix[numRows][4]);
}
}

However, I must be doing something wrong, as it keeps saying an error message of:

'[ found where not expected'

Am I on the right tracks? I hope so, but I must be doing something obvious wrong.

Yep, you're on the right track. Your errors are simple:

matrix[numRows][3] = [momRowIndex][3];

You forgot the word "matrix". Yeah, the fact that column 1 counts from 1 onward makes it easier than if it wasn't. Ancient Dragon commented on your other thread and made some good points, particularly about using "else" rather than another "if" statement. One problem is here:

if (rand3 < 0.5){
matrix[numRows][3] = [dadRowIndex][3];
printf("\t%d", matrix[numRows][3]);
}   
if (rand3 >= 0.5){
[B]matrix[numRows][4][/B] = [dadRowIndex][4];
printf("\t%d", matrix[numRows][4]);
}

See the bold above, the left side of the equals sign. Dad's column is column index 3. Mom's column is column index 4. Do not assign a random number from dad's row into mom's column. The problem is on the left side of the equals sign. The right side is fine. Both indexes on the left side of the equals sign need to be 3. Ditto for the printf statement. You are assigning one of two values from dad to dad's column of the child's row. If it helps, go back to the old code of "dadRandom1" and "dadRandom2", etc. Up to you. But the left side of the equals sign needs to be the same regardless of which of dad's numbers is picked. Ditto for the mom code.

Absolutely fantastic... IT WORKS!!

Thank you so much for your help, that step was a big hurdle.

Now I have a few queries which sound very complex to solve (well, to me anyway). Maybe you can help me with them?

1. To test the program when using individuals with 0 0, I ran the program 38 times. Each time I ran it I counted up how many 0's and 1's were produced. I ended up with 186 1's and 118 0's. Putting this into percentages, we get 61.18% 1's and 38.82% 0's.

When I saw these figures I was very happy with them (remember a 1 is supposed to occur with a frequency of 0.6, and 0.4 for a 0). This is proof that on a large scale of trials, the frequencies held. FEWF!

But then, a slight disaster...
I noticed a PATTERN in the 0's and 1's it was giving out. Yes, random for the first 20 trials. But the same numbers were given to me the next 20 trials, and then the next 20. Therefore, we must conclude that this isn't truly random?

It is giving them out at the right frequency randomly, but only for 20 runs of the program. For some reason it just gives the same results again for the next block of 20 and on and on... Do you have any ideas what's going on here? Maybe this is a problem I cannot overcome. It would be a shame if this is true as the key to this program is RANDOMNESS.

2. In the final program, I need to count how many 1's and 0's are being produced (discussed in the other thread), but I want to run the program 1000 times. This may be a bit pointless if it keeps giving me the same 20 results, but it is something I have to do...

Of course if I am running it 1000 times, I do not want the results to be printed to screen as in the one run I have constructed. The output would be MASSIVE! Therefore, I want the 1000 arrays produced to be invisible in the output, but I want the counter to count every 1 and 0 and total it for every run.

Is this possible?

Thanks again.

3.

I have been told that my input file actually looks like this:

10
1 0 0
2 0 0
3 0 0
4 1 2
5 1 2
6 0 0
7 3 4
8 5 6
9 7 8
10 7 8

With the number at the top of the list defining the number of people in the list. Obviously, my program will not work if it is in this format. Do you have any ideas of how I can change it so it will?

3.

I have been told that my input file actually looks like this:

10
1 0 0
2 0 0
3 0 0
4 1 2
5 1 2
6 0 0
7 3 4
8 5 6
9 7 8
10 7 8

With the number at the top of the list defining the number of people in the list. Obviously, my program will not work if it is in this format. Do you have any ideas of how I can change it so it will?

Not difficult: Right now your loop control is this:

while (inFile >> matrix[numRows][0])

Change this from a while loop to a for loop. Put the command:

inFile >> matrix[numRows][0];

as the first line in that for loop. If you use numRows as your loop counter, you won't have to change anything, but you should probably change that name to something more descriptive. You may have to take out any lines of code that previously incremented numRows, as it will be incremented inside the for loop. Look for any other lines using numRows. Most will be able to be kept as is, but when debugging, keep that in mind. Read in the first line of the input file into some variable. That'll be the number of times you go through the for loop. You may decide to call THAT variable numRows. Just be careful to change the existing numRows variable to something like i first.

As for the random numbers, you've had a few threads going and I commented on one of them on a way to seed the random number generator with time instead of your file. That's the way it is normally done. I'd check out that random seed number file you've been using and see if it changes every time. If there's a pattern in that file, that could be a problem. But I'd actually toss the whole reading of a file to seed the RNG out. Just seed it with the time. I normally call the rand () function a few times after that before actually generating numbers I need to make sure I got randomness. Probably just paranoid. There's a wealth of information out there on RNGs. I'd do a simple test program that generates a bunch of random integers and displays them. Look for a pattern. If there is one, something's wrong with your RNG.

Ok, well firstly I changed all the numRows to i's.

Then I changed:

while (inFile >> matrix[i][0])
{
printf ("%d\t", matrix[i][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[i][j];
printf ("%d\t", matrix[i][j]);
}

to...

for inFile >> matrix[i][0];
{
printf ("%d\t", matrix[i][0]);
for (j = 1; j < numCols; j++)
{
inFile >> matrix[i][j];
printf ("%d\t", matrix[i][j]);
}

However, it comes up with a warning sign saying a for loop should be followed by a bracket. I'm not sure what to make of this?

I think simple steps, firstly I want to make sure that damn file gets read in, otherwise my program won't be going anywhere. You say to read the first line of the input file into some variable. How exactly is this done? And to do this, do I have to change any of my current code?

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.