Machine Problem 5.9.
Write a program using two-‐dimensional arrays that computes the sum of data in rows
and sum of data in columns of the 3x3 (three by three) array variable n[3[3].
Sample input/output dialogue:
5 9 8 = 22
3 8 2 = 13
4 3 9 = 16
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
12 20 19
public static void Main(string[] args)
{
int n = Convert.ToInt16( Console.ReadLine( ) ) + 1;
int[ , ] arr = new int[ n, n ];
Console.WriteLine( "enter the numbers" );
for ( int i = 0; i < n - 1; i++ )
{
for ( int j = 0; j < n - 1; j++ )
{
arr[ i, j ] = Convert.ToInt16( Console.ReadLine( ) );
}
}
for (int i = 0; i < n - 1 ; i++) {
for (int j = 0; j < n - 1; j++) {
arr[i, n - 1] += arr[i, j];
arr[n - 1, j] += arr[i, j];
}
}
for (int p = 0; p < n; p++) {
for (int s = 0; s < n; s++) {
if (s == n - 2 && p != n - 1) {
Console.Write(arr[p, s] + " = ");
} else if (p == n - 1 && s == n - 1) {
} else {
Console.Write(arr[p, s] + " ");
}
}
if (p == n - 2) {
Console.WriteLine("\n------------");
} else {
Console.WriteLine();
}
}
}
Comments
Leave a comment