Reputation: 55
I have a Console Application in C# that allows a user to input 5 days of the week and then 5 numerical values corresponding with the days in a single 2D array.
The array is in string format so I tried to Parse the second half of the string data and produce a sum of them but it says my index is out of bounds, any ideas?
Code posted below:
//Ask the user to enter five days of the week and rainfall data for each day
double rainsum = 0;
Console.WriteLine("Please enter 5 days of the week.");
//Store the data in a two dimensional string array named rainfallData[]
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
rainfallData[i, j] = Console.ReadLine();
}
if (i == 0)
{
Console.WriteLine("Please enter the corresponding rain data.");
}
}
Console.WriteLine("Data placed in raindallData[] array.");
for (int i = 0; i < 2; i++)
{
Console.WriteLine();
for (int j = 0; j < 5; j++)
{
Console.WriteLine("rainfallData({0},{1})={2}", i, j, rainfallData[i, j]);
}
}
//Use iteration to calculate the following from the values in rainfallData[]:
//a) sum
Console.Write("Data values calculated using iteration. \n a) Sum of rainfallData[] = ");
for (int i = 1; i < rainfallData.Length; i++)
{
Console.WriteLine();
for (int j = 0; j < 5; j++)
{
rainsum += double.Parse(rainfallData[i, j]);
}
}
Console.WriteLine(rainsum);
//End Program
Console.ReadKey(true);
Upvotes: 0
Views: 80
Reputation: 117124
Your problem is that rainfallData.Length
refers to the number of items in the 2d array - in your case this is 2 x 5 = 10
, but you were putting that index in the first dimension and causing the out of bound error.
If I were you I would have started my code like this:
int columns = 2;
int days = 5;
string [,] rainfallData = new string[columns, days];
Now each and every time that refer to the bounds of each dimension use columns
and days
respectively.
The you'd write your code this way:
//Ask the user to enter five days of the week and rainfall data for each day
Console.WriteLine("Please enter 5 days of the week.");
//Store the data in a two dimensional string array named rainfallData[]
for (int j = 0; j < days; j++)
{
rainfallData[0, j] = Console.ReadLine();
}
Console.WriteLine("Please enter the corresponding rain data.");
for (int j = 0; j < days; j++)
{
rainfallData[1, j] = Console.ReadLine();
}
Console.WriteLine("Data placed in raindallData[] array.");
for (int i = 0; i < columns; i++)
{
Console.WriteLine();
for (int j = 0; j < days; j++)
{
Console.WriteLine("rainfallData({0},{1})={2}", i, j, rainfallData[i, j]);
}
}
//Use iteration to calculate the following from the values in rainfallData[]:
//a) sum
double rainsum = 0.0;
Console.Write("Data values calculated using iteration. \n a) Sum of rainfallData[] = ");
for (int i = 0; i < columns; i++)
{
Console.WriteLine();
for (int j = 0; j < days; j++)
{
rainsum += double.Parse(rainfallData[i, j]);
}
}
Console.WriteLine(rainsum);
Just for your edification here's how I might have really tackled this:
int days = 5;
Console.WriteLine("Please enter 5 days of the week.");
string[] first = Enumerable.Range(0, days).Select(x => Console.ReadLine()).ToArray();
Console.WriteLine("Please enter the corresponding rain data.");
string[] second = Enumerable.Range(0, days).Select(x => Console.ReadLine()).ToArray();
string[][] rainfallData = new[] { first, second };
Console.WriteLine("Data placed in raindallData[][] jaggard array.");
Console.WriteLine(String.Join("", Enumerable.Range(0, days).Select(x => $"rainfallData[0][{1}]={rainfallData[0][x]}\n")));
Console.WriteLine(String.Join(Environment.NewLine, Enumerable.Range(0, days).Select(x => $"rainfallData[1][{1}]={rainfallData[1][x]}")));
double rainsum = rainfallData.Sum(i => i.Sum(j => double.Parse(j)));
Console.WriteLine($"Data values calculated using iteration. \n a) Sum of rainfallData[][] = {rainsum}");
Here's an even more robust way:
double[] readNumbers(int items, string title)
{
Console.WriteLine($"Please enter {items} of {title} data.");
int attempt = 0;
return
Enumerable
.Range(0, items)
.Select(x =>
{
double result;
while (!double.TryParse(Console.ReadLine(), out result))
{
Console.WriteLine($"Input not a number. Please try again.");
}
return result;
})
.ToArray();
}
int days = 5;
double[] first = readNumbers(days, "first columnn");
double[] second = readNumbers(days, "rain data");
double[][] rainfallData = new[] { first, second };
Console.WriteLine("Data placed in raindallData[][] jaggard array.");
Console.WriteLine(String.Join("", Enumerable.Range(0, days).Select(x => $"rainfallData[0][{1}]={rainfallData[0][x]}\n")));
Console.WriteLine(String.Join(Environment.NewLine, Enumerable.Range(0, days).Select(x => $"rainfallData[1][{1}]={rainfallData[1][x]}")));
double rainsum = rainfallData.Sum(i => i.Sum());
Console.WriteLine($"Data values calculated using iteration. \n a) Sum of rainfallData[][] = {rainsum}");
Upvotes: 1