•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 428,629 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,951 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2016 | Replies: 32
![]() |
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Rep Power: 1
Solved Threads: 0
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!!
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!!
•
•
Join Date: Jan 2008
Posts: 1,780
Reputation:
Rep Power: 8
Solved Threads: 219
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:
You'll have a one dimensional array of type rowData. Something like:
since you have a maximum of 100 rows of data.
struct rowData
{
int data[3];
double randomNum[2];
};You'll have a one dimensional array of type rowData. Something like:
rowData rows[100];
•
•
Join Date: Jan 2008
Posts: 1,780
Reputation:
Rep Power: 8
Solved Threads: 219
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?
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?
Last edited by VernonDozier : Jan 28th, 2008 at 10:27 am.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Rep Power: 1
Solved Threads: 0
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!!
#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!!
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 1,780
Reputation:
Rep Power: 8
Solved Threads: 219
I assume by this:
you are referring to this line of code that I suggested earlier?
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:
You should end up with this:
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:
Looks like they're commenting on how to do random integers in the other thread, so I won't do it here.
•
•
•
•
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.
Last edited by VernonDozier : Jan 28th, 2008 at 6:21 pm. Reason: typo
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Rep Power: 1
Solved Threads: 0
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:
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:
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.
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.
Last edited by WaltP : Jan 29th, 2008 at 3:03 am. Reason: Learn to use CODE tags properly. And the PREVIEW button!
•
•
Join Date: Jan 2008
Posts: 1,780
Reputation:
Rep Power: 8
Solved Threads: 219
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:
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:
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:
The 4 needs to be changed to 5.
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 ();
}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.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C++ Forum
- Previous Thread: How do I call this function?
- Next Thread: how to terminate a thread in c++



Linear Mode