Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. i m getting an error while using array, please help me

i m getting an error while using array, please help me

Scheduled Pinned Locked Moved C#
helpcsharpdatabaselinqdata-structures
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    somasekhara777
    wrote on last edited by
    #1

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace multiplication
    {
    class Program
    {
    static void Main(string[] args)
    {
    int[,] a = new int[2, 2] { { 1, 2 }, { 3, 4 } };
    int[,] b = new int[2, 2] { { 1, 2 }, { 3, 4 } };
    int[,] c = new int[2, 2];
    for (int i = 0; i <= a.Length; i++)
    {
    for (int j = 0; j <= b.Length; j++)
    {
    c[i, j] = (a[i, j] * b[i, j])+(a[i,,j+1]*b[i+1,,j]);
    Console.Write(c[i, j]+ " " );

                }
                Console.WriteLine();
            }
                Console.ReadLine();
    
        }
    }
    

    }
    erorr is like
    Index was outside the bounds of the array.

    j somasekhar

    L M P L 5 Replies Last reply
    0
    • S somasekhara777

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace multiplication
      {
      class Program
      {
      static void Main(string[] args)
      {
      int[,] a = new int[2, 2] { { 1, 2 }, { 3, 4 } };
      int[,] b = new int[2, 2] { { 1, 2 }, { 3, 4 } };
      int[,] c = new int[2, 2];
      for (int i = 0; i <= a.Length; i++)
      {
      for (int j = 0; j <= b.Length; j++)
      {
      c[i, j] = (a[i, j] * b[i, j])+(a[i,,j+1]*b[i+1,,j]);
      Console.Write(c[i, j]+ " " );

                  }
                  Console.WriteLine();
              }
                  Console.ReadLine();
      
          }
      }
      

      }
      erorr is like
      Index was outside the bounds of the array.

      j somasekhar

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Please include the error-text, and the result that you were aiming for - ie, what should the code do?

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      1 Reply Last reply
      0
      • S somasekhara777

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;

        namespace multiplication
        {
        class Program
        {
        static void Main(string[] args)
        {
        int[,] a = new int[2, 2] { { 1, 2 }, { 3, 4 } };
        int[,] b = new int[2, 2] { { 1, 2 }, { 3, 4 } };
        int[,] c = new int[2, 2];
        for (int i = 0; i <= a.Length; i++)
        {
        for (int j = 0; j <= b.Length; j++)
        {
        c[i, j] = (a[i, j] * b[i, j])+(a[i,,j+1]*b[i+1,,j]);
        Console.Write(c[i, j]+ " " );

                    }
                    Console.WriteLine();
                }
                    Console.ReadLine();
        
            }
        }
        

        }
        erorr is like
        Index was outside the bounds of the array.

        j somasekhar

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        For those who can't read minds, the exception you are getting is an out of bounds exception. The area that you are hitting this exception is here:

        (a[i, j + 1] * b[i + 1, j])

        The reason you are hitting this is because j+1 is 2 after the first iteration of j, which is outside of the bounds of the array. As I'm not sure what effect you are aiming at here, I can't really offer any concrete solution other than to say that you need to think through your logic here. BTW - what length do you think is represented as a.Length or b.Length. It's not the 2 you're expecting; it's 4. Also, you are using <= in your conditions and it should be < only.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        M 1 Reply Last reply
        0
        • S somasekhara777

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace multiplication
          {
          class Program
          {
          static void Main(string[] args)
          {
          int[,] a = new int[2, 2] { { 1, 2 }, { 3, 4 } };
          int[,] b = new int[2, 2] { { 1, 2 }, { 3, 4 } };
          int[,] c = new int[2, 2];
          for (int i = 0; i <= a.Length; i++)
          {
          for (int j = 0; j <= b.Length; j++)
          {
          c[i, j] = (a[i, j] * b[i, j])+(a[i,,j+1]*b[i+1,,j]);
          Console.Write(c[i, j]+ " " );

                      }
                      Console.WriteLine();
                  }
                      Console.ReadLine();
          
              }
          }
          

          }
          erorr is like
          Index was outside the bounds of the array.

          j somasekhar

          M Offline
          M Offline
          markovl
          wrote on last edited by
          #4

          At first glance, there's a syntax error here (...) (a[i,,j+1]*b[i+1,,j]) (...) - note the consecutive commas, lose one of each. Like this:

          // omitted
          c[i, j] = (a[i, j] * b[i, j])+(a[i,j+1]*b[i+1,j]);
          // omitted

          2A

          1 Reply Last reply
          0
          • P Pete OHanlon

            For those who can't read minds, the exception you are getting is an out of bounds exception. The area that you are hitting this exception is here:

            (a[i, j + 1] * b[i + 1, j])

            The reason you are hitting this is because j+1 is 2 after the first iteration of j, which is outside of the bounds of the array. As I'm not sure what effect you are aiming at here, I can't really offer any concrete solution other than to say that you need to think through your logic here. BTW - what length do you think is represented as a.Length or b.Length. It's not the 2 you're expecting; it's 4. Also, you are using <= in your conditions and it should be < only.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            M Offline
            M Offline
            markovl
            wrote on last edited by
            #5

            Nice catch! Have a five. After spotting the syntax error in the code sample, I directly assumed that was the problem and stopped thinking right there... :)

            2A

            P 1 Reply Last reply
            0
            • M markovl

              Nice catch! Have a five. After spotting the syntax error in the code sample, I directly assumed that was the problem and stopped thinking right there... :)

              2A

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              I spotted that error and figured it was just an error putting it into the CP editor. As soon as I saw that it was arrays and looping, I knew he'd have an out of bounds error in there somewhere. The rest was just figuring out where it was.

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              1 Reply Last reply
              0
              • S somasekhara777

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;

                namespace multiplication
                {
                class Program
                {
                static void Main(string[] args)
                {
                int[,] a = new int[2, 2] { { 1, 2 }, { 3, 4 } };
                int[,] b = new int[2, 2] { { 1, 2 }, { 3, 4 } };
                int[,] c = new int[2, 2];
                for (int i = 0; i <= a.Length; i++)
                {
                for (int j = 0; j <= b.Length; j++)
                {
                c[i, j] = (a[i, j] * b[i, j])+(a[i,,j+1]*b[i+1,,j]);
                Console.Write(c[i, j]+ " " );

                            }
                            Console.WriteLine();
                        }
                            Console.ReadLine();
                
                    }
                }
                

                }
                erorr is like
                Index was outside the bounds of the array.

                j somasekhar

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                If that is meant to perform a matrix multiplication, then: 1. it would require 3 nested loops, not 2, as each element (that is 2 loops) of the result matrix is to equal the inner product of one row and one column (that is the third loop, dealing with both the row of a and the column of b) 2. I would not waste time on doing a specific case; instead use variable dimensions to make it general-purpose. 3. And unless you want this as an exercise, there is a Matrix class that does it all for you, no hassle. See e.g. here[^] :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                1 Reply Last reply
                0
                • S somasekhara777

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;

                  namespace multiplication
                  {
                  class Program
                  {
                  static void Main(string[] args)
                  {
                  int[,] a = new int[2, 2] { { 1, 2 }, { 3, 4 } };
                  int[,] b = new int[2, 2] { { 1, 2 }, { 3, 4 } };
                  int[,] c = new int[2, 2];
                  for (int i = 0; i <= a.Length; i++)
                  {
                  for (int j = 0; j <= b.Length; j++)
                  {
                  c[i, j] = (a[i, j] * b[i, j])+(a[i,,j+1]*b[i+1,,j]);
                  Console.Write(c[i, j]+ " " );

                              }
                              Console.WriteLine();
                          }
                              Console.ReadLine();
                  
                      }
                  }
                  

                  }
                  erorr is like
                  Index was outside the bounds of the array.

                  j somasekhar

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  . If that is meant to perform a matrix multiplication, then: 1. it would require 3 nested loops, not 2, as each element (that is 2 loops) of the result matrix is to equal the inner product of one row and one column (that is the third loop, dealing with both the row of a and the column of b) 2. I would not waste time on doing a specific case; instead use variable dimensions to make it general-purpose. 3. And unless you want this as an exercise, there is a Matrix class that does it all for you, no hassle. See e.g. here[^] :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups