Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::string;
  9. using std::cerr;
  10. using std::rand;
  11. using std::ifstream;
  12.  
  13. const int amountOfNumbers = 10;
  14. int ARRAY[amountOfNumbers] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  15. const string INT_REQ = "Main: Integer required!";
  16. const string ERROR_FILE_NOT_OPEN = "Main: Cannot open the file.";
  17.  
  18. void printArray(int *arr, int n)
  19. {
  20.     for (int elem = 0; elem < n; elem++)
  21.     {
  22.         cout << arr[elem] << ' ';
  23.     }
  24.  
  25. }
  26.  
  27.  
  28. int amountDividedWithoutRemainder(int *arr, int digit)
  29. {
  30.     int count = 0;
  31.     for (int elem : arr)
  32.     {
  33.         if (elem % digit == 0)
  34.         {
  35.             ++ count;
  36.         }
  37.        
  38.     }
  39.     return count;
  40. }
  41.  
  42. int main()
  43. {
  44.     try
  45.     {
  46.         cout << "Enter a digit to divide to: " << endl;
  47.         int digit = 0;
  48.         cin >> digit;
  49.         if (!cin)
  50.         {
  51.             throw INT_REQ;
  52.         }
  53.  
  54.         cout << "Static array: ";
  55.         printArray(ARRAY, amountOfNumbers);
  56.         cout << endl;
  57.  
  58.         cout << "The number of dividing by " <<
  59.         digit << " in static array is equal to: " << amountDividedWithoutRemainder(ARRAY, digit) << endl;
  60.         cout << endl;
  61.  
  62.         cout << "Enter an amount of numbers in dinamic array: " << endl;
  63.         int n = 0;
  64.         cin >> n;
  65.         if (!cin)
  66.         {
  67.             throw INT_REQ;
  68.         }
  69.         int *DinamicArray = new int[n];
  70.         for (int i = 0; i < n; i++)
  71.         {
  72.             DinamicArray[i] = rand();
  73.         }
  74.         cout << "Dinamic array: ";
  75.         printArray(DinamicArray, n);
  76.         cout << endl;
  77.  
  78.         cout << "The number of dividing by " <<
  79.         digit << " in dinamic array is equal to: " << amountDividedWithoutRemainder(DinamicArray, digit) << endl;
  80.         cout << endl;
  81.         delete [] DinamicArray;
  82.         ifstream file;
  83.         file.open("input.txt");
  84.         if (!file.is_open())
  85.         {
  86.             throw ERROR_FILE_NOT_OPEN;
  87.         }
  88.         else
  89.         {
  90.             delete [] DinamicArray;
  91.             int amountOfArraysInFile = 0;
  92.             file >> amountOfArraysInFile;
  93.             if (!file)
  94.             {
  95.                 throw INT_REQ;
  96.             }
  97.            
  98.             for (int j = 0; j < amountOfArraysInFile; j++)
  99.             {
  100.                 int amountOfNumbersInArrayInFile = 0;
  101.                 file >> amountOfNumbersInArrayInFile;
  102.                 if (!file)
  103.                 {
  104.                     throw INT_REQ;
  105.                 }
  106.                 DinamicArray = new int[amountOfNumbersInArrayInFile];
  107.  
  108.                 for (int k = 0; k < amountOfNumbersInArrayInFile; k++)
  109.                 {
  110.                     file >> DinamicArray[k];
  111.                     if (!file)
  112.                     {
  113.                         throw INT_REQ;
  114.                     }
  115.                 }
  116.                 cout << "Array from file: ";
  117.                 printArray(DinamicArray, amountOfNumbersInArrayInFile);
  118.                 cout << endl;
  119.                 cout << "The number of dividing by " <<
  120.                 digit << " in dinamic array is equal to: " <<
  121.                 amountDividedWithoutRemainder(DinamicArray, digit) << endl;
  122.                 cout << endl;
  123.                 delete [] DinamicArray;
  124.  
  125.             }
  126.             file.close();
  127.             return 0;
  128.         }
  129.     }
  130.     catch(const string &err)
  131.     {
  132.         cerr << endl << err << endl;
  133.         return -1;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement