Reputation: 301
using System; using System.Collections.Generic;
namespace MATRIX_algebra
{
public struct Struct_matrix
{
List<List<double>> entries;
public Struct_matrix(List<List<double>> values)
{
entries = values;
}
}
// public delegate void process_matrix(Struct_matrix matrix);
public class Matrix_init
{
public int size_C, size_R;
public void matrix_size()
{
Console.WriteLine("Enter the size of the matrix ");
Console.WriteLine("rows? ");
this.size_R = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("columns? ");
this.size_C = Convert.ToInt32(Console.ReadLine());
}
public List<List<double>> values = new List<List<double>>();
public void matrix_value()
{
for (int i = 0; i < this.size_R; i++)
{
Console.WriteLine("Enter the entries of the {0} row ",i+1);
for (int j = 0; j < this.size_C; j++)
{
values[i][j] = Convert.ToDouble(Console.ReadLine());
}
}
Struct_matrix matrix_init = new Struct_matrix(values);
}
}
}
namespace test
{
using MATRIX_algebra;
public class test_values
{
static void Main()
{
Matrix_init matrix1 = new Matrix_init();
for (int i = 0; i < matrix1.size_R; i++)
{
for (int j = 0; j < matrix1.size_C; j++)
{
Console.WriteLine(matrix1.values[i][j]);
}
}
}
}
}
I feel so dumb with this question, but I really need help since I'm just beginner
I don't know why when I run the program, it doesn't run through some parts of the code.
I debugged it, Main() -> instantiate Matrix_init ->public List> values = new List>(); -> end the program.
Upvotes: 1
Views: 94
Reputation: 101680
You need a constructor change public void matrix_size()
to:
public Matrix_init()
{
Console.WriteLine("Enter the size of the matrix ");
Console.WriteLine("rows? ");
this.size_R = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("columns? ");
this.size_C = Convert.ToInt32(Console.ReadLine());
}
And this program is running Just it doesn't work as expected.Because matrix_size()
method is never called so the size_R
and size_C
is never assigned to a value.They will have the default value for int which is zero, and that's why your program never enters the for
loop.Instead of adding constructor you can also simply call the matrix_size()
method.
Matrix_init matrix1 = new Matrix_init();
matrix1.matrix_size();
And then call the matrix_value()
method to get inputs from user and assign the values of your matrix array.Remember all methods needs to be call, they won't do anything if you don't call them.
Upvotes: 3