.NET 4.x Ошибка "IndexOutOfRangeException" при заполнении двумерного массива - C#
Формулировка задачи:
Программа кампилируеться, но при заполнении двумерного массива на строке "x[i,j]=(3*(i+4)*(j+1))/(2*(i+1)+5*(j+2))-j;" и "y[i,j]=i+(4*(i+7)-j)/(i*j+2);" выдаёт "System.IndexOutOfRangeException: Index was outside the bounds of the array. Помогите с решением, пожалуйста. Заранее спасибо
"
using System;
namespace OOP2
{
class Program
{
public static void Main(string[] args)
{
int n=0, m=0, i, j;
Console.Write("Введите значение n");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Введите значение m");
m = Convert.ToInt32(Console.ReadLine());
int [,] x = new int [m, n];
int [,] y = new int [m, n];
for (i=0; i<=m; i++){
for (j=0; j<=n; j++){
x[i,j]=(3*(i+4)*(j+1))/(2*(i+1)+5*(j+2))-j;
y[i,j]=i+(4*(i+7)-j)/(i*j+2);
}
}
for (i=0; i<=m; i++){
for (j=0; j<=n; j++){
Console.WriteLine(x[i,j].ToString()+" ",y[i,j].ToString()+" ");
Console.WriteLine();
}
}
// TODO: Implement Functionality Here
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}Решение задачи: «.NET 4.x Ошибка "IndexOutOfRangeException" при заполнении двумерного массива»
textual
Листинг программы
using System;
namespace OOP2
{
class Program
{
public static void Main(string[] args)
{
int n=0, m=0, i, j;
Console.Write("Введите значение n ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Введите значение m ");
m = Convert.ToInt32(Console.ReadLine());
int [,] x = new int [m, n];
int [,] y = new int [m, n];
x[i,j]=f1(x,i,j);
y[i,j]=f2(y,i,j);
Console.WriteLine("Массив Х");
for (i=0; i<m; i++){
for (j=0; j<n; j++){
Console.Write(x[i,j].ToString()+" ");
}
Console.WriteLine();
}
Console.WriteLine("Массив Y");
for (i=0; i<m; i++){
for (j=0; j<n; j++){
Console.Write(y[i,j].ToString()+" ");
}
Console.WriteLine();
}
// TODO: Implement Functionality Here
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
public static Int32 F1(int[,] x)
{
int n=0, m=0;
Console.Write("Введите значение n ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Введите значение m ");
m = Convert.ToInt32(Console.ReadLine());
for (int i=0; i<m; i++){
for (int j=0; j<n; j++){
x[i,j]=(3*(i+4)*(j+1))/(2*(i+1)+5*(j+2))-j;
return x[i,j];
}
}
}
public static Int32 F2(int[,] y)
{
int n=0, m=0;
Console.Write("Введите значение n ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Введите значение m ");
m = Convert.ToInt32(Console.ReadLine());
for (int i=0; i<m; i++){
for (int j=0; j<n; j++){
y[i,j]=i+(4*(i+7)-j)/(i*j+2);
return y[i,j];
}
}
}
}
}