Solved

Add values of each row in an array

Posted on 2016-10-28
3
97 Views
Last Modified: 2016-10-29
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;
}

Open in new window

0
Comment
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
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 500 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;
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
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;
  }
}

Open in new window

http://www.cplusplus.com/reference/numeric/accumulate/?kw=accumulate
0
 
LVL 3

Author Closing Comment

by:Computer Guy
ID: 41865115
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question