C# init arrays | Newbie | | Join Date: Oct 2007
Posts: 13
| | |
I''m working on a project that will add,multipy,subtract and divide a matrix. Each one is suppose to be display in a method. I can figure out the addition and subtraction but not the multipication and divison. I also needs help with user input. The user suppose to enter the values in the Display Method, but I'm having trouble with the Console.Intt32. My code is below.
[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();
}
}
{/CODE}
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: C# init arrays
Hi wkid87
Please put your code withing [code] tags for readability.
I dont think that Console does not have a Intt32 method or property.
There is site which i came across doing a search, especially since matrix operations have been around for a very long time This site provides the code to use
cheers
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays Quote:
Originally Posted by Shashi Sadasivan Hi wkid87
Please put your code withing [code] tags for readability.
I dont think that Console does not have a Intt32 method or property.
There is site which i came across doing a search, especially since matrix operations have been around for a very long time This site provides the code to use
cheers Thanks for the link, but it did not help any. Didn't see the code.
Do you know how to get values from the user?
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: C# init arrays Quote:
Originally Posted by wkid87 Thanks for the link, but it did not help any. Didn't see the code.
Do you know how to get values from the user? well the code is to be donloaded from the site.
I think it will be as a library, with all the functions defined in it, all you would need is to reference the library and use the methods in it. The algorithm should be implemented within that.
How does your application run?
Windows app, Console based, or web based?
cheers
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays
I use Visual Basic 2005.
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: C# init arrays Quote:
Originally Posted by wkid87 I use Visual Basic 2005. I would guess it's a console application in VB.NET
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays
Its Microsoft Visual C# 2005
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: C# init arrays Quote:
Originally Posted by wkid87 Its Microsoft Visual C# 2005 You *JUST* said you were using Visual Basic.
Which is it?
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays
Its Microsoft Visual C# 2005, sorry. Was on my laptop earlier and was confuse.
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: C# init arrays Quote:
Originally Posted by wkid87 Its Microsoft Visual C# 2005, sorry. Was on my laptop earlier and was confuse. Ok...VB to C#
So....is it a web application? a windows application or a console one?
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays Quote:
Originally Posted by Shashi Sadasivan Ok...VB to C#
So....is it a web application? a windows application or a console one? It is a Console application
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays
I trying to figure out the multipication. I know I probably need three for statements but I'm still stuck.
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: C# init arrays
You might either want to post some code for anyone to help you on that. I tried my hands on the link that i had previously provided. Worked fine and i didnt have to do any nested forloops...(all done by that library :))
Have you figured out user input for you console app?
cheers
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays Quote:
Originally Posted by Shashi Sadasivan You might either want to post some code for anyone to help you on that. I tried my hands on the link that i had previously provided. Worked fine and i didnt have to do any nested forloops...(all done by that library :))
Have you figured out user input for you console app?
cheers Is the a way that you can post the code on here. I retried to open the code but saying something it can't convert.
No I have not figure that out. I tried to do a ConvertToInt32 and keep getting errors.
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: C# init arrays
Is that on user input?
could you post the code you are actually using rather than giving 1/2 of the snippet?
cheers
I have attached the project below
That code was made in 1.1 due to which vs2005 will attempt to convert it.
cheers
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays
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. - .
-
-
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)
-
{
-
-
-
-
-
}
-
-
-
}
-
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: C# init arrays
I can see quite a few runtime errors (practically in each method that you have created, minor ones, but will affect runtime critically)
Things like assigning the matrix (dosent return anything, addidion subtraction, multiplication dosent really hlp unless you are printing the result from that method itself)
Anyhows, what are the issues you are facing? I wanst able to get your problem
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays
Instead of displaying the results in the each method. I want the result only display in the Answer method. I want to return the Add, Subtract, and Multipy Methods so I can get the reults to display in the Answer method. How will I do that. -
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];
-
-
}
-
-
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();
-
-
}
-
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();
-
}
-
Console.WriteLine();
-
}
-
-
}
-
public static void Answers(int[,] array3)
-
{
-
-
-
-
-
}
-
-
-
}
-
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: C# init arrays - using System;
-
using System.Collections.Generic;
-
using System.Text;
-
-
namespace ConsoleApplication4
-
{
-
public static class Matrix
-
{
-
-
public static void theMatrix()
-
{
-
-
int[,] matrix1 = { { 1, 2 }, { 3, 4 } };
-
-
int[,] matrix2 = { { 2, 3 }, { 4, 5 } };
-
-
int[,] matrix3 = { { 0, 0 }, { 0, 0 } };
-
-
InputMatrix(ref matrix1);
-
-
Console.WriteLine();
-
-
InputMatrix(ref matrix2);
-
-
Console.WriteLine();
-
-
AddMatrix(matrix1, matrix2, ref matrix3);
-
-
SubtractMatrix(matrix1, matrix2, ref matrix3);
-
-
Multipy(matrix1, matrix2, ref matrix3);
-
-
}
-
-
public static void InputMatrix(ref 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,ref 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];
-
}
-
Answers(array3,"Addition");
-
}
-
-
public static void SubtractMatrix(int[,] array1, int[,] array2, ref 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];
-
}
-
Answers(array3,"Subtraction");
-
}
-
-
public static void Multipy(int[,] array1, int[,] array2, ref 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];
-
}
-
}
-
Answers(array3,"Multiplication");
-
}
-
public static void Answers(int[,] array3, string answerType)
-
{
-
Console.WriteLine("Answer({0}):",answerType);
-
for (int row = 0; row < array3.GetLength(0); row++)
-
{
-
for (int column = 0; column < array3.GetLength(1); column++)
-
{
-
Console.Write("{0}", array3[row, column]);
-
}
-
Console.WriteLine();
-
}
-
}
-
-
-
}
-
-
}
Hi,
I have modified your code a bit so as to make it a complete static class. Please make the necessary changees to revert it back to your (static class, Main has been replaced to theMatrix)
I have put the ref tags, generally we could have done without it. especially for array3.
Input can be modified to a return type.
cheers
| | Newbie | | Join Date: Oct 2007
Posts: 13
| | | re: C# init arrays
Thanks. One more thing. Is there a way I can space the numbers out instead of being so jam.
|  | Moderator | | Join Date: Aug 2007 Location: Brisbane, Australia
Posts: 1,414
| | | re: C# init arrays Quote:
Originally Posted by wkid87 Thanks. One more thing. Is there a way I can space the numbers out instead of being so jam. hello again,
Put a space :).... you can do a lot of things.
if you want you can even make a table.
But you have to experiment it yourself
happy coding
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|