...

View Full Version : Arrays



wkid87
10-09-2007, 04:21 PM
Hello, I need help with three things. I'm during a matrix/multi-dimesional array that is a size 2 x 2. The user must enter the values after the user enter the values the program must add, subtract, multipy, and divide. I can not figure out how the user will enter the values and I can figure out the multiplication and divison. I tried doing a Console.Int32 but I was getting an error. I tried during a for statement just like I did with addition and I was not getting the right number. Any help will be appreciate. Thanks! Here is my code:

using System;

public class Matrix

{

public static void Main(string[] args)

{


int[,] matrix1 = { {1, 2 }, { 3, 4 } };

int[,] matrix2 = { { 2, 3 }, { 4, 5 } };

int[,] matrix3 = new int [2,2];

DisplayMatrix(matrix1);

Console.WriteLine();

DisplayMatrix(matrix2);

AddMatrix(matrix1, matrix2, matrix3);

SubtractMatrix(matrix1, matrix2, matrix3);

}

public static void DisplayMatrix(int[,] array)

{

for (int row=0; row < array.GetLength(0); row++)

{

for (int column = 0; column < array.GetLength(1); column++)

array=Convert.ToInt(Console.ReadLine());

Console.Write("{0}", array[ row, column]);

Console.WriteLine();


}

}

public static void AddMatrix(int[,] array1, int [,] array2, int[,] array3)

{

for(int row=0; row< array1.GetLength(0); row++)

{

for(int column=0; column <array1.GetLength( 1 ); column++)

array3[row, column] = array1 [row, column] + array2 [row, column];

}

for (int row = 0; row < array1.GetLength(0); row++)

{

for (int column = 0; column < array1.GetLength(1); column++)


Console.Write("{0}", array3[row, column]);

}

Console.WriteLine();

}

public static void SubtractMatrix(int[,] array1, int[,] array2, int[,] array3)

{

for (int row = 0; row < array1.GetLength(0); row++)

{

for (int column = 0; column < array1.GetLength(1); column++)

array3[row, column] = array1[row, column] - array2[row, column];

}

for (int row = 0; row < array1.GetLength(0); row++)

{

for (int column = 0; column < array1.GetLength(1); column++)

Console.Write("{0}", array3[row, column]);

}

Console.WriteLine();

}


}

nikkiH
10-09-2007, 07:17 PM
What is this, C#.NET? 1.1 or 2.0?

wkid87
10-09-2007, 11:21 PM
I'm using Microsoft Visual C# 2005 Express Edition.

nikkiH
10-10-2007, 08:36 PM
Okay, and what exactly is it you're trying to do?
Think it through and write pseudocode for what you're trying to do, in order to set out the logic. Then you write code from that.

It isn't clear from your post if you want the user to enter some number of values first and then calc the matrices, or ...?
Are they entering a bunch of numbers all at once, or a bunch of numbers that go into the first matrix, then numbers for the second, then numbers for the third.

Pseudocode helps you think it through and saves time.
Example of pseudocode:

for x=1 to 3 do
-- prompt user for 4 numbers
-- enter numbers into matrix x
done

or if you wanted this, it's different
for X=1 to 3 do
-- while (user enters a number)
-- -- enter number into matrix X
-- end while
done

Does that make sense?
Once you get it figured out "in english", so to speak, then you code it a lot easier.

wkid87
10-11-2007, 06:48 AM
Okay, and what exactly is it you're trying to do?
Think it through and write pseudocode for what you're trying to do, in order to set out the logic. Then you write code from that.

It isn't clear from your post if you want the user to enter some number of values first and then calc the matrices, or ...?
Are they entering a bunch of numbers all at once, or a bunch of numbers that go into the first matrix, then numbers for the second, then numbers for the third.

Pseudocode helps you think it through and saves time.
Example of pseudocode:

for x=1 to 3 do
-- prompt user for 4 numbers
-- enter numbers into matrix x
done

