S
S
samsungovetch2018-07-09 08:50:55
C++ / C#
samsungovetch, 2018-07-09 08:50:55

C# - How to fix the error - Index was outside the bounds of the array?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Введите порядок матрицы");
        int N = Int32.Parse(Console.ReadLine());
        int M = Int32.Parse(Console.ReadLine());
        Console.WriteLine();
        int[,] a = new int[N, M];
        int[] b = new int[N];
        int max;
        Random rnd = new Random();

        max = 0;
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                a[i, j] = rnd.Next(0, 10);
                Console.Write(a[i, j] + "\t");
            }

            Console.WriteLine();
        }

        Console.WriteLine();


        for (int i = 0; i < N; i++)
        {
            for (int j = 0; i < M; j++)
            {
                if (a[i, j] > max)
                {
                    max = a[i, j];
                }
            }
            b[i] = max;
            Console.Write(b[i] + "\t");
        }
        Console.WriteLine();
    }
}

The error occurs at the line if (a[i, j] > max)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2018-07-09
@samsungovetch

most likely the reason is on the screen
pk91U3j.png

D
Dmitry Bashinsky, 2018-07-10
@BashkaMen

I advise you to use x, y, .... for multidimensional arrays and not to use similar letters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question