Solved

Add values of each row in an array

Posted on 2016-10-28
3
31 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now