Программа определения количества симметричных матриц - C#

Узнай цену своей работы

Формулировка задачи:

Заданы две матрицы С(4, 4) и D(3, 3). Написать программу определения количества симметричных матриц. Матрица называется симметричной, если транспонированная матрица равна исходной. Для каждой симметричной матрицы вычислить сумму элементов, лежащих вне главной диагонали. ИМЕЕТСЯ СЛЕДУЮЩИЙ (ОШИБОЧНЫЙ) КОД:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Laba7
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[,] c = new Int32[4, 4];
            Int32[,] ct = new Int32[4, 4];
            Int32[,] d = new Int32[3, 3];
            Int32[,] dt = new Int32[3, 3];
            Int32 p1, p2;
            int ic, jc, id, jd;
            Console.WriteLine("Введите массив C");
            for (ic = 0; ic < 4; ic++)
            {
                for (jc = 0; jc < 4; jc++)
 
                    c[ic, jc] = Convert.ToInt32(Console.ReadLine());
            }
            for (ic = 0; ic < 4; ic++)
            {
                for (jc = 0; jc < 4; jc++)
                {
                    Console.Write("{0,5}", c);
                }
                Console.WriteLine();
            }
            // transp(c, 4, ct); 
            if (c.Equals(ct))
            {
                Console.WriteLine("Массив С симметричный");
                p1 = c[1, 2] + c[1, 3] + c[1, 4] + c[2, 1] + c[2, 3] + c[2, 4] + c[3, 1] + c[3, 2] + c[3, 4] + c[4, 1] + c[4, 2] + c[4, 3];
                Console.WriteLine(p1);
            }
            else
                Console.WriteLine("Массив С не симметричный");

            Console.WriteLine("Введите массив D");
            for (id = 0; id < 3; id++)
            {
                for (jd = 0; jd < 3; jd++)
                    d[id, jd] =
                    Convert.ToInt32(Console.ReadLine());
            }
            transp(d, 3, dt);
            if (d.Equals(dt))
            {
                Console.WriteLine("Массив D симметричный");
                p2 = d[1, 2] + d[1, 3] + d[2, 1] + d[2, 3] + d[3, 1] + d[3, 2];
                Console.WriteLine(p2);
            }
            else
                Console.WriteLine("Массив D не симметричный");

        }
 
        static void transp(Int32[,] mtr, int p, Int32[,] mtrt)
        {
            int i; int j;
            for (i = 0; i < p; i++)
            {
                for (j = 0; j < p; j++)
                    mtrt[j, i] = mtr[i, j];
            }
        }
    }
}
ИЗВИНИТЕ,ЕСЛИ НЕПРАВИЛЬНО ОФОРМИЛ. ОЧЕНЬ НАДО. БУДУ БЛАГОДАРЕН

Решение задачи: «Программа определения количества симметричных матриц»

textual
Листинг программы
using System;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int i, j;
            int[,] C = new int[4, 4];
            int[,] D = new int[3, 3];
            Console.WriteLine("Заполните матрцу C");
            for (i = 0; i < 4; i++)
            {
                for (j = 0; j < 4; j++)
                {
                    Console.Write("Введите элемент [{0},{1}] ",i,j);
                    C[i, j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("Заполните матрцу D");
            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.Write("Введите элемент [{0},{1}] ", i, j);
                    D[i, j] = int.Parse(Console.ReadLine());
                }
            }
            if (Semetr(C))
            {
                int sum = 0;
                for (i = 0; i < 4; i++)
                {
                    for (j = 0; j < 4; j++)
                    {
                        if (i != j)
                        {
                            sum += C[i, j];
                        }
                    }
                }
                        Console.WriteLine("Сумма : {0} ",sum);
                        Console.WriteLine("Матрица C : семетрична");
            }
            else
                Console.WriteLine("Матрица C : не семетрична");
            if (Semetr(D))
            {
                int sum = 0;
                for (i = 0; i < 3; i++)
                {
                    for (j = 0; j < 3; j++)
                    {
                        if (i != j)
                        {
                            sum += D[i, j];
                        }
                    }
                }
                Console.WriteLine("Сумма : {0} ", sum);
                Console.WriteLine("Матрица D : семетрична");
            }
            else
                Console.WriteLine("Матрица D : не семетрична");
            Console.ReadKey();
        }
        static bool Semetr(int[,] a)
        {
            bool symm = true;
            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])
                    {
                        symm = false;
                        break;
                    }
                if (!symm) break;
            }
            return symm;
        }
    } 
 
 
}

Оцени полезность:

5   голосов , оценка 4.2 из 5
Похожие ответы