Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Add values of each row in an array

Posted on 2016-10-28
Medium Priority
109 Views
Hello,

Not sure where I am going wrong, but I want to display the sum of each row of the array:

``````#include <iostream>
using namespace std;
int main() {
int sum = 0;
int i, j,val[3][4] = {8, 16, 9, 52, //85
3, 15, 27, 6, //51
14, 25, 2, 10}; //51
i = 0;
for (j = 0; j < 4; j++)
sum = sum+ val[i][j];
cout << "The value of row 1 is " << sum << endl;
cout << "The value of row 2 is " << sum << endl;
cout << "The value of row 3 is " << sum << endl;

return 0;
}
``````
0
Question by:Computer Guy
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 15

Accepted Solution

Minh Võ Công earned 2000 total points
ID: 41864854
``````int val[3][4] = {{8, 16, 9, 52}, //85
{3, 15, 27, 6}, //51
{14, 25, 2, 10}}; //51
for (i = 0; i < 3; i++)
{
sum = 0;
for (j = 0; j < 4; j++)
sum = sum+ val[i][j];
cout << "The value of row "<< i+1<< " is " << sum << endl;
}
``````
0

LVL 32

Expert Comment

ID: 41864915
``````#include <iostream>
#include <numeric>

using namespace std;

int main()
{
int i;
int val[3][4] = {{8, 16, 9, 52}, //85
{3, 15, 27, 6}, //51
{14, 25, 2, 10}}; //51

for (i = 0; i < 3; i++)
{
cout << "The value of row "<< i+1<< " is "
<< accumulate( &val[i][0], &val[i][4], 0) << endl;
}
}
``````
http://www.cplusplus.com/reference/numeric/accumulate/?kw=accumulate
0

LVL 3

Author Closing Comment

ID: 41865115
Thank you!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month11 days, 14 hours left to enroll