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 ();
  }

Ew!
A few things.
* <iostream.h> and <fstream.h> are old and now in the form of <iostream> and <fstream>. You might want to using namespace std; if you're going to change them.
* main returns int, so int main().
* Why are you using C File IO when this is C++? Or rather selectively using it? Pick one method and stick with it, OK? There's no need to confuse yourself with more than that.
* matrix[101][3] has 101 (0 -> 100), columns and 4 rows (0 ->3).
* Use code tags.
* There are easier ways to generate random numbers. rand()%1, rand()&1 are just two[1]

Now. To your code...

[1] For fuller reasonings read about random numbers

Ok, well I do apologise, but I am only a beginner. Thanks for pointing those things out, but the c compiler I am using at university is ancient and I cannot use the new tags. Also, I must use random numbers from seed (sadly).

I have converted 2 random numbers into the 1's and 0's successfuly outside of the array, but do not know how to get them inside it? Could you help? The code I have used for this is:

if (value<0.31){
printf("0\n");
}

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

This gives me the right ratio of 0's and 1's perfect. I just need to know how to get them into the 4th and 5th columns in my array! :(

Oh and of course you're right, it would be [100] [4] if I wanted 5 columns. Silly me :)

I recommend you download a proper C compiler for yourself at home... there should be a list somewhere... http://www.cprogramming.com/compilers.html There's no point in stoning away when you should be in the slick 21st century.

Anyway.

There aren't many things about arrays, fortunately.

int main( void ) {
  int int_array[2][3] = {0};
  int_array[0][0] = 12;
  int_array[1][1] = 12;

  int y, x;

  for ( y=0; y<2; y++ ) {
    for ( x=0; x<3; x++ ) {
      printf( "[%d,%d] - %d\t", x, y, int_array[y][x] );
      int_array[x][y] = x*y;
    }
    printf("\n");
  }

  for ( y=0; y<2; y++ ) {
    for ( x=0; x<3; x++ ) {
      printf( "[%d,%d] - %d\t", x, y, int_array[y][x] );
    }
    printf("\n");
  }

}

You assign and get values by accessing like so. That answer your question?

I think I'm getting there...

I inputted that code in as follows:

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

int j;
int matrix[101][3] = {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("1\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]);
        }
      numRows++;
      printf ("\n");
    }

int int_array[2][3];
int_array[0][0] = 12; // 0, 0th element
int_array[2][1] = 12; // third col (0, 1, 2), second row (0, 1)
int y, x;
for (y=0; y<2; y++) {
for ( x=0; x<3; x++ ) {
printf( "%d ", int_array[y][x] );
}
printf("\n");
}

  inFile.close ();
  }

The code you gave me, I put at the bottom. Is this the right place?

This runs and places the infile into the array, however no random numbers are generated and the 1's and 0's are definitely not placed in it.

The error message says:

SetSeed(void)__C - in file array.c at line 71 [+03f5]
main - in file array.c at line 17 [+0023]

line 71 is:
int_array[2][1] = 12; // third col (0, 1, 2), second row (0, 1)

line 17 is:
SetSeed();


Sorry about this hassle, but I'm not too hot on this, sorry.

You use code tags: [code] PUT YOUR C CODE HERE [/code] for future reference.

The code I posted was meant as a learning exercise. It wasn't meant to do anything other than to show you how to access arrays.

For the random number:

for (counter = [B]0[/B];counter <= 2;counter++){
    value = rand()%1;
    printf( "%d", value );
  }

That last post just produced 000 every time i used the random number. I really need to stick with the seed file.

The output I have from my file at the moment 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

0

1

Where the last two numbers are randomly generated numbers converted to either a 0 or a 1 dependant on their size. How would I get it to read:

1 0 0 0 1
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

??

Actually... in this file there are 4 lines ending in 0 0 (1, 2, 3, and 6). So I need 8 randomly generated numbers here, so it would read something like this:


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

Could you please help me with the code I've got? I'm not too confident about altering it. Thanks for your help though.

Just commented on the other thread. Might answer your question from over here.

I think you want:

rand () % 2

not

rand () % 1
commented: Whoops. Good catch. +5
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.