9 Replies - 4334 Views - Last Post: 24 February 2010 - 02:42 PM Rate Topic: -----

#1 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 10

Creating a 2 dimensional array that uses the products of...

Posted 22 February 2010 - 08:29 PM

Write the following program that creates and uses a loop to populate a one-dimensional array that holds the even numbers between 1 and 12; creates and uses a loop to populate a second one-dimensional array that holds the odd numbers between 1 and 12; creates a two-dimensional array that holds the values of the products of the two one-dimensional arrays. Example...the value of [0][0] in the two dimensional array should be the value of the the [0] subscript in the even array multiplied by the [0] subscript in the odd array. The value of the [1][1] spot in the two dimensional array should be the value of the [1] subscript in the even array multipled by the [1] subscript in the odd array. Output the values of the two dimensional array to the screen in an organized fashion so it is clear which values are which.

This is what I have so far:
I don't know how to create the 2 dimensional array that accesses the other one dimensionals and also I know that my product int is wrong. Any help is greatly appreciated.


public class Mult {
	public static void main(String[] args){
		
		
		int M1[] = new int[13];
		int M2[] = new int[12];
		int M3[][] = ;
		
		
		
		for (int i = 2; i < M1.length; i+=2){
			
		}
		
		for (int j = 1; j < M2.length; j+=2)
			{
				
			}
	    int product = M1[i]*M2[j];
			
	    System.out.println(M3);
	}		
}  


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a 2 dimensional array that uses the products of...

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Creating a 2 dimensional array that uses the products of...

Posted 22 February 2010 - 08:47 PM

providing total code will be a cheating and moderators will throw me out of here
so please try to code as far as possible
here are some hints
  • You can store a one dimensional array in one element of two dimensional array
  • you are not storing array elements using the for loop, i.e. the loops are empty
  • you should do the product calculations in the loops only
  • Also while printing the array you should use loops

Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Creating a 2 dimensional array that uses the products of...

Posted 22 February 2010 - 08:50 PM

why is one 2d array contains 12 values, and the second contans 13?
both arrays should contain 6 values. (odd: 1, 3 ,5 ,7, 9 ,11 are 6 numbers), (even: 2, 4, 6, 8, 10, 12 are 6 numbers).

int[] evenArr = new int[6];
int[] oddArr = new int[6];

//populate the arrays:

for(int i = 1; i <=12; i++){
     if(i%2==0){
          evenArr[i / 2 - 1] = i;
     }
     else {
          oddArr[i / 2] = i;
     }
}



your way of populating the arrays is also fine. just place the value if i\j in the correct array.

to create a 2d array:
int[][] M3 = new int[ROWS][COLS];
//in your case:
int[][] M3 = new int[6][6];



to populate the 2d array:
for(int i = 0 ; i < 6; i++){
M3[i][i] = evenArr[i] * oddArr[i];
}


usually you will assign values to 2d array with a nested for loop. however, here you have to assign to the same row and column index, so one for loop is enough.

take a look at this tutorial for a further understanding of arrays in java:
http://java.sun.com/...lts/arrays.html
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Creating a 2 dimensional array that uses the products of...

Posted 22 February 2010 - 09:07 PM

Japanir is right (as usual) but this one might be easier to understand

You definitively seem to have a problem between array index and array value
int[] odd = new int[6];
int[] even = new int[6];
int k = 0;
for(int i = 0; i < 6; i++) {
   even[i] = k;
   k++;
   odd[i] = k;
   k++;
}



Also if you want to continue in Java respect Java universal standarts that say that variable names begin with a lowercase letter
Was This Post Helpful? 1
  • +
  • -

#5 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 10

Re: Creating a 2 dimensional array that uses the products of...

Posted 23 February 2010 - 08:35 AM

I did it as you said, and I didn't think of using and if else statement to populate the odd array, but I am still having trouble with the 2 dimensional array. (I did read the java tutorial but I just didn't see anything I could use in this case). Thanks.

public class Mult {
	public static void main(String[] args){
		
		
		int even[] = new int[6];
		int odd[] = new int[6];
		int[][] M = new int[6][6];

		
		
		
		for (int i = 1; i <= 12; i++){
			if(i%2==0){
		          even[i / 2 - 1] = i;
		     }
		     else {
		          odd[i / 2] = i;
		     }
			
			
			for(int j = 0; i < 6; j++){
			M[i][i]= even[i] * odd[i];
			}
			System.out.println(M);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 10

Re: Creating a 2 dimensional array that uses the products of...

Posted 23 February 2010 - 08:42 AM

the j in
for(int j = 0; i < 6; j++)
has been changed to an i as per instruction. I get the error message
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable i
Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Creating a 2 dimensional array that uses the products of...

Posted 23 February 2010 - 08:52 AM

look at your for loop here:
for(int j = 0; i < 6; j++){ 
                        M[i][i]= even[i] * odd[i]; 
                        } 
                        System.out.println(M);

you start looping j, but checking if i is smaller than 6?
fix this loop to:
for(int j = 0; j < 6; j++){ 
                        M[j][j]= even[j] * odd[j]; 
                        } 

also, remove this loop out of the for loop that fills the even and odd arrays.

also, to print your 2d array:
for(int rows = 0 ; rows < 6; rows++){ 
        for(int cols = 0 ; cols < 6; cols++){ 
               System.out.print(M[rows][cols] + " ");
        }
        //new row, new line.
        System.out.println();
}


the full updated code may look like:
public class Mult { 
        public static void main(String[] args){ 
                 
                 
                int even[] = new int[6]; 
                int odd[] = new int[6]; 
                int[][] M = new int[6][6]; 
 
                 
                 
                 
                for (int i = 1; i <= 12; i++){ 
                        if(i%2==0){ 
                          even[i / 2 - 1] = i; 
                     } 
                     else { 
                          odd[i / 2] = i; 
                     } 
                }//end for
                         
                         
                        for(int j = 0; j < 6; j++){ 
                        M[j][j]= even[j] * odd[j];  
                        } 
for(int rows = 0 ; rows < 6; rows++){ 
        for(int cols = 0 ; cols < 6; cols++){ 
               System.out.print(M[rows][cols] + " ");
        }
        //new row, new line.
        System.out.println();
}
                
        } //end main
}//end class


**edited code tags

This post has been edited by japanir: 23 February 2010 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 10

Re: Creating a 2 dimensional array that uses the products of...

Posted 23 February 2010 - 09:38 AM

That displays the table, 6x6, but only values at (0,0)(1,1)(2,2)(3,3) etc. the other values are all zero.
how does this work? if(i%2==0){
even[i / 2 - 1] = i;
}
else {
odd[i / 2] = i;
if i has a remainder of 2 it is 0? i/2-1
and
M[j][j]= even[j] * odd[j];

this tells the program that M will be filled with even and odd arrays?
Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Creating a 2 dimensional array that uses the products of...

Posted 23 February 2010 - 09:47 AM

this is what you try to do yourself:

Quote

the value of [0][0] in the two dimensional array should be the value of the the [0] subscript in the even array multiplied by the [0] subscript in the odd array. The value of the [1][1] spot in the two dimensional array should..

only the cells of M[IND][IND] like M[0][0], M[1][1] will be printed. the rest are zeroes, since when initiating an array of int values, all values are initated to 0.
what value for example would you place in the [0][1] index of the 2d array?

if you ask about the logic of this:
if(i%2==0){ 
even[i / 2 - 1] = i; 
} 
else { 
odd[i / 2] = i;


if i is even, place the value of i in the i/2 - 1 index of the array.
else place it in the i/2 index of the array.

for example:
i = 1.
i is odd, place it in the i/2 = 0 index of the odd array.
odd[0] = 1;
i = 2
i is even, place it in the i/2 - 1 = 0 index of the even array:
even[0] = 2;
etc..
Was This Post Helpful? 1
  • +
  • -

#10 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 10

Re: Creating a 2 dimensional array that uses the products of...

Posted 24 February 2010 - 02:42 PM

I'm sorry I just don't understand how to initialize it to replace the zeros with numbers. I can see that the numbers showing are all multiples of 2 and they seem to be in some kind of order. The number in the 0,1 index should be an odd one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1