c# - Filling in a matrix(2D array) using functions from a class -
i've learned c++ , taking c#. clarify, providing instructions if can help parts (b) , (c) think can rest.
this assignment: matrix 2 dimensional array of row size r , column size c. there specific rules of adding or subtracting matrices , b (of same row size m , column size n). product of matrix of dimension m n (m row size or row dimension, n column dimension) , matrix b of dimension p q meaningful if p = n , in case ab matrix of dimension m q. (a) define class matrix @ to the lowest degree private variables depending on row size, column size etc. (b) provide constructor when row size , column size specified. set arguments 0.0. (c) provide method set entries of matrix (d) provide add together method of matrices (e) provide subtract method. (f) provide scalar multiplication multiply elements of matrix value x. (g) provide multiplication method mul a.mul(b) equal ab if a’s column dimension c equal b’s row dimension. generate error message if product ab meaningless.
{ { private int r=10;//row size private int c=10;//column size int[,] array=new int [10,10]; public matrix() { (int = 0; < this.r; i++) { (int j = 0; j < this.c; j++) { } } } public matrix(int rowsize, int columnsize) { this.r = rowsize; this.c = columnsize; } public void setmatrix() { (int i=0; i<this.r; i++) { for(int j=0; j<this.c; j++) { } } } } }
without finite size, i'm not sure how proceed creating array (the 10,10 compiler stop complaining). secondly, when that's established, i'm not sure on how fill in array (most console). question doesn't sound much i'm asking homework me. xd
first - don't need loop initialize matrix. declaring int[r,c]
give 2d array filled zeros. description doesn't 0 argument constructor required, wouldn't provide one, it's not clear how should behave anyway. create 1 constructor this:
public matrix(int rows, int cols) { r = rows; c = cols; array = new int[r,c]; // note: filled zeros! }
for setting entries can provide method this:
public void setcell(int row, int col, int val) { array[row,col] = val; }
or using indexer:
public int this[int row, int col] { { homecoming array[row,col]; } set { array[row,col] = value; } // didn't inquire setter, assume you'll need 1 }
if can help parts (b) , (c) think can rest.
ok, luck then
edit:
so clarify, when declare array of int
filled zeros. in contrast c++ random block of memory filled random junk , need set zero. utilize of unassigned variables 1 of "problems" c++ c# aimed fix. example, if run code:
int[] arr = new int[10]; console.writeline(string.join(",",arr));
it write 0,0,0,0,0,0,0,0,0,0
every single time.
for more info read here. in particular, part:
note if not initialize array @ time of declaration, array members automatically initialized default initial value array type. also, if declare array field of type, set default value null when instantiate type.
but note, if this:
int a; console.writeline(a);
you compile time error complaining usage of unassigned variable.
c# matrix multidimensional-array
No comments:
Post a Comment