Connecting Tech Pros Worldwide Forums | Help | Site Map

C# init arrays

Newbie
 
Join Date: Oct 2007
Posts: 13
#1: Oct 9 '07
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}

Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#2: Oct 9 '07

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
#3: Oct 9 '07

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?
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#4: Oct 9 '07

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
#5: Oct 9 '07

re: C# init arrays


I use Visual Basic 2005.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: Oct 9 '07

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
#7: Oct 9 '07

re: C# init arrays


Its Microsoft Visual C# 2005
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#8: Oct 9 '07

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
#9: Oct 9 '07

re: C# init arrays


Its Microsoft Visual C# 2005, sorry. Was on my laptop earlier and was confuse.
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#10: Oct 9 '07

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
#11: Oct 10 '07

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
#12: Oct 10 '07

re: C# init arrays


I trying to figure out the multipication. I know I probably need three for statements but I'm still stuck.
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#13: Oct 10 '07

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
#14: Oct 10 '07

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.
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#15: Oct 10 '07

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
Attached Files
File Type: zip ConsoleApplication5.zip (61.6 KB, 19 views)
Newbie
 
Join Date: Oct 2007
Posts: 13
#16: Oct 11 '07

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.
Expand|Select|Wrap|Line Numbers
  1. .
  2.  
  3. using System;
  4.  
  5.  
  6.  
  7. public class Matrix
  8. {
  9.  
  10.     public static void Main(string[] args)
  11.     {
  12.  
  13.         int[,] matrix1 = { { 1, 2 }, { 3, 4 } };
  14.  
  15.         int[,] matrix2 = { { 2, 3 }, { 4, 5 } };
  16.  
  17.         int[,] matrix3 = { { 0, 0 }, { 0, 0 } };
  18.  
  19.         InputMatrix(matrix1);
  20.  
  21.         Console.WriteLine();
  22.  
  23.         InputMatrix(matrix2);
  24.  
  25.         Console.WriteLine();
  26.  
  27.         AddMatrix(matrix1, matrix2, matrix3);
  28.  
  29.         SubtractMatrix(matrix1, matrix2, matrix3);
  30.  
  31.         Multipy(matrix1, matrix2, matrix3);
  32.  
  33.     }
  34.  
  35.     public static void InputMatrix(int[,] array)
  36.     {
  37.  
  38.         for (int row = 0; row < array.GetLength(0); row++)
  39.         {
  40.  
  41.             for (int column = 0; column < array.GetLength(1); column++)
  42.             {
  43.                 Console.WriteLine("Enter element {0}, {1}", row, column);
  44.                 array[row, column] = Convert.ToInt32(Console.ReadLine());
  45.  
  46.             }
  47.             Console.WriteLine();
  48.  
  49.  
  50.         }
  51.  
  52.     }
  53.  
  54.     public static void AddMatrix(int[,] array1, int[,] array2, int[,] array3)
  55.     {
  56.  
  57.         for (int row = 0; row < array1.GetLength(0); row++)
  58.         {
  59.  
  60.             for (int column = 0; column < array1.GetLength(1); column++)
  61.  
  62.                 array3[row, column] = array1[row, column] + array2[row, column];
  63.             Console.WriteLine();
  64.  
  65.         }
  66.  
  67.         for (int row = 0; row < array1.GetLength(0); row++)
  68.         {
  69.  
  70.             for (int column = 0; column < array1.GetLength(1); column++)
  71.  
  72.                 Console.WriteLine();
  73.  
  74.  
  75.         }
  76.  
  77.    }
  78.  
  79.     public static void SubtractMatrix(int[,] array1, int[,] array2, int[,] array3)
  80.     {
  81.  
  82.         for (int row = 0; row < array1.GetLength(0); row++)
  83.         {
  84.  
  85.             for (int column = 0; column < array1.GetLength(1); column++)
  86.  
  87.                 array3[row, column] = array1[row, column] - array2[row, column];
  88.  
  89.         }
  90.  
  91.         for (int row = 0; row < array1.GetLength(0); row++)
  92.         {
  93.  
  94.             for (int column = 0; column < array1.GetLength(1); column++)
  95.  
  96.                 Console.Write("{0}", array3[row, column]);
  97.  
  98.         }
  99.  
  100.         Console.WriteLine();
  101.  
  102.     }
  103.     public static void Multipy(int[,] array1, int[,] array2, int[,] array3)
  104.     {
  105.         for (int row = 0; row < array1.GetLength(0); row++)
  106.         {
  107.             for (int column = 0; column < array2.GetLength(1); column++)
  108.             {
  109.                 for (int a = 0; a < array1.GetLength(1); a++)
  110.                     array3[row, column] += array1[row, a] * array2[a, column];
  111.                 Console.Write("{0}", array3[row, column]);
  112.             }
  113.             Console.WriteLine();
  114.         }
  115.  
  116.     }
  117.     public static void Answers(int[,] array3)
  118.     {
  119.  
  120.  
  121.  
  122.  
  123.     }
  124.  
  125.  
  126.  }
  127.  
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#17: Oct 11 '07

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
#18: Oct 11 '07

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.
Expand|Select|Wrap|Line Numbers
  1.  using System;
  2.  
  3.  
  4.  
  5. public class Matrix
  6. {
  7.  
  8.     public static void Main(string[] args)
  9.     {
  10.  
  11.         int[,] matrix1 = { { 1, 2 }, { 3, 4 } };
  12.  
  13.         int[,] matrix2 = { { 2, 3 }, { 4, 5 } };
  14.  
  15.         int[,] matrix3 = { { 0, 0 }, { 0, 0 } };
  16.  
  17.         InputMatrix(matrix1);
  18.  
  19.         Console.WriteLine();
  20.  
  21.         InputMatrix(matrix2);
  22.  
  23.         Console.WriteLine();
  24.  
  25.         AddMatrix(matrix1, matrix2, matrix3);
  26.  
  27.         SubtractMatrix(matrix1, matrix2, matrix3);
  28.  
  29.         Multipy(matrix1, matrix2, matrix3);
  30.  
  31.     }
  32.  
  33.     public static void InputMatrix(int[,] array)
  34.     {
  35.  
  36.         for (int row = 0; row < array.GetLength(0); row++)
  37.         {
  38.  
  39.             for (int column = 0; column < array.GetLength(1); column++)
  40.             {
  41.                 Console.WriteLine("Enter element {0}, {1}", row, column);
  42.                 array[row, column] = Convert.ToInt32(Console.ReadLine());
  43.  
  44.             }
  45.             Console.WriteLine();
  46.  
  47.  
  48.         }
  49.  
  50.     }
  51.  
  52.     public static void AddMatrix(int[,] array1, int[,] array2, int[,] array3)
  53.     {
  54.  
  55.         for (int row = 0; row < array1.GetLength(0); row++)
  56.         {
  57.  
  58.             for (int column = 0; column < array1.GetLength(1); column++)
  59.  
  60.                 array3[row, column] = array1[row, column] + array2[row, column];
  61.  
  62.         }
  63.  
  64.         for (int row = 0; row < array1.GetLength(0); row++)
  65.         {
  66.  
  67.             for (int column = 0; column < array1.GetLength(1); column++)
  68.  
  69.                 Console.Write("{0}", array3[row, column]);
  70.  
  71.                 Console.WriteLine();
  72.  
  73.  
  74.         }
  75.  
  76.    }
  77.  
  78.     public static void SubtractMatrix(int[,] array1, int[,] array2, int[,] array3)
  79.     {
  80.  
  81.         for (int row = 0; row < array1.GetLength(0); row++)
  82.         {
  83.  
  84.             for (int column = 0; column < array1.GetLength(1); column++)
  85.  
  86.                 array3[row, column] = array1[row, column] - array2[row, column];
  87.  
  88.         }
  89.  
  90.         for (int row = 0; row < array1.GetLength(0); row++)
  91.         {
  92.  
  93.             for (int column = 0; column < array1.GetLength(1); column++)
  94.  
  95.                 Console.Write("{0}", array3[row, column]);
  96.  
  97.         }
  98.  
  99.         Console.WriteLine();
  100.  
  101.     }
  102.     public static void Multipy(int[,] array1, int[,] array2, int[,] array3)
  103.     {
  104.         for (int row = 0; row < array1.GetLength(0); row++)
  105.         {
  106.             for (int column = 0; column < array2.GetLength(1); column++)
  107.             {
  108.                 for (int a = 0; a < array1.GetLength(1); a++)
  109.                     array3[row, column] += array1[row, a] * array2[a, column];
  110.                 Console.Write("{0}", array3[row, column]);
  111.                 Console.WriteLine();
  112.             }
  113.             Console.WriteLine();
  114.         }
  115.  
  116.     }
  117.     public static void Answers(int[,] array3)
  118.     {
  119.  
  120.  
  121.  
  122.  
  123.     }
  124.  
  125.  
  126.  }
  127.  
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#19: Oct 11 '07

