1

I'm trying to create a row in a matrix.
For example, I want the user to enter the row length then for the program to create that row length in the matrix.

I've been doing:

int i, j;
int[,] arr1 = new int[1, 1];

However, I don't know what to do for new int[1, 1] because if new int is [column, row] how do I make it so the user's input is stored in there?

I hope that makes sense. I'm quite new to c# and I'm still trying to understand everything. Any help is greatly appreciated. Please let me know if I've been too vague.

Oram
  • 1,589
  • 2
  • 16
  • 22
Samuel
  • 11
  • 2
  • 2
    Type in google C# arrays tutorial? – Leszek P Mar 13 '19 at 11:28
  • Use `Console.ReadLine` to get an input from the user, parse it with `int.TryParse`, store the parsed `int`s into variables and _then_ instatiate the array with the stored `int`s – Magnetron Mar 13 '19 at 11:32

2 Answers2

1

If I had to do this project I would create a class for the matrix where you assign the column count of your matrix on creation and add a List of int values to create a new row for your matrix

public class Matrix
{
    int Width;
    List<int[]> dataset = new List<int[]>();

    public Matrix(int ColumnCount)
    {
        Width = ColumnCount;
    }

    public void addrow(int[] row)
    {
        //intelligence here to make sure the row length is correct
        dataset.Add(row);
    }
}

Then you use the class to create your matrix.
You will obviously then need to create the retrieval methods of your data

Neil
  • 641
  • 1
  • 7
  • 21
0

People in the comments have suggested how to do it with arrays, but, considering you're new to C#, I strongly suggest you start using List, instead of arrays, since working with lists is much better. Here's why

With List, you do not need to bother whether or not the memory was initially allocated or not. You just Add to the list, and it "expands" as it needs.

An example:

var list = new List<int>();

list.Add(1);
list.Add(2);

Remember to add using System.Collections.Generic; to the top of your class file.

fhcimolin
  • 616
  • 1
  • 8
  • 27
  • A `List` has only one dimension, the closest thing to a matrix using Lists would be a `List>`, but all rows in a matrix must have the same number of columns, and using List you would have to provide extra logic. For matrixes, OP's aproach is better. – Magnetron Mar 13 '19 at 11:37
  • @Magnetron I totally forgot that it's a matrix. Whoops. What about `Dictionary` then? – fhcimolin Mar 13 '19 at 11:39
  • Even worse in my opinion. I think you meant to store the coordinates in the key? You would have the same issue of the list of lists, where you cannot guarantee that the number of columns and rows are constant – Magnetron Mar 13 '19 at 11:43
  • @Magnetron You win. But since OP is new to the language, it's still a good learn for him. Depending on what he'll use the array for, sometimes it's better to use a `List` of say, objects, in case he wants a table, so on. – fhcimolin Mar 13 '19 at 11:47