i m getting an error while using array, please help me
-
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
-
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
-
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
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
orb.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
-
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
-
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
orb.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
-
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
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
-
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
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
-
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
. 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