Hi guys,
I have an array which reads in a file and displays it to the screen. The output is 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
I then used a random number generator to add a 1 or a 0 to a further 2 columns in the array. An example of an output is as follows:
1 0 0 1 0
2 0 0 0 1
3 0 0 1 1
4 1 2 1 0
5 1 2 0 1
6 0 0 1 1
7 3 4 1 1
8 5 6 1 1
9 7 8 1 0
10 7 8 0 1
I was wondering if anyone knew how to count the 1's and 0's I have in the 4th and 5th columns? For the above example, below the output of the array, I would like it to print the following to the screen:
Number of 0's present: 6
Number of 1's present: 14
I'm struggling with the code. Does anyone have any ideas? My current code is:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int j;
int matrix[100][5] = {0};
int numCols = 3;
int numRows = 0;
float rand1, rand2, rand3, rand4;
void SetSeed();
int main () {
const float RANGE = 1.0;
SetSeed();
ifstream inFile;
inFile.open("input.txt");
if (!inFile)
{
printf ("Unable to open input file");
exit(1);
}
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;
if (matrix[numRows][1] == 0 && matrix[numRows][2] == 0){
for (counter = 1;counter <= 1;counter++){
rand1 = (float) rand() * RANGE / RAND_MAX;
rand2 = (float) rand() * RANGE / RAND_MAX;
}
if (rand1 < 0.4){
matrix[numRows][3] = 0;
printf("\t%d", matrix[numRows][3]);
}
if (rand1 >= 0.4){
matrix[numRows][3] = 1;
printf("\t%d", matrix[numRows][3]);
}
if (rand2 < 0.4){
matrix[numRows][4] = 0;
printf("\t%d", matrix[numRows][4]);
}
if (rand2 >= 0.4){
matrix[numRows][4] = 1;
printf("\t%d", matrix[numRows][4]);
}
}
else{
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] = 0;
printf("\t%d", matrix[numRows][3]);
}
if (rand3 >= 0.5){
matrix[numRows][3] = 1;
printf("\t%d", matrix[numRows][3]);
}
if (rand4 < 0.5){
matrix[numRows][4] = 0;
printf("\t%d", matrix[numRows][4]);
}
if (rand4 >= 0.5){
matrix[numRows][4] = 1;
printf("\t%d", matrix[numRows][4]);
}
}
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);
}
Thanks for any help you can give me :)