Solved

Add values of each row in an array

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 72
c++ getting the first 10 characters of a char* string 11 96
Dialogbox API leak? 18 96
VS2015 Redefinition errors 4 52
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

831 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