Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- using std::cout;
- using std::cin;
- using std::endl;
- using std::string;
- using std::cerr;
- using std::rand;
- using std::ifstream;
- const int amountOfNumbers = 10;
- int ARRAY[amountOfNumbers] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- const string INT_REQ = "Main: Integer required!";
- const string ERROR_FILE_NOT_OPEN = "Main: Cannot open the file.";
- void printArray(int *arr, int n)
- {
- for (int elem = 0; elem < n; elem++)
- {
- cout << arr[elem] << ' ';
- }
- }
- int amountDividedWithoutRemainder(int *arr, int digit)
- {
- int count = 0;
- for (int elem : arr)
- {
- if (elem % digit == 0)
- {
- ++ count;
- }
- }
- return count;
- }
- int main()
- {
- try
- {
- cout << "Enter a digit to divide to: " << endl;
- int digit = 0;
- cin >> digit;
- if (!cin)
- {
- throw INT_REQ;
- }
- cout << "Static array: ";
- printArray(ARRAY, amountOfNumbers);
- cout << endl;
- cout << "The number of dividing by " <<
- digit << " in static array is equal to: " << amountDividedWithoutRemainder(ARRAY, digit) << endl;
- cout << endl;
- cout << "Enter an amount of numbers in dinamic array: " << endl;
- int n = 0;
- cin >> n;
- if (!cin)
- {
- throw INT_REQ;
- }
- int *DinamicArray = new int[n];
- for (int i = 0; i < n; i++)
- {
- DinamicArray[i] = rand();
- }
- cout << "Dinamic array: ";
- printArray(DinamicArray, n);
- cout << endl;
- cout << "The number of dividing by " <<
- digit << " in dinamic array is equal to: " << amountDividedWithoutRemainder(DinamicArray, digit) << endl;
- cout << endl;
- delete [] DinamicArray;
- ifstream file;
- file.open("input.txt");
- if (!file.is_open())
- {
- throw ERROR_FILE_NOT_OPEN;
- }
- else
- {
- delete [] DinamicArray;
- int amountOfArraysInFile = 0;
- file >> amountOfArraysInFile;
- if (!file)
- {
- throw INT_REQ;
- }
- for (int j = 0; j < amountOfArraysInFile; j++)
- {
- int amountOfNumbersInArrayInFile = 0;
- file >> amountOfNumbersInArrayInFile;
- if (!file)
- {
- throw INT_REQ;
- }
- DinamicArray = new int[amountOfNumbersInArrayInFile];
- for (int k = 0; k < amountOfNumbersInArrayInFile; k++)
- {
- file >> DinamicArray[k];
- if (!file)
- {
- throw INT_REQ;
- }
- }
- cout << "Array from file: ";
- printArray(DinamicArray, amountOfNumbersInArrayInFile);
- cout << endl;
- cout << "The number of dividing by " <<
- digit << " in dinamic array is equal to: " <<
- amountDividedWithoutRemainder(DinamicArray, digit) << endl;
- cout << endl;
- delete [] DinamicArray;
- }
- file.close();
- return 0;
- }
- }
- catch(const string &err)
- {
- cerr << endl << err << endl;
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement