Given two dimensional array of size n×n. Get new array by multiplying the elements of each row of the first array by the largest from the values of the elements of the corresponding row of the second array.
First Array:
2 3 4
1 1 1
12 1 1
Second Array:
8 12 16
1 1 1
12 1 1
I tried to get this result, but I have some problem.
using System;
namespace Lab_4
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Введите размерность массива - n:");
int n = Convert.ToInt32(Console.ReadLine());
int[,] array = new int[n, n];
int[,] A = new int[n, n];
int max,i,j;
max = 999;
Random r = new Random();
for (i = 0; i < n; i )
{
for (j = 0; j < n; j )
{
array[i, j] = r.Next(1, 10);
}
}
Console.WriteLine("Ваш массив:");
for (i = 0; i < n; i )
{
for (j = 0; j < n; j )
{
Console.Write(array[i, j] " ");
}
Console.WriteLine();
}
for (i = 0; i < n; i )
{
for (j = 0; j < n - 1; j )
{
max = array[i, j];
if (max < array[i, j 1])
{
max = array[i, j];
}
}
A[i, j] = array[i, j] * max;
}
Console.WriteLine("Новый массив :");
for (i = 0; i < n; i )
{
for (j = 0; j < n; j )
{
Console.Write(A[i, j] " ");
}
Console.WriteLine();
}
}
}
}
CodePudding user response:
First of all lets call them matrixes then declare two random matrixes:
var n = 10;
var random = new Random();
var matrix1 = Enumerable.Range(0,n).Select(_=>Enumerable.Range(0, n)
.Select(_=>random.Next(1,10)).ToArray()).ToArray();
var matrix2 = Enumerable.Range(0,n).Select(_=>Enumerable.Range(0, n)
.Select(_=>random.Next(1,10)).ToArray()).ToArray();
var resultMatrix = new int[n, n];
and then multiply each element of the first to the max element of the second's from the corresponding row:
for (var i = 0; i < n; i )
{
for (var j = 0; j < n; j )
{
resultMatrix[i, j] = matrix1[i][j] * matrix2[i].Max();
}
}
CodePudding user response:
I'd break up your program into pieces to make it easier to follow:
static void Main(string[] args)
{
Console.WriteLine("Введите размерность массива - n:");
int n = Convert.ToInt32(Console.ReadLine());
int[,] array = new int[n, n];
int[,] A = new int[n, n];
int max,i,j;
max = 999;
Random r = new Random();
for (i = 0; i < n; i )
{
for (j = 0; j < n; j )
{
array[i, j] = r.Next(1, 10);
}
}
Console.WriteLine("Ваш массив:");
displayArray(array, "0");
for (i = 0; i < n; i )
{
int rowMax = getRowMax(array, i);
for (j = 0; j < n; j )
{
A[i, j] = array[i, j] * rowMax;
}
}
Console.WriteLine("Новый массив :");
displayArray(A, "00");
}
public static void displayArray(int[,] arr, String format) {
for(int row=0; row<=arr.GetUpperBound(0); row ) {
for(int col=0; col<=arr.GetUpperBound(1); col ) {
Console.Write(arr[row, col].ToString(format) " ");
}
Console.WriteLine();
}
}
public static int getRowMax(int[,] arr, int row) {
int max = arr[row, 0];
for(int col=1; col<=arr.GetUpperBound(1); col ) {
if (arr[row, col] > max) {
max = arr[row, col];
}
}
return max;
}
Sample run:
Введите размерность массива - n:
10
Ваш массив:
3 3 8 3 2 4 2 7 8 1
6 3 7 9 5 7 4 8 5 8
7 5 7 6 5 6 1 3 5 3
6 3 8 3 2 8 3 4 2 3
8 1 6 4 8 8 4 2 1 2
9 7 8 8 3 8 4 2 8 5
6 9 9 7 6 9 8 5 8 7
3 5 6 7 6 3 5 3 9 2
7 7 1 6 9 9 5 3 8 4
4 6 5 6 9 2 1 9 7 6
Новый массив :
24 24 64 24 16 32 16 56 64 08
54 27 63 81 45 63 36 72 45 72
49 35 49 42 35 42 07 21 35 21
48 24 64 24 16 64 24 32 16 24
64 08 48 32 64 64 32 16 08 16
81 63 72 72 27 72 36 18 72 45
54 81 81 63 54 81 72 45 72 63
27 45 54 63 54 27 45 27 81 18
63 63 09 54 81 81 45 27 72 36
36 54 45 54 81 18 09 81 63 54