| | |
Array trouble
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
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 ();
}
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
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.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
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 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. You assign and get values by accessing like so. That answer your question?
Anyway.
There aren't many things about arrays, fortunately.
cpp Syntax (Toggle Plain Text)
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"); } }
Last edited by twomers; Jan 28th, 2008 at 6:43 pm.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
I think I'm getting there...
I inputted that code in as follows:
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.
I inputted that code in as follows:
cpp Syntax (Toggle Plain Text)
#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.
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.
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:
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 );
}•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,832
Reputation:
Solved Threads: 501
I think you want:
not
C++ Syntax (Toggle Plain Text)
rand () % 2
not
C++ Syntax (Toggle Plain Text)
rand () % 1
![]() |
Similar Threads
- Find void array index from pointer (C)
- returning three dimensional array (C)
- Reading in certain columns from CSV files into array C++ (C++)
- single-dimensional array trouble (C++)
- error 88:'(' expected when trying to display an array any help (Pascal and Delphi)
- Array without twice the same number? (C)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- Having trouble with links on PHP site! Any help would be great! (PHP)
Other Threads in the C++ Forum
- Previous Thread: decrypt/encrypt program
- Next Thread: c++ homework seg faults
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline graph homeworkhelp homeworkhelper iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






