How can I use a for loop to load elements into each of my arrays and then output the sum of each array into a sum array?

I am working on a C++ application that will load elements into the first array which is 10 rows by 10 columns and will load elements into a second array that is also a 10 by 10 and then add each array elements together and put them in an array called sum. For example, in real when you do addition of matrices, you would do something like this below, but my arrays are more complex then this. The question is how I can use a nested for loop to load elements into each of my arrays. Any examples would be much appreciated!!! Here is the code I have so far. I know how to output elements in an array, but I never loaded elements into an array using for loop. The comment part where you see the nested for loop going through the elements row by column and then storing them into the first array, I am not to sure about. Can someone please help me or refer me to some online resources?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  
#include "stdafx.h" 
#include <iostream> 
#include <cstdlib>
#include <string> 
using namespace std;   
int main() 
{ 
 
   /* 
 
  For this version, the matrices are square (defined by size, which is a maximum of 10).  
  Thus, m & n (rows & columns) are the same.   
  Later versions can allow the number of rows and columns to be different. 
 
   size - the dimension of both matrices.  
   m - the number of rows   
   n - the number of columns  
   c - the delimiter of the outer loop         
   d - the delimiter of the inner loop 
 
   first  - the first matrix  
   second - the second matrix 
   sum - holds the sum of the 2 matrices 
 
    */ 
 
      int size, m, n, c, d, first[10][10], second[10][10], sum[10][10]; 
 
     cout << "Enter the number of rows and columns of matrices: ";         
     cin >> size;  m = size; n = size;    

     
    /* for (int i=0; i<size; ++i)
      {
        for (int j=0; j<size; ++j)
        {
          cout << "first[" << i << "][" << j << "]" <<endl;
          cin >> first[i][j]; 
        }
      }
    */ 


    // Load the first matrix 

      cout << "Enter the elements of first matrix: "; 
 


    // Load the second matrix    
     
     /* for (int i=0; i<size; ++i)
       {
          for (int j=0; j<size; ++j)
          {
            cout << "second[" << i << "][" << j << "]" <<endl;
            cin >> second[i][j];
          }
        }
    */ 
    
    cout << "Enter the elements of second matrix: ";    
     
   // Sum the elements of the matrices 
 
   // Print the sum matrix
 
   cout << "Hit any key to continue" << endl; 
 
    system ("pause");    return 0; 
}
Your code is fine and since you're going to be running the same code twice to populate the two arrays you might also consider wrapping a function around this code. Then call the function twice instead of typing everything out two times.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

constexpr auto SIZE = 10;
void fillArray (int ary[][SIZE])

{
    for (size_t i = 0; i < SIZE; ++i)
      {
        for (size_t j = 0; j < SIZE; ++j)
        {
          std::cout << "array element[" << i << "][" << j << "]" << std::endl;
          std::cin >> ary[i][j];
        }
      }
}
void printArray (const int ary[][SIZE])
{
    for (size_t i = 0; i < SIZE; ++i)
    {
        for (size_t j = 0; j < SIZE; ++j)
        {
            std::cout << ary[i][j] << " ";
        }
        std::cout << "\n";
    }
}
int main()
{
    int first[SIZE][SIZE];
    int second[SIZE][SIZE];

    fillArray(first);
    fillArray(second);

    printArray(first);
    printArray(second);
}
Last edited on
@gunnerfunner (1472),

Do you think you could show me an example of loading the arrays using the code I already provided? Also, how would you make a sum array using the two arrays you demonstrated? I like how you made different functions- one to fill the arrays and one to output the arrays, but I just trying to keep it simple. Any examples would be good relative to my questions. Thank you!!!! You code does have errors in it. I debugged it. Especially when you declare constexpr auto SIZE= 10; (I might have to update my compiler or is there a way to work around this compiler error?
As I said, your code was fine and I used lines 34-42 (or lines 51-61 if you will since they're repeated) of your OP for the fillArray() function, changing the variable names and using size_t instead of int
for sum array you'd have to declare another array sumArray[10][10] such that:
 
sumArray[i][j] = first[i][j] + second[i][j]; 

you should of course also check that the two matrices can indeed be added together, in this case it's perhaps true by construction but generally one should always check these
My program is fine, just re-tested it on the shell: http://cpp.sh/6mzp
I think you need to update your compiler since constexpr, auto are newer features but, in the meantime, you can use const size_t SIZE = 10; instead
@gunnerfunner (1473),
Thank you for showing me how add matrices!!!!!!!!!
Last edited on
Topic archived. No new replies allowed.