# cannot convert from int to int[][]

• 04-18-2010, 02:59 AM
besweeet
cannot convert from int to int[][]
I need to "change all number n in the array by |M-n, where M is the result of method Average applied on the input 2D-Array."

I was given some partially completed code:
Code:

```public **** Modify(xxxx){         for(int i = 0; i < 10; i++){                 for(int j = 0; j < 10; j++){                         compute average         for(int i = 0; i < 10; i++){                 for(int j = 0; j < 10; j++){                 {                         matrix[][] = m - matrix[][];                         check if the value is < 0, if < 0 then make it positive                         SOP(.....);                 }         }         return matrix; }```
Here's my code:
Code:

```        public static int[][] Modify(int [][] array1)         {                 int sum = 0;                 for (int row = 0; row < array1.length; row++){                         for (int col = 0; col < array1[row].length; col++){                                 sum = sum+array1[row][col];}}                 int average = sum / 100;                 for (int row= 0 ; row < array1.length; row++){                         for (int col = 0; col < array1[row].length; col++){                                 array1 = average - array1[row][col];                                 if(array1[row][col] < 0)```
On the second to the last line, I get this error: "Type mismatch: cannot convert from int to int[][]"

Any ideas?
• 04-18-2010, 03:11 AM
Cruncher
Code:

`array1 = average - array1[row][col];`
array1 is an array, and are trying to assign it a value that is not an array. you need to change this to:
Code:

`array1[row][col] = average - array1[row][col];`
• 04-18-2010, 03:11 AM
besweeet
Yes, I just did that, thank you :).
• 04-18-2010, 03:16 AM
Fubarable
Code:

`int average = sum / 100;`
Why use the arbitrary number 100 as the denominator here? Are you absolutely sure that this is the correct number to use? Isn't there a way that you can calculate precisely what the denominator should be?
• 04-18-2010, 03:19 AM
Cruncher
Quote:

Code:

`int average = sum / 100;`
Why use the arbitrary number 100 as the denominator here? Are you absolutely sure that this is the correct number to use? Isn't there a way that you can calculate precisely what the denominator should be?

inside of his given code he has:
Code:

```        for(int i = 0; i < 10; i++){                 for(int j = 0; j < 10; j++){```
That assumes there will be 100, (10 * 10) entries. But i agree with you in the sense he should set his condition in his loop to either be 10, or replace sum with array1.length * array1[row].length
• 04-18-2010, 03:20 AM
Cruncher
Quote:

inside of his given code he has:
Code:

```        for(int i = 0; i < 10; i++){                 for(int j = 0; j < 10; j++){```
That assumes there will be 100, (10 * 10) entries. But i agree with you in the sense he should set his condition in his loop to either be 10, or replace sum with array1.length * array1[row].length

edit: should actually be array1.length * array1.length the row variable doesn't exist after the for is ended

OOPS, hit the quote button not edit, lol... stupid me
• 04-18-2010, 03:23 AM
Fubarable
Quote:

But i agree with you in the sense he should set his condition in his loop to either be 10, or replace sum with array1.length * array1[row].length

Which is safer in the long (and short) run?
• 04-18-2010, 04:26 AM
besweeet
I used 100 because that's exactly the amount of numbers that are in the array. I don't know how to do it otherwise.
• 04-18-2010, 04:30 AM
Cruncher
Quote:

I used 100 because that's exactly the amount of numbers that are in the array. I don't know how to do it otherwise.

okay but for safe measures, you can count the number of times it loops, or just use your loop conditions to know how many numbers have been added to sum.

if you KNOW 100% that it's a 10*10 array, then you should use 10 as your condition.

Also note that sum / 100 will always return an integer, but it does not round, it just drops the decimal. e.g 2.99 = 2. So you should probably say average = Math.round(sum / 100.0)
• 04-18-2010, 04:36 AM
Fubarable
If your arrays are not ragged, then simply get the array1.length and array1.length and multiply the two. If the arrays are ragged, then create an int counter variable that's initialized to 0 and that gets incremented by one each time your nested for loop loops.

If you know that the arrays are of length 10, I'd still use the array lengths as noted above because next thing you know, you'll be instructed to make your method work with a 20x10 two-dimensional array, and if you use lengths, you'll not have to change anything.