I have an array that has read from file as follows:
1 0 0
2 0 0
3 1 2
4 1 2
5 3 0
For the first two lines ( the ones that end 0 0) I want to enter two additional sets of data, so an additional two columns are required.
This new data comes from random numbers I have created (between zero and 1). I'm making it truly random by using a seed file. If the random number is 0.4 or less, I want a zero to be placed in the array. If it is 0.5 or above, I want a 1 to be placed in.
So, if the two random numbers for the first line are 0.1 and 0.5, I want a '0' and a '1' to be placed into the array, like this:
1 0 0 0 1
My code so far is as follows, and up to now I have managed to create a 100x3 array which fills itself with an input file (i have used 100 as this file is interchangeable with larger ones). Also, I have managed to create two random numbers between zero and 1.
I just need to know how to link it all together!!
Thanks guys!!
#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 ();
}