2017-05-03 195 views
0

編寫一個控制檯應用程序,它將第一個二維數組(所謂的數組1)的行與第二個數組的行(所謂的數組2)相加,並將結果分配給第三個數組的行所謂的數組3),所有相同的維數。行和coloumns的上限是N,其中N是從用戶獲取的整數變量。第一個陣列的值由以下等式給出:array1(i,j)= i N + j。第二個數組的值由以下等式給出:array2(x,y)= x N - y。最後,程序逐行打印結果。二維數組數組

+1

而你的問題是什麼? –

回答

0

由於這個問題被標記的Visual Studio我相信C#是一種合適的語言:

using System; 

class MainClass { 
    public static void Main (string[] args) { 
    Console.Write("Enter the value of N:"); 
    int N = Convert.ToInt32(Console.ReadLine()); 
    int[,] array1 = new int[N, N]; 
    int[,] array2 = new int[N, N]; 
    int[,] array3 = new int[N, N]; 

    //initializing values of array-1 
    for(int i=0; i<array1.GetLength(0); i++) { 
     for(int j=0; j<array1.GetLength(1); j++) { 
     array1[i,j] = i*N+j; 
     } 
    } 

    //initializing values of array-2 
    for(int x=0; x<array2.GetLength(0); x++) { 
     for(int y=0; y<array2.GetLength(1); y++) { 
     array2[x,y] = x*N-y; 
     } 
    } 

    //initializing values of array-3 and printing results row by row 
    Console.WriteLine("array3 looks like this:"); 
    for(int a=0; a<array3.GetLength(0); a++) { 
     for(int b=0; b<array3.GetLength(1); b++) { 
     array3[a,b] = array1[a,b] + array2[a,b]; 
     Console.Write(string.Format("{0} ", array3[a, b])); 
     } 
     Console.Write(Environment.NewLine); 
    } 
    } 
} 

試試吧here!

用法示例:

Enter the value of N: 6 
array3 looks like this: 
0 0 0 0 0 0 
12 12 12 12 12 12 
24 24 24 24 24 24 
36 36 36 36 36 36 
48 48 48 48 48 48 
60 60 60 60 60 60