hi there,
i have a homework to do, write c++ code for union find..
in order to do that i have to read data from a text file that is generating 600000+ of random integers...
here is my file:
here is the thing i need to save the content of the text file in an array so i can implement the union find function..
the instructor said the first integer in the text file generated is the size of the array we should use. now here is my code:
the instructor said the first integer in the text file generated is the size of the array we should use
He did not tell you that whole file should be read.
If you still insist on reading whole file, you can:
1) Overallocate your array to some very large number (uneffective, dangerous)
2) Use automatically groing container as std::vector (most effective)
3) Write code to grow array when needed asyou input data (complex, nedd to be careful)
4) Do two passes on a file: first to count numbers, second to read them.
no im 100% positive that i should read all the file.. now i went on over the code that is generating the random numbers and it turned out that the first integer is the maximum numbers and the rest is being written in form of x,y for ex:
#include <ctime>
#include <iostream>
#include <fstream>
usingnamespace std;
typedefunsigned __int32 uint32_t;
/***** SET VALUE IN seed variable BELOW ***/
uint32_t seed=7;
/*************************/
uint32_t my_rand(){
int mod=0xffffffff;
int a=1103515245;
int c=12345;
seed=(seed*a+c)%mod;
return seed;
}
int main()
{
ofstream os;
os.open("data-for-union-find.txt");
// max used in Spring 2014
int max=600000;
int num_edges=2*max+seed*max/20;
int x,y;
os<<max<<endl;
for(int i=1;i<=num_edges;i++){
x=my_rand()%max;
y=my_rand()%max;
os<<x<<" "<<y<<endl;
}
os.close();
}