Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Numbers are random generated from user inputted range.
- // Results are both written to file and shown in console.
- // Result file can be changed @ line 17
- // By changing "xx.txt"
- #include <cstdlib>
- #include <iostream>
- #include <iomanip> // setprecision library
- #include <stdio.h> // printf library
- #include <string>
- #include <cmath> // mathematical commands // pow() sqrt() abs() ..
- #include <limits> // cin.ignore(numeric_limits<streamsize>::max(),'\n');
- #include <fstream> // File input and output.
- #include <ctime> // Time for random generator
- using namespace std;
- const char rezF[] = "3U.txt";
- // used to find number of digits in a number (1000 = 4) to be used in setw()
- template <class T>
- int numDigits(T number)
- {
- int digits = 0;
- if (number < 0) digits = 1; // remove this line if '-' counts as a digit
- while (number) {
- number /= 10;
- digits++;
- }
- return digits;
- }
- int main(int argc, char *argv[])
- {
- // Range: [x;y]
- // a = (x + rand() % (y - x +1)) // Random numbers in range;
- // Variables
- int a;
- int x, y;
- int temp;
- int inputedRange;
- ofstream rf(rezF);
- cout << "Range in which random numbers will be generated? [x;y]" << endl;
- cout << "From: ";cin >> x;
- cout << "To: ";cin >> y;
- cout << "" << endl;
- rf << "User selected range" << endl;
- rf << "From: " << x << endl;
- rf << "To: " << y << endl << endl;
- inputedRange = numDigits(y); // setting setw() range
- srand(time(NULL)); // Initializing random number @ time
- for (int i = 0; i < 10; i++) // Rows
- {
- for (int j = 0; j < 10; j++)// Lines
- {
- a = (x + rand() % (y - x + 1));
- cout << setw(inputedRange) << a << " ";
- rf << setw(inputedRange) << a << " ";
- }
- cout << "" << endl;
- rf << "" << endl;
- }
- cout << "\nThe numbers have been put in a file.";
- // Ending
- cout << " " << endl;
- //system ("PAUSE");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement