array maths

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrcw
    New Member
    • Nov 2008
    • 82

    #1

    array maths

    if I have an 2 dimensional array eg

    int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };

    how do I add 8 to the first item of the third row eg 5 to make

    [3, 2] { {1, 2}, {3, 4}, {13, 6} };

    thnx
    mrcw
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Your initializer creates the following:
    numbers[0,0] = 1
    numbers[0,1] = 2
    numbers[1,0] = 3
    numbers[1,1] = 4
    numbers[2,0] = 5
    numbers[2,1] = 6

    So to add 8 to the first item of the third row:
    numbers[2,0] = numbers[2,0] + 8;

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      You need to access the appropriate index for that item. In C#, array indexes start at zero, so according to your setup the third row would be index 2, and the first item of that row would be index 0.

      Code:
      numbers[2,0] += 8;
      // or...
      numbers[2,0] = numbers[2][0] + 8;
      Here's the MSDN page on multidimensiona l arrays...
      http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx

      *Edit: Ahhh look at me, getting caught up by syntax. Fixed!

      Comment

      • mrcw
        New Member
        • Nov 2008
        • 82

        #4
        can I use that code in a windows form application ? It works fine as a console application, but as a wpa it gives me lots of nonsense errors

        mrcw

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          It should work fine. Please provide a copy of the code from your app along with the error message

          Comment

          • mrcw
            New Member
            • Nov 2008
            • 82

            #6
            int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; is fine -no errors


            numbers[2, 0] = numbers[2, 0] + 8; red wriggly line under both 2, = and +

            Error 1 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

            Error 2 Invalid token '=' in class, struct, or interface member declaration

            Error 3 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

            Error 4 Invalid token '+' in class, struct, or interface member declaration

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              As was earlier suggested, try it as numbers[2][0]

              Comment

              • mrcw
                New Member
                • Nov 2008
                • 82

                #8
                hi

                I am trying the examples from http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

                This gives me 11 errors that are copies of the ones I gave before
                I still qustion "can I use arrays in wpa? It does appear not.

                mrcw
                Code:
                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.Data;
                using System.Drawing;
                using System.Linq;
                using System.Text;
                using System.Windows.Forms;
                
                
                namespace Arrays1
                {
                    public partial class Form1 : Form
                    {
                        public Form1()
                        {
                            InitializeComponent();
                        }
                
                
                         int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
                         numbers[2, 0] = numbers[2][0] + 8;
                
                        //int[,] myArray = new int[4,2];
                        // int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
                
                         int[,] myArray = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
                         myArray[1,0] = myArray[1,3] + 5;
                
                         int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
                         array2D[2 ,1 ] = 25;
                
                
                         //int[,] array5;
                         //array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; 
                         //array5[2, 1] = 25;
                
                         int[,] array6 = new int[10, 10];
                
                    }
                }[
                Last edited by Frinavale; Jan 13 '10, 03:26 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

                Comment

                Working...