1

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.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
Hung Nguyen
  • 301
  • 1
  • 2
  • 11
  • 3
    In the future please come up with a better title; we shouldn't have to open the question to understand the title. – Dour High Arch Apr 12 '14 at 02:47
  • 3
    It doesn't run through because you are not calling it. Having methods is not enough, you need to call them manually. I do not see any call to matrix_size or matrix_value anywhere, you're only creating the class. – Pierre-Luc Pineault Apr 12 '14 at 02:48
  • 1
    Side note: unless you understand what you are doing *don't use* `struct`. Please check questions about "struct vs. class" like http://stackoverflow.com/questions/1951186/which-is-best-for-data-store-struct-classes. – Alexei Levenkov Apr 12 '14 at 02:51
  • 1
    @DourHighArch it does garner interest though to be fair – ScottMcGready Apr 12 '14 at 02:56

1 Answers1

3

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.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184