【问题标题】:I need to use the matrix array in the multiplyMatrixByConstant method but im not sure how我需要在 multiplyMatrixByConstant 方法中使用矩阵数组,但我不确定如何
【发布时间】:2021-03-03 13:28:49
【问题描述】:

我收到multiplyMatrixByConstant(); 的错误消息,说“没有给出与所需形式参数相对应的参数”我不确定要包含哪些内容以允许我在 multiplyMatrixByConstant(); 方法中使用矩阵数组

    public class Program
{
    public static void Main()
    {
        Console.WriteLine("              ----------Welcome to the Matrix Program----------");
        Console.WriteLine("Please select one of the following options:");
        Console.WriteLine("1: The Random Matrix");
        Console.WriteLine("2: The Transpose Matrix");
        Console.WriteLine("3: Multiplying a Matrix by a Constant");
        Console.WriteLine("4: Multiplying Two Matrices");
        Console.Write("Your choice is: ");
        string choice = Console.ReadLine();
        if (choice == "1")
        {
            generateMatrix();
        }
        else if (choice == "2")
        {
            generateTranspose();
        }
        else if (choice == "3")
        {
            multiplyMatrixByConstant();
        }
    }

    static int[, ] generateMatrix()
    {
        Console.Write("Enter the number of columns: ");
        int c = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the number of rows: ");
        int r = Convert.ToInt32(Console.ReadLine());
        int[, ] matrix = new int[c, r];
        Random rnd = new Random();
        Console.WriteLine("The Matrix is: ");
        for (int i = 0; i < c; i++)
        {
            for (int x = 0; x < r; x++)
            {
                matrix[i, x] = rnd.Next(0, 10);
            }
        }

        for (int i = 0; i < c; i++)
        {
            for (int x = 0; x < r; x++)
            {
                Console.Write(matrix[i, x] + " ");
            }

            Console.WriteLine(" ");
        }

        return (matrix);
    }

    static void generateTranspose()
    {
        Console.Write("Enter the number of columns: ");
        int c = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the number of rows: ");
        int r = Convert.ToInt32(Console.ReadLine());
        int[, ] matrix = new int[c, r];
        Random rnd = new Random();
        for (int i = 0; i < c; i++)
        {
            for (int x = 0; x < r; x++)
            {
                matrix[i, x] = rnd.Next(0, 10);
            }
        }

        for (int i = 0; i < c; i++)
        {
            for (int x = 0; x < r; x++)
            {
                Console.Write(matrix[i, x] + " ");
            }

            Console.WriteLine(" ");
        }

        Console.WriteLine("The Transpose is:");
        int[, ] transpose = new int[r, c];
        for (int i = 0; i < c; i++)
        {
            for (int x = 0; x < r; x++)
            {
                transpose[x, i] = matrix[i, x];
            }
        }

        for (int i = 0; i < r; i++)
        {
            for (int x = 0; x < c; x++)
            {
                Console.Write(transpose[i, x] + " ");
            }

            Console.WriteLine(" ");
        }
    }

    static void multiplyMatrixByConstant(int[, ] matrix, int c, int r)
    {
        generateMatrix();
        Console.Write(" Enter a Constant to Multiply the Matrix by: ");
        int constant = Convert.ToInt32(Console.ReadLine());
        int[, ] result = new int[c, r];
        for (int i = 0; i < c; i++)
        {
            for (int x = 0; x < r; x++)
            {
                result[i, x] = matrix[i, x] * constant;
            }
        }

        for (int i = 0; i < c; i++)
        {
            for (int x = 0; x < r; x++)
            {
                Console.Write(result[i, x] + " ");
            }

            Console.WriteLine(" ");
        }
    }
}

【问题讨论】:

  • 请将您在完整堆栈跟踪中遇到的错误添加到问题中。

标签: c# arrays matrix multidimensional-array methods


【解决方案1】:

你需要传入参数,(int[, ] matrix, int c and int r)。

【讨论】:

  • 它说那些在当前上下文中不存在
  • 这些需要自己收入
猜你喜欢
  • 2017-02-21
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 2015-06-05
  • 2012-08-24
  • 1970-01-01
相关资源
最近更新 更多