Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Arrays

  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrays

    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();

    }


    }

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    What is this, C#.NET? 1.1 or 2.0?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm using Microsoft Visual C# 2005 Express Edition.

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikkiH View Post
    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)
    {




    }


    }

  • #6
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •