Advertisement
Mexahoid

Array

Oct 21st, 2021
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. // massiv.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. int solution(int[], int);
  10.  
  11. int main()
  12. {
  13.     const char *arr[5] = { "data1.txt", "data2.txt", "data3.txt", "data4.txt", "data5.txt" };
  14.  
  15.     int f = 1;
  16.  
  17.     while (f)
  18.     {
  19.         cout << "Choose file: " << endl;
  20.         int count = sizeof(arr) / sizeof(char*);
  21.         for (int i = 0; i < count; i++)
  22.         {
  23.             cout << i+1 << ". " << arr[i] << endl;
  24.         }
  25.  
  26.         int num = -1;
  27.  
  28.         while (num < 1 || num > count)
  29.         {
  30.             cout << "Enter number >> ";
  31.             cin >> num;
  32.         }
  33.  
  34.         fstream fin = fstream(arr[num - 1]);
  35.         if (!fin)
  36.         {
  37.             cout << "Could not open file" << endl;
  38.             continue;
  39.         }
  40.  
  41.         int n = 0;
  42.         fin >> n;
  43.         if (!n)
  44.         {
  45.             fin.close();    // закрыть файл с данными
  46.             cout << "Error in file format" << endl;
  47.             continue;
  48.         }
  49.  
  50.         int* a = new int[n];
  51.  
  52.         int k = 0;
  53.         cout << "Array elements:" << endl;
  54.         while (!fin.eof() && k < n)
  55.         {
  56.             fin >> a[k];
  57.             cout << a[k++] << " ";
  58.         }
  59.         cout << endl;
  60.         fin.close();
  61.  
  62.         if (k < n)
  63.         {
  64.             cout << "More elements declared than found: declared " << n << ", found " << k << endl;
  65.             continue;
  66.         }
  67.  
  68.         int res = solution(a, n);
  69.         switch (res)
  70.         {
  71.         case 1:
  72.             cout << "Sequence is ascending";
  73.             break;
  74.  
  75.         case 0:
  76.             cout << "Sequence is not ascending";
  77.             break;
  78.  
  79.         case -1:
  80.             cout << "Less than 2 elements";
  81.             break;
  82.         }
  83.  
  84.         fin.close();    // закрыть файл с данными
  85.         cout << endl;
  86.         cout << "Type 0 if you want to exit, 1 if you want to run program again >> ";
  87.         cin >> f;
  88.     }
  89.  
  90.     return 0;
  91. }
  92.  
  93. int solution(int b[], int m)
  94. {
  95.     if (m < 2)
  96.         return -1;
  97.  
  98.     bool asc = true;
  99.     int prev = b[0];
  100.     for (int i = 1; i < m; i++)
  101.     {
  102.         if (prev > b[i])
  103.             asc = false;
  104.         prev = b[i];
  105.     }
  106.  
  107.     return asc;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement