通过用户输入乘以矩阵

时间:2015-11-24 08:34:54

标签: c# multidimensional-array visual-studio-2015 user-input

这是一个代码示例,它将矩阵乘以一些用户输入。应该通过用户输入的数量重复该动作。例如,如果我输入数字3,我得到3个相同的印刷矩阵。相反,我希望该矩阵与数字3相乘3次(每个新矩阵乘以数字3)。有谁知道我怎么做?谢谢

*using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace user_input
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] nodes = { "1", "2", "3", "4", "5", "6" };
            int[,] adjmatrix = new int[,]
                    {{2,7,3,8,4},
                     {7,0,8,2,6},
                     {6,8,4,9,7},
                     {8,5,9,8,8},
                     {4,9,7,8,1}
                    };
            int[,] newmatrix = new int[adjmatrix.GetLength(0), adjmatrix.GetLength(1)];
            Printmatrix(adjmatrix, nodes, nodes, "Input Matrix");
            Newline();
            int n = 0;
            System.Console.WriteLine("Please enter a number");
            n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                MultipliedMatrix(ref newmatrix, adjmatrix, n);
                Newline();
                Printmatrix(newmatrix, nodes, nodes, "Multiplied Matrix");
            }


            Console.ReadKey();
        }
        private static void Newline()
        {
            Console.Write("\n");
        }
        private static void MultipliedMatrix(ref int[,] newmatrix, int[,] adjmatrix, int n)
        {
            for (int i = 0; i < adjmatrix.GetLength(0); i++)
            {
                for (int j = 0; j < adjmatrix.GetLength(1); j++)
                {
                    newmatrix[i, j] = adjmatrix[i, j] * n;
                }
            }
        }

        private static void Printmatrix(int[,] adjmatrix, string[] nodes_h, string[] nodes_v, string title)
        {
            if (adjmatrix.GetLength(0) != 0)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("{0}\n", title);
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.Write("\t");
                for (int i = 0; i < adjmatrix.GetLength(0); i++)
                    Console.Write("{0}\t", nodes_v[i]);
                for (int i = 0; i < adjmatrix.GetLength(0); i++)
                {
                    Newline();
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.Write("{0}\t", nodes_h[i]);
                    for (int j = 0; j < adjmatrix.GetLength(1); j++)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        if (adjmatrix[i, j] < 500) Console.Write("{0}\t", adjmatrix[i, j]);
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("-\t", adjmatrix[i, j]);
                        }
                    }
                    Console.ForegroundColor = ConsoleColor.White;
                }
                Newline();
            }
        }
    }
}*

*

2 个答案:

答案 0 :(得分:0)

你继续传递相同的矩阵作为MultipliedMatrix()的输入,所以它一直在做同样的事情。

要使其在新矩阵上运行,请在循环内调用Printmatrix()后立即添加以下行:

adjmatrix = newmatrix;

或者(更好的IMO)是改变MultipliedMatrix()以返回新矩阵,如下所示:

private static int[,] MultipliedMatrix(int[,] adjmatrix, int n)
{
    int[,] newmatrix = new int[adjmatrix.GetLength(0), adjmatrix.GetLength(1)];

    for (int i = 0; i < adjmatrix.GetLength(0); i++)
        for (int j = 0; j < adjmatrix.GetLength(1); j++)
            newmatrix[i, j] = adjmatrix[i, j] * n;

    return newmatrix;
}

然后,乘法循环中的代码如下所示:

    for (int i = 0; i < n; i++)
    {
        adjmatrix = MultipliedMatrix(adjmatrix, n);
        Newline();
        Printmatrix(adjmatrix, nodes, nodes, "Multiplied Matrix");
    }

请注意,这会丢失对原始adjmatrix的引用,这对您当前的代码来说不是问题。

但是,如果您需要保留原始参考,请按以下步骤操作:

var newMatrix = adjmatrix;

for (int i = 0; i < n; i++)
{
    newMatrix = MultipliedMatrix(newMatrix, n);
    Newline();
    Printmatrix(adjmatrix, nodes, nodes, "Multiplied Matrix");
}

答案 1 :(得分:0)

首先我会将乘法更改为函数而不是void方法调用,因此它看起来更好

这将是伪代码:

n = userinput;
i=0;
int[,] result;

do
  i+=1;
  result = MultiplyMatrix(result, n);
while i<n


int[,] MultiplyMatrix(adjmatrix, n){
   (multiply)  
   return resultMatrix };