Results 1 to 10 of 10
  1. #1
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default 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:
    Java 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:
    Java 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?

  2. #2
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Java 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:
    Java Code:
    array1[row][col] = average - array1[row][col];

  3. #3
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Yes, I just did that, thank you :).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have to wonder about this line too:
    Java 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?

  5. #5
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I have to wonder about this line too:
    Java 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:
    Java 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

  6. #6
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by Cruncher View Post
    inside of his given code he has:
    Java 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[0].length the row variable doesn't exist after the for is ended

    OOPS, hit the quote button not edit, lol... stupid me

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cruncher View Post
    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?

  8. #8
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    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.

  9. #9
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by besweeet View Post
    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)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If your arrays are not ragged, then simply get the array1.length and array1[0].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.

Similar Threads

  1. convert XML using XSL
    By rajjan4u in forum XML
    Replies: 3
    Last Post: 11-20-2009, 05:37 PM
  2. Convert to Int
    By 6kyAngel in forum New To Java
    Replies: 0
    Last Post: 03-06-2009, 10:18 AM
  3. how to convert xml to xsd
    By adi in forum XML
    Replies: 2
    Last Post: 02-12-2009, 03:43 PM
  4. need the help to convert to and from UTF-8 and UCS-2
    By sachin_n in forum Advanced Java
    Replies: 1
    Last Post: 01-05-2009, 10:35 PM
  5. Convert to Applet
    By Urgle in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 02:15 PM

Posting Permissions

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