or if you wanted this, it's different
for X=1 to 3 do
-- while (user enters a number)
-- -- enter number into matrix X
-- end while
done

Does that make sense?
Once you get it figured out "in english", so to speak, then you code it a lot easier.

I got it figure out. Instead of displaying my results in each Method I need it to be display in the Answer Method. I need to pass the arguments to the Answer Method The next problem I having is the answer that it is displaying is not in a matrix but in a straight line. Here are the directions. I want to make sure I have this right:

Write a method that sets data size and the elements for each of the matrices.
Write a method that multipies the two matrices.
Write a method that dds the two matrices.
Write a method that subtract one matrix from the other.
Write a method nicely dsplay the matrices.

The main Method should test the class. The method names shall be selected to reflect what the method does.

Code: ( text )
.

using System;



public class Matrix
{

public static void Main(string[] args)
{

int[,] matrix1 = { { 1, 2 }, { 3, 4 } };

int[,] matrix2 = { { 2, 3 }, { 4, 5 } };

int[,] matrix3 = { { 0, 0 }, { 0, 0 } };

InputMatrix(matrix1);

Console.WriteLine();

InputMatrix(matrix2);

Console.WriteLine();

AddMatrix(matrix1, matrix2, matrix3);

SubtractMatrix(matrix1, matrix2, matrix3);

Multipy(matrix1, matrix2, matrix3);

}

public static void InputMatrix(int[,] array)
{

for (int row = 0; row < array.GetLength(0); row++)
{

for (int column = 0; column < array.GetLength(1); column++)
{
Console.WriteLine("Enter element {0}, {1}", row, column);
array[row, column] = Convert.ToInt32(Console.ReadLine());

}
Console.WriteLine();


}

}

public static void AddMatrix(int[,] array1, int[,] array2, int[,] array3)
{

for (int row = 0; row < array1.GetLength(0); row++)
{

for (int column = 0; column < array1.GetLength(1); column++)

array3[row, column] = array1[row, column] + array2[row, column];
Console.WriteLine();

}

for (int row = 0; row < array1.GetLength(0); row++)
{

for (int column = 0; column < array1.GetLength(1); column++)

Console.WriteLine();


}

}

public static void SubtractMatrix(int[,] array1, int[,] array2, int[,] array3)
{

for (int row = 0; row < array1.GetLength(0); row++)
{

for (int column = 0; column < array1.GetLength(1); column++)

array3[row, column] = array1[row, column] - array2[row, column];

}

for (int row = 0; row < array1.GetLength(0); row++)
{

for (int column = 0; column < array1.GetLength(1); column++)

Console.Write("{0}", array3[row, column]);

}

Console.WriteLine();

}
public static void Multipy(int[,] array1, int[,] array2, int[,] array3)
{
for (int row = 0; row < array1.GetLength(0); row++)
{
for (int column = 0; column < array2.GetLength(1); column++)
{
for (int a = 0; a < array1.GetLength(1); a++)
array3[row, column] += array1[row, a] * array2[a, column];
Console.Write("{0}", array3[row, column]);
}
Console.WriteLine();
}

}
public static void Answers(int[,] array3)
{




}


}

nikkiH
10-11-2007, 02:37 PM
Ok, I see this:

Write a method that sets data size and the elements for each of the matrices.
Write a method that multipies the two matrices.
Write a method that dds the two matrices.
Write a method that subtract one matrix from the other.
Write a method nicely dsplay the matrices.

That's 5 methods. (what is "dds"?)
You don't have 5 methods - you don't have your initialization in a method - it needs to be.

Also, you made everything static. You're likely supposed to have these all be instance methods and you instantiate the class and call the methods in main to test.

Your methods for the Matrix class should not write to console. Your matrix class should have a member(s) that the methods affect and a public property to get. Only the display would write to console (unless you're doing that to debug).

I'd have a Matrix class separate from the test class that had the main method and the input, personally, unless the requirements explicitly stated not to do that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum