如何使用for循环将元素加载到每个数组中,然后将每个数组的总和输出到sum数组中?

时间:2017-04-09 22:02:30

标签: c++ arrays

#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;    

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

    // Load the second matrix    

    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; 
} 

大家好,

我正在开发一个C ++应用程序,它将元素加载到10行10列的第一个数组中,并将元素加载到第二个数组中,该数组也是10 x 10,然后将每个数组元素添加到一起并放入它们在一个名为sum的数组中。例如,实际上当你添加矩阵时,你会在下面做这样的事情,但是我的数组比这更复杂。问题是如何使用嵌套for循环将元素加载到我的每个数组中。任何例子都将非常感谢!!!这是我到目前为止的代码。我知道如何在数组中输出元素,但我从未使用for循环将元素加载到数组中。

[1 2 4] + [2 3 7] = [3 5 11]

1 个答案:

答案 0 :(得分:0)

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