I am trying to create a program that will create an array where the user will enter in the number of rows and columns and then the code will populate the array with random numbers between 0 and 100. Visual Studio shows no errors in the code, but the app crashes when I enter in the value for the columns and the app displays: "Unhandled Exception: System.RankException: Only single dimension arrays are supported here."
I'm not sure where I'm going wrong, but if anyone could point me in the correct direction, I would appreciate it. Thank you.
using System;
using System.Collections;
namespace RandomNumberApp
{
class RandomNumberApp
{
static void Main()
{
//variables
int row = 0,
column = 0,
largestRandom = 0,
maxX = 0,
maxY = 0;
//method calls
row = GetRow(row);
column = GetColumn(column);
int[,] randomArray = new int[row, column];
FillArray(row, column, randomArray);
largestRandom = GetLargestNumber(ref randomArray, row, column, out maxX, out maxY);
DisplayResults(randomArray, largestRandom, maxX, maxY);
//determine whether user wants to run the program again
Console.WriteLine("Do you want to create another array?");
Console.WriteLine("Press 'Y if yes; 'N' if no");
char userResponse;
userResponse = Convert.ToChar(Console.ReadLine());
if (userResponse == 'Y' || userResponse == 'y')
Main();
}
//method to ask the user to enter the row size for the array
static int GetRow(int row)
{
//variables
string rawRow;
Console.Write("Please enter the number of rows for your array: ");
rawRow = Console.ReadLine();
row = Convert.ToInt32(rawRow);
return row;
}
//method to ask the user to enter the column size for the array
static int GetColumn(int column)
{
//variables
string rawColumn;
Console.WriteLine("\nPlease enter the number of columns for your array: ");
rawColumn = Console.ReadLine();
column = Convert.ToInt32(rawColumn);
return column;
}
//method to fill the array with random numbers
static int[,] FillArray(int row, int column, int[,] randomArray)
{
//creates a random variable to fill the array
Random randFill = new Random();
//loop to fill the array with random numbers
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
randomArray[i, j] = randFill.Next(0, 100);
}
}
return randomArray;
}
//method to find the largest number
static int GetLargestNumber(ref int[,] randomArray, int row, int column, out int maxX, out int maxY)
{
int max = int.MinValue;
maxX = -1;
maxY = -1;
for (int x = 0; x < column; x++)
{
for (int y = 0; y < row; y++)
{
if (randomArray[x, y] > max)
{
max = randomArray[x, y];
maxX = x;
maxY = y;
}
}
}
return max;
}
//method to display the results
static void DisplayResults(int[,] randomArray, int largestRandom, int maxX, int maxY)
{
//display the array elements in a list
for (int i = 0; i < randomArray.GetLength(0); i++)
for (int j = 0; j < randomArray.GetLength(1); j++)
Console.WriteLine("{0,-3}", randomArray[i, j]);
Console.WriteLine("\nLargest Number In Array: " + largestRandom);
Console.WriteLine(String.Format("\nIndex Of Largest Number:\nX: {0}\nY: {1}\n ", maxX, maxY));
}
}
}
int[,] randomArray = new int[row, column];whenrowandcolumnare both set to0, so it has a total of 0 elements. Second, it'll be useful to point out at which line the error occurs. In any case, I suggest usingint[][]instead ofint[,]whenever possible.Array.IndexOfworks on single dimensional arrays - you have a two dimensional array, i.e. one defined as an exact number of rows and column, not a jagged array, which is an "array of arrays", where each array can be of a different length. Jagged arrays are declared in the form[][]not[,]