Hi.I am learning C++.
I need to build code that does the following.
1) Generates 3 random numbers and passes them to next function
2) Next function uses these three different random numbers (lets say 66762734234234234, 9234234234444444444, 3545345555555) to pass them to x, y and z, which are then added. The numbers are large, so bigint is used.
3) The result is appended to text file.The file should look like this:
934855435345345435
4353453454354354555
345345435435436543534534
etc
So, the code should not prompt user for anything, just write lets say a million of added x,y and z to a text file.
The code below does what described above, except I dont know how to pass the random numbers to x, y and z. They are just printed on screen without being used by the following function. They are not connected with the addition function. Its just some snippet I found on the net.
Question.
How do I alter the code so random numbers (different for every increment and pass) are passed to the x, y and z.
Thanks.
V.
Start of code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include "BigIntegerLibrary.hh"
BigInteger _pow(BigInteger &base, const BigUnsigned power, BigInteger *res)
{
*res = 1;
for (BigUnsigned i(0); i < power; i ++)
{
*res *= base;
}
return *res;
}
using namespace std;
int main()
{
{
/* Random numbers generator, not plugged in yet */
srand((unsigned)time(0));
int random_integer;
int lowest=1000000, highest=100000000;
int range=(highest-lowest)+1;
for(int index=0; index<20; index++){
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << random_integer << endl;
}
/* End of random numbers generator */
std::string s;
BigInteger x, y, z;
using std::cout;
using std::cin;
using namespace std;
ofstream myfile;
/* Start of large number addition function, random numbers from above are
supposed to be passed to x, y and z instead of prompting users to enter them.
The random numbers should be different for each x, y and z on every line output below */
cout << "Please enter x: ";
cin >> s;
x = stringToBigInteger(s);
cout << "Please enter y: ";
cin >> s;
y = stringToBigInteger(s);
cout << "Please enter z: ";
cin >> s;
z = stringToBigInteger(s);
BigInteger res;
std::cout << "Adding numvers: \n";
std::cout << (x + y + z) ;
std::cout << std::endl;
/* End of large number addition function */
/* Start of file writing function */
myfile.open ("vdb_similar.txt", ios::app );
/* Separate entries in datafiles in new lines */
myfile << "\n";
/* Write run length on new line */
myfile << (x + y + z) ;
myfile.close();
std::cout << "Done writing, closed datafile\n";
system("pause");
return 0;
}
}
|