Answer the question
In order to leave comments, you need to log in
How to output in a loop once?
The code checks if the array is symmetrical about the main diagonal.
I have a problem with output. Output every cycle. It is necessary that it is displayed only once in accordance with the result. If each time the If loop is true , then the array is "symmetrical". If it gives false at least once, then the output is "not symmetrical"
int n = 3;
int[,] a = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
for (int i = 0; i < a.GetLength(0); ++i)
for (int j = 0; j < a.GetLength(1); ++j)
if (a[i, j] == a[j, i])
Console.WriteLine("Симметрична");
else Console.WriteLine("Ne simmetrichna");
Answer the question
In order to leave comments, you need to log in
well then:
int[,] a = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
bool s = true;
for (int i = 0; i < a.GetLength(0) && s; ++i)
for (int j = 0; j < a.GetLength(1) && s; ++j)
s &= a[i, j] == a[j, i];
Console.WriteLine(s ? "Симметрична" : "Ne simmetrichna");
int[,] a = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
bool s = true;
for (int i = 0; i < a.GetLength(0) && s; ++i)
for (int j = 0; j < a.GetLength(1) && s; ++j)
s = a[i, j] == a[j, i];
Console.WriteLine(s ? "Симметрична" : "Ne simmetrichna");
using System;
namespace simm_array
{
class Program
{
static void Main(string[] args)
{
var m = new int[,] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
var w = m.GetLength(0);
var h = m.GetLength(1);
var s = w == h;
for (var x = 0; x < w && s; ++x)
for (var y = x; y < h && s; ++y)
s = m[x, y] == m[y, x];
Console.WriteLine(s ? "Симметрична" : "Ne simmetrichna");
}
}
}
using System;
namespace simm_array
{
class Program
{
static void Main(string[] args)
{
var m = new int[,] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
var w = m.GetLength(0);
var h = m.GetLength(1);
var s = w == h;
for (var x = 0; x < w && s; ++x)
for (var y = x + 1; y < h && s; ++y)
s = m[x, y] == m[y, x];
Console.WriteLine(s ? "Симметрична" : "Ne simmetrichna");
}
}
}
using System;
namespace simm_array
{
static class Program
{
static void Main(string[] args)
{
var m = new int[,] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
m.dump();
m.isSymmetrical().report();
}
static bool isSymmetrical(this int[,] m)
{
var w = m.GetLength(0);
var h = m.GetLength(1);
var s = w == h;
for (var x = 0; x < w && s; x++)
for (var y = x + 1; y < h && s; y++)
s = m[x, y] == m[y, x];
return s;
}
static void dump(this int[,] m)
{
var w = m.GetLength(0);
var h = m.GetLength(1);
for (var x = 0; x < w; x++)
{
for (var y = 0; y < h; y++)
{
Console.Write($"\t{m[x, y]}");
}
Console.WriteLine();
}
Console.WriteLine();
}
static void report(this bool s) => Console.WriteLine(s ? "Симметрична" : "Ne simmetrichna");
}
}
int[,] a = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
bool s = true;
for (int i = 0; s && i < a.GetLength(0); ++i)
for (int j = 0; s && j < a.GetLength(1); ++j)
s = s && a[i, j] == a[j, i];
Console.WriteLine(s ? "Симметрична" : "Ne simmetrichna");
If at least 1 time a [i, j] <> a[j, i]
, then the martitsa is asymmetric. So it suffices to change the condition
if (a[i, j] <> a[j, i])
Console.WriteLine("Ne simmetrichna")
break;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question