Array trouble

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Array trouble

 
0
  #1
Jan 28th, 2008
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 ();
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Array trouble

 
0
  #2
Jan 28th, 2008
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
Last edited by twomers; Jan 28th, 2008 at 6:17 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: Array trouble

 
0
  #3
Jan 28th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Array trouble

 
0
  #4
Jan 28th, 2008
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.
  1. int main( void ) {
  2. int int_array[2][3] = {0};
  3. int_array[0][0] = 12;
  4. int_array[1][1] = 12;
  5.  
  6. int y, x;
  7.  
  8. for ( y=0; y<2; y++ ) {
  9. for ( x=0; x<3; x++ ) {
  10. printf( "[%d,%d] - %d\t", x, y, int_array[y][x] );
  11. int_array[x][y] = x*y;
  12. }
  13. printf("\n");
  14. }
  15.  
  16. for ( y=0; y<2; y++ ) {
  17. for ( x=0; x<3; x++ ) {
  18. printf( "[%d,%d] - %d\t", x, y, int_array[y][x] );
  19. }
  20. printf("\n");
  21. }
  22.  
  23. }
You assign and get values by accessing like so. That answer your question?
Last edited by twomers; Jan 28th, 2008 at 6:43 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: Array trouble

 
0
  #5
Jan 28th, 2008
I think I'm getting there...

I inputted that code in as follows:
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>
  4.  
  5. int j;
  6. int matrix[101][3] = {0};
  7. int numCols = 3;
  8. int numRows = 0;
  9. void SetSeed();
  10.  
  11. int main () {
  12.  
  13. float value;
  14. int counter;
  15. const int RANGE = 1.0;
  16.  
  17. SetSeed();
  18.  
  19. for (counter = 1;counter <= 2;counter++){
  20.  
  21. value = (float) rand() * RANGE / RAND_MAX;
  22.  
  23. if (value<0.31){
  24. printf("\n0\n");
  25. }
  26.  
  27. else
  28. if(value>0.31){
  29. printf("1\n");
  30. }
  31. }
  32.  
  33. }
  34.  
  35. void SetSeed() {
  36.  
  37. FILE *seed_file;
  38. long int seed_value;
  39.  
  40. seed_file = fopen("seed.txt","r");
  41. fscanf(seed_file,"%d",&seed_value);
  42. fclose(seed_file);
  43. srand(seed_value);
  44. seed_file = fopen("seed.txt","w");
  45. fprintf(seed_file,"%d",rand());
  46. fclose(seed_file);
  47.  
  48. ifstream inFile;
  49. inFile.open("input.txt");
  50.  
  51. if (!inFile)
  52. {
  53. cout << "Unable to open input file";
  54. exit(1); // terminate with error
  55. }
  56.  
  57. while (inFile >> matrix[numRows][0])
  58. {
  59. printf ("%d\t", matrix[numRows][0]);
  60. for (j = 1; j < numCols; j++)
  61. {
  62. inFile >> matrix[numRows][j];
  63. printf ("%d\t", matrix[numRows][j]);
  64. }
  65. numRows++;
  66. printf ("\n");
  67. }
  68.  
  69. int int_array[2][3];
  70. int_array[0][0] = 12; // 0, 0th element
  71. int_array[2][1] = 12; // third col (0, 1, 2), second row (0, 1)
  72. int y, x;
  73. for (y=0; y<2; y++) {
  74. for ( x=0; x<3; x++ ) {
  75. printf( "%d ", int_array[y][x] );
  76. }
  77. printf("\n");
  78. }
  79.  
  80. inFile.close ();
  81. }


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.
Last edited by WaltP; Jan 29th, 2008 at 3:57 am. Reason: C'mon, man. Use CODE tags. They were described in the rules you read when you signed up, AND the background of the input box you keep typing in.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Array trouble

 
0
  #6
Jan 28th, 2008
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 = 0;counter <= 2;counter++){
    value = rand()%1;
    printf( "%d", value );
  }
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: Array trouble

 
0
  #7
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,832
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Array trouble

 
0
  #8
Jan 28th, 2008
Just commented on the other thread. Might answer your question from over here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,832
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Array trouble

 
1
  #9
Jan 28th, 2008
I think you want:

  1. rand () % 2

not
  1. rand () % 1
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC