473,378 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C# init arrays

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}
Oct 9 '07 #1
20 6395
Shashi Sadasivan
1,435 Expert 1GB
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
Oct 9 '07 #2
wkid87
13
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?
Oct 9 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
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
Oct 9 '07 #4
wkid87
13
I use Visual Basic 2005.
Oct 9 '07 #5
Plater
7,872 Expert 4TB
I use Visual Basic 2005.
I would guess it's a console application in VB.NET
Oct 9 '07 #6
wkid87
13
Its Microsoft Visual C# 2005
Oct 9 '07 #7
Plater
7,872 Expert 4TB
Its Microsoft Visual C# 2005
You *JUST* said you were using Visual Basic.
Which is it?
Oct 9 '07 #8
wkid87
13
Its Microsoft Visual C# 2005, sorry. Was on my laptop earlier and was confuse.
Oct 9 '07 #9
Shashi Sadasivan
1,435 Expert 1GB
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?
Oct 9 '07 #10
wkid87
13
Ok...VB to C#
So....is it a web application? a windows application or a console one?
It is a Console application
Oct 10 '07 #11
wkid87
13
I trying to figure out the multipication. I know I probably need three for statements but I'm still stuck.
Oct 10 '07 #12
Shashi Sadasivan
1,435 Expert 1GB
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
Oct 10 '07 #13
wkid87
13
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.
Oct 10 '07 #14
Shashi Sadasivan
1,435 Expert 1GB
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, 112 views)
Oct 10 '07 #15
wkid87
13
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.  
Oct 11 '07 #16
Shashi Sadasivan
1,435 Expert 1GB
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
Oct 11 '07 #17
wkid87
13
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.  
Oct 11 '07 #18
Shashi Sadasivan
1,435 Expert 1GB
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
Oct 11 '07 #19
wkid87
13
Thanks. One more thing. Is there a way I can space the numbers out instead of being so jam.
Oct 11 '07 #20
Shashi Sadasivan
1,435 Expert 1GB
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
Oct 11 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Vij | last post by:
I can do this int a = { 5,6,7,8,9}; but how can I do this inside a class? something like this class CTest { private: int a // Init the array here };
6
by: Paul | last post by:
Hello, Consider this case: Init(params) does some initialization and there is a function Connect(params). The system starts and is idle until some one calls Connect, as soon as first...
3
by: S. Nurbe | last post by:
Hi, small question concerning pointer: How do I allocate and have access to this pointer in C: float **x; such that I can work with it like an array, e.g. x?
14
by: Stef | last post by:
Hello, I have a question: Is it possible to init a 2d array of structures to zero ? For example with array I do. int array = {0} but:
4
by: Anatoly | last post by:
Put any control on web page. create Init event for ths control. Write Response.Write("here") inside this event. Compile\build\run. I never saw "here" string appear on web page. Why???
10
by: Wylbur via DotNetMonster.com | last post by:
Hello to all of you geniuses, I'm having a problem trying to get an Init handler to fire for a Placeholder control at the initialization phase. I’ve posted this problem to 3 other ASP.NET...
6
by: Shimon Sim | last post by:
I have Panel control on the page. I am handling Init event for it. It doesn't seem to fire at all. Why? Thank you Shimon.
8
by: Ender.Dai | last post by:
I have writen following demo code, but it doesn't work :( Source code: -------------------------------- /* hello.c */ #include <stdio.h> extern int hello_init() __attribute__...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
2
by: Christof Warlich | last post by:
Hi, I'm working on a (template) library that is up to now entirely implemented in header-files. This makes the library quite convenient to use as no extra object code needs to be linked when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.