We need to find the sum of the columns of the array.
But not added the last items, how to do?
for (int j = 0; j < m; j++) { Sum[j] = D[mesto][j]; Sum[j] = Sum[j] + D[mesto + 1][j]; cout << "Summ:" << Sum[j] << endl; } We need to find the sum of the columns of the array.
But not added the last items, how to do?
for (int j = 0; j < m; j++) { Sum[j] = D[mesto][j]; Sum[j] = Sum[j] + D[mesto + 1][j]; cout << "Summ:" << Sum[j] << endl; } The code is completely incomprehensible actually needed. Perhaps something like:
for( int i = 0; i < mesto; i++ ) { Sum[mesto] = 0; for(int j = 0; j < m; j++) { Sum[mesto] += D[mesto][j]; } cout << "Sum[" << mesto << "]: " << Sum[mesto] << endl; } Below is a demo program that answers your question.
How to find the sum of the elements of the array columns?
#include <iostream> #include <iomanip> int main() { const size_t Rows = 3; const size_t Cols = 4; int a[Rows][Cols] = { { 0, 1, 2, 3, }, { 4, 5, 6, 7, }, { 8, 9, 10, 11, }, }; long long int sum[Cols]; for ( size_t col = 0; col < Cols; col++ ) { sum[col] = 0; for ( size_t row = 0; row < Rows; row++ ) { sum[col] += a[row][col]; } } for ( size_t row= 0; row < Rows; row++ ) { for ( size_t col = 0; col < Cols; col++ ) { std::cout << std::setw( 2 ) << a[row][col] << ' '; } std::cout << std::endl; } std::cout << std::endl; for ( long long int x : sum ) std::cout << std::setw( 2 ) << x << ' '; std::cout << std::endl; return 0; } The output of the program to the console is as follows.
0 1 2 3 4 5 6 7 8 9 10 11 12 15 18 21 The program uses a for -range-based cycle to display column sums. If your compiler does not support such a construct, simply replace it with a regular for loop using indexes.
Apparently you need to find the "Sum of all elements of the array." Here for an example an array from 3 elements. But this solution is suitable if you have a static array, that is, it has a fixed number of elements.
int a[3]; a[0] = 5; a[1] = 10; a[2] = 22; int c = 0; for(int z = 0; z<3;z++) { int b = a[z]; c = c + b; } Source: https://ru.stackoverflow.com/questions/586157/
All Articles