Другие языки программирования и технологии

вычисление суммы. С/С++

задана матрица целых чисел. вычислить сумму элементов матрицы, расположенных над главной диагональю
Denis Kondratyev
Denis Kondratyev
63
C++

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;

int main(int argc, char *argv[])
{
  srand(static_cast <unsigned> (time(NULL)));
  int n = rand() % 18 + 2;
  int **a = new int* [n];
  setlocale(LC_ALL, "Russian");
  cout << "Дана матрица " << n << "x" << n << ":" << endl;
  for (int i = 0; i < n; i++)
  {
    a[і] = new int [n];
    for (int j = 0; j < n; j++)
    {
      a[і][j] = rand() % 199 - 99;
      cout << setw(4) << a[і][j];
    }
    cout << endl;
  }
  int sum = 0;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++)
      sum += a[і][j];
  cout << "Сумма элементов над главной диагональю = " << sum << endl;
  cout << endl << "Для завершения работы нажмите любую клавишу... ";
  while(!_kbhit());
  for (int i = 0; i < n; i++)
    delete [ ] a[і];
  delete a;
  return 0;
}
Aдам Дешнеев
51 590
Лучший ответ
int s = 0;
for (int i = 0; i < L; i++)
for (int j = 0; i < L; j++)
if (j == i + 1) s += arr[ j ];

Похожие вопросы