Thread: Adding-up Elements of a Two-Dimensional Array

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    17

    Adding-up Elements of a Two-Dimensional Array

    Good day guys, I would just like to share my code and wanted to do something about it. This code shows a loop inside a loop initializing a two-dimensional array named arr with elements [2] and [5]. Here's my code:
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main ()
    {
        int arr [2] [5];
        int val = 1;
    
    
        
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                arr [i][j] = val;
                cout << arr [i][j] << " ";
                val++;
                }
                            cout << endl;
                }
                
    
    
                
                getch ();
                return 0;
                }

    Now, what I wanted to do is to have an output showing the sums of each elements. Example, the above code has an output of:
    1 2 3 4 5
    6 7 8 9 10
    I wanted to have more codes which would add up the elements 1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10 and produce and output:
    7 9 11 13 15


    Hope anyone would let me lend me their precious time and expertise for such matter. Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How would you do it on paper?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    Well, this:
    Code:
    sum = arr[0][0] + arr [0][5];
    cout << sum <<" ";
    would give you your first sum of 7, and:
    Code:
    sum = arr[0][1] + arr[1][1];
    cout << sum <<" ";
    would give you your second sum of 9.

    Just need to figure out a way to implement that into a loop.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by setleaf View Post
    Well, this:
    Code:
    sum = arr[0][0] + arr [0][5];
    cout << sum <<" ";
    would give you your first sum of 7, and:
    wrong. it should be:

    Code:
    sum = arr[0][0] + arr [1][0];
    cout << sum <<" ";
    Quote Originally Posted by setleaf View Post
    Code:
    sum = arr[0][1] + arr[1][1];
    cout << sum <<" ";
    would give you your second sum of 9.
    This one is correct.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    Whoops, good call Elkvis. I haven't had my coffee yet.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    In addition, you should really be defining constants for the size of your array and loop iterations.

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    const int rows = 2;
    const int columns = 5;
    
    int main ()
    {
        int arr [rows] [columns];
        int val = 1;
     
     
         
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                //etc...
    It might also be a good idea to change i and j to row and column, respectively. Descriptive variable names are to be preferred over single-letters.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Aug 2014
    Posts
    17
    Here's my final code and it works. Can't do it without your help guys, thanks.
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main ()
    {
        int arr [2] [5];
        int val = 1;
    
    
        
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                arr [i][j] = val;
                val++;
                }
                }
                
        int sum[5] = {0};
    	for (int j=0; j <5; j++)
    	{
    		for (int i=0; i <2; i++)
    		 {
    			 sum [j]+=arr [i][j];
    		 }
    	}
    	
    	for (int j=0; j <5; j++) cout << sum[j] << " ";
    	cout << endl;
                
    
    
                
                getch ();
                return 0;
                }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,556
    Good to hear that it works for you, but you need to work on your code formatting, particularly your indentation. For example:
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
        int arr[2][5];
        int val = 1;
    
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                arr[i][j] = val;
                val++;
            }
        }
    
        int sum[5] = {0};
        for (int j = 0; j < 5; j++)
        {
            for (int i = 0; i < 2; i++)
            {
                sum[j] += arr[i][j];
            }
        }
    
        for (int j = 0; j < 5; j++)
        {
            cout << sum[j] << " ";
        }
        cout << endl;
    
        getch();
        return 0;
    }
    Keep in mind that <conio.h> and getch are non-standard, and that you do not actually need them here since you can either run your program from a separate command prompt window, configure your IDE to pause your program before it exits, or use standard facilities to pause your program before it exits.

    Also, instead of using the magic numbers 2 and 5 all over the place, you should have defined named constants.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2014
    Posts
    17
    @laserlight : I see, thanks dude. Sorry for being such a newbie. :3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in shifting elements in 2 Dimensional array
    By biswanath in forum C Programming
    Replies: 4
    Last Post: 03-01-2013, 09:53 PM
  2. help with adding elements to an array
    By cseter in forum C Programming
    Replies: 1
    Last Post: 03-18-2010, 10:25 AM
  3. adding elements in array
    By reb221 in forum C Programming
    Replies: 6
    Last Post: 12-30-2008, 02:41 PM
  4. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 10:02 PM
  5. Adding elements to array?
    By INFERNO2K in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2005, 01:56 PM

Tags for this Thread