Advertisement
dmzem

Untitled

Dec 5th, 2022
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. const int s = 10;
  7. int arr[s];
  8.  
  9. void createArray()
  10. {
  11.     srand(time(0));
  12.     for (int i = 0; i < s; i++)
  13.         arr[i] = rand();
  14. }
  15.  
  16. void printArray()
  17. {
  18.     for (int i = 0; i < s; i++)
  19.         cout << "A[" << i << "]=" << arr[i] << endl;
  20. }
  21.  
  22. int sumArray()
  23. {
  24.     int sum = 0;
  25.     for (int i = 0; i < s; i++)
  26.         sum += arr[i];
  27.     return sum;
  28. }
  29.  
  30. int sumArray(int a, int b)
  31. {
  32.     int sum = 0;
  33.     for (int i = a; i < b + 1; i++)
  34.         sum += arr[i];
  35.     return sum;
  36. }
  37.  
  38. string pythagoras()
  39. {
  40.     string fname = "Novyy tekstovyy dokument.txt";
  41.     ofstream out(fname);
  42.     for (int i = 0; i <= 9; i++)
  43.         out << i << '\t';
  44.     out << endl;
  45.  
  46.     for (int i = 1; i <= 9; i++)
  47.     {
  48.         out << i << '\t';
  49.         for (int j = 1; j <= 9; j++)
  50.             out << i * j << '\t';
  51.         out << endl;
  52.     }
  53.     return fname;
  54. }
  55.  
  56. string pythagoras(string fname)
  57. {
  58.     ofstream out(fname);
  59.     for (int i = 0; i <= 9; i++)
  60.         out << i << '\t';
  61.     out << endl;
  62.  
  63.     for (int i = 1; i <= 9; i++)
  64.     {
  65.         out << i << '\t';
  66.         for (int j = 1; j <= 9; j++)
  67.             out << i * j << '\t';
  68.         out << endl;
  69.     }
  70.     return fname;
  71. }
  72.  
  73. int main()
  74. {
  75.     cout << "Zemlyakov" << endl;
  76.     createArray();
  77.     cout << "Odnomernyy massiv iz sluchaynykh chisel: " << endl;
  78.     printArray();
  79.     cout << "Summa vsekh chisel odnomernogo massiva : " << sumArray() << endl;
  80.     cout << "Summa ot 3-go do 7-go elementa massiva : " << sumArray(3, 7) << "\n\n";
  81.     cout << "Zapisan fayl " << pythagoras() << endl;
  82.     cout << "Zapisan fayl " << pythagoras("Pythagoras.txt") << endl;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement