0

I'm new to coding and just doing it as a hobby and I'm working through some CodeAbbey problems to practice.

I'm trying to take groups of numbers separated by spaces and new lines and populate a multidimensional array so I can perform some math on these numbers. I haven't got to the math part yet and am still trying to get the data into the array.

Sample of the dataset:

5 (this is the number of data sets)

(below are the actual pieces of data)

7899743 906

6574065 -1243290

5441 1320

9965047 86

4781 1934

My code is as follows:

namespace Rounding
{
    class Program
    {
        static void Main(string[] args)
        {
            // declare variables
            // n asks user for number of array columns
            // raw takes the numbers seperated by new lines
            int n = int.Parse(Console.ReadLine());
            string[] raw = Console.ReadLine().Split('\n');
            int[,] numbers = new int [n,2];

            // loop through raw array and split the numbers and add them to multidimentional array
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    numbers [i, j] = Convert.ToInt32(raw[n].Split(' '));
                }

            }

            // display data from multidimentional array (for testing)
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine(numbers[i,j]);
                }
            }
        }
    }
}

The program should wait for the user to input the number of data sets then wait again for the actual data and then use that data to populate the array.

The debug message I get is on numbers [i, j] = Convert.ToInt32(raw[n].Split(' ')); It says

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

Any guidance on what I am doing wrong would be appreciated.

3
  • It will depend what values you type in the console when you run it, for example type 0 and then 8 and it will work, type 1 and then 8 and it won't (as arrays are zero based, it's now looking for the second value and there isn't one). Commented Jun 17, 2020 at 9:09
  • You want to populate your array of n rows and 2 columns with values inputted in array raw. But what if the user has inputted less than n x 2 numbers - you'll obviously get an index out of range exception. Commented Jun 17, 2020 at 9:15
  • I don't need to assume a user will input the wrong amount in this scenario. I just want to get the code to work but am having trouble and can't figure out what I'm doing wrong. If I play around with the numbers, like put 1 and then 8 8 I get the following error: System.InvalidCastException: 'Unable to cast object of type 'System.String[]' to type 'System.IConvertible'.' Not sure what that is Commented Jun 17, 2020 at 9:19

2 Answers 2

1

Try this :

// declare variables
    // n asks user for number of array columns
    // raw takes the numbers seperated by new lines
    int n = int.Parse(Console.ReadLine());
    string[] raw = Console.ReadLine().Split(' ');
    double[,] numbers = new double[n, 2];


     if (raw.Count() != n * 2)
     {
        Console.WriteLine("Invalid values");
        return;
     }


    int index = 0;

    // loop through raw array and split the numbers and add them to multidimentional array
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            numbers[i, j] = Convert.ToInt32(raw[index++]);
        }

    }

    // display data from multidimentional array (for testing)
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            Console.WriteLine(numbers[i, j]);
        }
    }

This gives output 7899743 906 6574065 -1243290 5441 1320 9965047 86 4781 1934

when entering 5 and those values.

Sign up to request clarification or add additional context in comments.

Comments

0

This could be solved nicely with a Linq pipeline:

var str = @"7899743 906

            6574065 -1243290

            5441 1320

            9965047 86

            4781 1934";

str.Split('\n')
.Where(s => string.IsNullOrWhiteSpace(s) == false)
.Select(s => s.Split(' '))
.Select(s => new int[] { int.Parse(s[0]), int.Parse(s[1]) })
.ToList() // just so we can use the ForEach method of List<T>
.ForEach(s => Console.WriteLine($"{s[0]} * {s[1]} = {s[0] * s[1]}"));

This would give the following output:

7899743 * 906 = -1432767434
6574065 * -1243290 = -146509562
5441 * 1320 = 7182120
9965047 * 86 = 856994042
4781 * 1934 = 9246454

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.