fork download
  1. #include <iostream>
  2. #include <locale>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. bool readFile(char*fname, int*a, int&n) {
  7. ifstream fin;
  8. fin.open(fname);
  9. if (!fin) return 0;
  10. fin >> n;
  11. a = new int[n];
  12. for (int i = 0; i < n; i++)
  13. fin >> a[i];
  14.  
  15. fin.close();
  16. return 1;
  17. }
  18.  
  19.  
  20.  
  21. int pr(int * a, int n)
  22. //Функция вычисления произведения четных элементов
  23. {
  24. int preven = 1;
  25. for (int i = 0; i < n-1; i++) {
  26. if (a[i+1]%2 == 0)
  27. preven*= a[i];
  28. }
  29. return preven;
  30. }
  31. int zerosum(int * a, int n)
  32. {
  33. int i1, i2;
  34. int sum;
  35. sum = 0;
  36. i1 = 1;
  37. i2 = 1;
  38. for (int i = 0; i<n; i++)
  39. if (a [i] == 0){
  40. if (i1 != 0)
  41. i1 = 0;
  42. else
  43. i2 = 0;};
  44. for (i1<=i2; i1++;)
  45. sum = sum+a[i1];
  46. return sum;
  47. }
  48.  
  49. void sort(int *a, int n)
  50. {
  51. for (int i = 0; i < n - 1; i++) // i - номер прохода
  52. {
  53. for (int j = 0; j < n - 1; j++) // внутренний цикл прохода
  54. {
  55. if (a[j] < a[j+1])
  56. {
  57. swap(a[j + 1], a[j]);
  58. }
  59. }
  60. }
  61. for (int i = 0; i < n; i++)
  62. cout << a[i] << "\t";
  63. }
  64. int main() {
  65. setlocale(LC_ALL, "");
  66. int n;
  67. int *a = new int;
  68.  
  69. char fname[10];
  70. cout << "fname="; cin >> fname;
  71. if (!readFile(fname, a, n)) {
  72. cout << "nofile\n";
  73. return 0;
  74. }
  75. for (int i=0; i<n; i++)
  76. cout << a[i];
  77. cout << endl;
  78. cout << "Произведение четных элементов равно " << pr(a, n) << endl;
  79.  
  80. cout << "Сумма элементов между первым и последним нулевыми элементами равна " << zerosum(a, n) << endl;
  81. sort(a, n);
  82. cout << endl;
  83. }
  84.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
fname=nofile