מערכים ב #C

igalep132

New member
מערכים ב #C

שלום , שאלה על מערך דו מיידמי ב #C כשאני משתמש ב ()GetLength. מה אני אמור לרשום בתוך הסוגרים ? הבנתי שזה אמור להיות המימד, אבל איך בדיוק מתחלקת החלוקה למידמים ? (נניח במטריצה 3*3) תודה
 

igalep132

New member
ועוד שאלה על קוד שלא עובד טוב

using System; namespace lab3 { /// <summary> /// Demonstrates the use of multi-dimensional arrays. /// </summary> /// <remarks> /// Used in Lab-3 task # 2. /// </remarks> /// public class MatrixMultiply { /// <summary> /// Multiplies two matrixes of size [2,2]. /// </summary> /// <param name="matrix1"></param> /// <param name="matrix2">The 2nd matrix to multiply.</param> /// <returns>The resulting [2,2] matrix of the multiplication.</returns> private static int[,] MultiplyMatrixes(int[,] matrix1, int[,] matrix2) { int[,] result = new int[2, 2]; for ( int rows = 0; rows < result.GetLength(0); rows++ ) for ( int cols = 0; cols < result.GetLength(1); cols++ ) { result[rows,cols] += matrix1[rows,0] * matrix2[0,cols] + matrix1[rows,1] * matrix2[1,cols]; } return result; } /// <summary> /// Populates a matrix of any dimensions from user input. /// </summary> /// <param name="matrix">The matrix to populate.</param> private static void InputMatrix(int[,] matrix) { Console.WriteLine(" PLS insert the metrix "); for (int rows = 0; rows < matrix.GetLength(0); rows++) { for (int colmn = 0; colmn < matrix.GetLength(1); colmn++) { Console.WriteLine(" The current cell {0},{1} ", rows, colmn, " will be :"); matrix[rows, colmn] = Console.Read(); } Console.WriteLine(); } } /// <summary> /// Writes a matrix of any dimensions to the console. /// </summary> /// <param name="matrix">The matrix to write.</param> private static void OutputMatrix(int[,] matrix) { for (int rows = 0; rows < matrix.GetLength(0); rows++) { for (int cols = 0; cols < matrix.GetLength(1); cols++) { Console.Write("{0} ", matrix[rows, cols]); } Console.WriteLine(); } Console.ReadLine(); } /// <summary> /// Multiplies two matrixes of 2,2 size and prints the result. /// </summary> public static void Main() { int[,] matrix1 = new int[2, 2]; int[,] matrix2 = new int[2, 2]; // printing the 1st matrix InputMatrix(matrix1); Console.WriteLine("The input of the 1st matrix is:"); OutputMatrix(matrix1); InputMatrix(matrix2); // printing the 2nd matrix Console.WriteLine("The input of the 2nd matrix is:"); OutputMatrix(matrix2); int[,] resultMultiplying = new int[2, 2]; resultMultiplying = MultiplyMatrixes(matrix1, matrix2); Console.WriteLine("The result of multiplying matrix1 by matrix2 is: "); Output​
 

igalep132

New member
הבעיה היא

משום מה, כשאני קולט את המטריצות , הערכים שמופיעים בתכלס במטריצה, הם לא אלו שהכנסתי , וגם אני יכול להכניס ערכים בכל התחלה של שורה חדשה, אבל כשעוברים עמודות, משום מה יש ערכים רנדומאלים.... תודה על העזרה
 
שלום ידידי היקר.

בgetlength הפרמטר זה המימד שבו אתה רוצה את האורך, אני לא זוכר בדיוק , אבל אם אני לא טועה, 0 זה כמות עמודות , 1 זה כמו שורות .. 2 זה המימד השלישי ,וכ"ד ובקשר לconsole read אני חושב שהוא יחזיר לך char או string שצריך לעשות לו המרה בעזרה תconvert או משהו כזה. אחרת המספר יהיה הערך אסקי.
 

igalep132

New member
חתימה של Console.read

החתימה של Console.read (ככל הידוע לי) היא int (כך לפחות רשום בMSDN) מישהו יודע משהו אחר
 
למעלה