2017-04-09 8 views
-4
#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; 
} 

こんにちは私は10列で10行であり、また10である二番目の配列内の要素をロードする最初の配列に要素をロードするC++アプリケーションに取り組んでいます誰もが、forループを使用して各配列に要素をロードし、各配列の合計を合計配列に出力できますか?

10とし、各配列要素をまとめてsumと呼ばれる配列に入れます。たとえば、実際に行列を追加すると、以下のようなことが起こりますが、配列はもっと複雑になります。問題は、各配列に要素をロードするためにネストされたforループを使用する方法です。どんな例も大変ありがとう!ここに私がこれまで持っていたコードがあります。私は配列の要素を出力する方法を知っていますが、forループを使って要素を配列にロードしたことはありません。

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

+2

あなたも自分でこの問題を解決しようとしましたか?あなたのコードはどんな種類の「for」ループやあなたの意図の他の部分もトレースしていません – UnholySheep

+0

@ UnholySheepあなたはこの種の問題について私にいくつかのオンラインリソースを紹介してくれると思いますか? – DSeals

+0

@ UnholySheepどのように私の問題を解決するための参照? – DSeals

答えて

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]; 
    } 
} 
+0

@ Shibli-だから、最初の行列のすべての行と列を通り、各要素を最初の行列に格納するforループを作成しましたか?実際の価値はどこで再生されますか?私は各マトリックスが100の要素を持っていることを知っていますし、合計も100の要素も持っていることを知っています。 – DSeals

+0

コードは行列の各要素の値を求め、 'std :: cin'で与えたものを格納します。実際の値ではどういう意味ですか? – Shibli

関連する問題