T
T
tera10042018-12-12 16:15:52
C++ / C#
tera1004, 2018-12-12 16:15:52

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

3 answer(s)
#
#, 2018-12-12
@tera1004

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");

in this case, you can simply assign the value to the control
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");

and you can check the squareness of the matrix before the start of the cycles, as well as iterate over the "triangle"
spoiler
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");
        }
    }
}

in the comments it was suggested not to scan the main diagonal .. and this is logical! )) .. well, actually, it’s just done
spoiler
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");
        }
    }
}

well, a bit of functional style + syntactic sugar
spoiler
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");
    }
}

выхлоп
1P9RDij.png

D
Dmitry Belyaev, 2018-12-12
@bingo347

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");

F
Farwisdomer, 2018-12-12
@Farwisdomer

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;

if the condition is not met after the loop, output is symmetrical.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question