Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add values of each row in an array

Posted on 2016-10-28
3
Medium Priority
?
120 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 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;
}

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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 …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

972 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