TheRealDax
TheRealDax

Reputation: 5

Trying to take some user input and populate multidimensional array

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.

Upvotes: 0

Views: 87

Answers (2)

Yosef Bernal
Yosef Bernal

Reputation: 1074

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

Upvotes: 0

auburg
auburg

Reputation: 1475

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.

Upvotes: 1

Related Questions