re: C# init arrays


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ConsoleApplication4
  6. {
  7.     public static class Matrix
  8.     {
  9.  
  10.         public static void theMatrix()
  11.         {
  12.  
  13.             int[,] matrix1 = { { 1, 2 }, { 3, 4 } };
  14.  
  15.             int[,] matrix2 = { { 2, 3 }, { 4, 5 } };
  16.  
  17.             int[,] matrix3 = { { 0, 0 }, { 0, 0 } };
  18.  
  19.             InputMatrix(ref matrix1);
  20.  
  21.             Console.WriteLine();
  22.  
  23.             InputMatrix(ref matrix2);
  24.  
  25.             Console.WriteLine();
  26.  
  27.             AddMatrix(matrix1, matrix2, ref matrix3);
  28.  
  29.             SubtractMatrix(matrix1, matrix2, ref matrix3);
  30.  
  31.             Multipy(matrix1, matrix2, ref matrix3);
  32.  
  33.         }
  34.  
  35.         public static void InputMatrix(ref int[,] array)
  36.         {
  37.             for (int row = 0; row < array.GetLength(0); row++)
  38.             {
  39.                 for (int column = 0; column < array.GetLength(1); column++)
  40.                 {
  41.                     Console.WriteLine("Enter element {0}, {1}", row, column);
  42.                     array[row, column] = Convert.ToInt32(Console.ReadLine());
  43.                 }
  44.                 Console.WriteLine();
  45.             }
  46.         }
  47.  
  48.         public static void AddMatrix(int[,] array1, int[,] array2,ref int[,] array3)
  49.         {
  50.             for (int row = 0; row < array1.GetLength(0); row++)
  51.             {
  52.                 for (int column = 0; column < array1.GetLength(1); column++)
  53.                     array3[row, column] = array1[row, column] + array2[row, column];
  54.             }
  55.             Answers(array3,"Addition");
  56.         }
  57.  
  58.         public static void SubtractMatrix(int[,] array1, int[,] array2, ref int[,] array3)
  59.         {
  60.             for (int row = 0; row < array1.GetLength(0); row++)
  61.             {
  62.                 for (int column = 0; column < array1.GetLength(1); column++)
  63.                     array3[row, column] = array1[row, column] - array2[row, column];
  64.             }
  65.             Answers(array3,"Subtraction");
  66.         }
  67.  
  68.         public static void Multipy(int[,] array1, int[,] array2, ref int[,] array3)
  69.         {
  70.             for (int row = 0; row < array1.GetLength(0); row++)
  71.             {
  72.                 for (int column = 0; column < array2.GetLength(1); column++)
  73.                 {
  74.                     for (int a = 0; a < array1.GetLength(1); a++)
  75.                         array3[row, column] += array1[row, a] * array2[a, column];
  76.                 }
  77.             }
  78.             Answers(array3,"Multiplication");
  79.         }
  80.         public static void Answers(int[,] array3, string answerType)
  81.         {
  82.             Console.WriteLine("Answer({0}):",answerType);
  83.             for (int row = 0; row < array3.GetLength(0); row++)
  84.             {
  85.                 for (int column = 0; column < array3.GetLength(1); column++)
  86.                 {
  87.                     Console.Write("{0}", array3[row, column]);
  88.                 }
  89.                 Console.WriteLine();
  90.             }
  91.         }
  92.  
  93.  
  94.     }
  95.  
  96. }
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
#20: Oct 11 '07

re: C# init arrays


Thanks. One more thing. Is there a way I can space the numbers out instead of being so jam.
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#21: Oct 11 '07

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
Reply