# Need help to write the code

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-26-2012, 10:53 PM
Galichka
Need help to write the code
Hello guys!! i'm new to this forum and also new to java :) but i'm working hard on it.
i have a question to ask, if anyone could help me with two dimensional arrays. here is the problem,
i have to write some code so that output would look like this:
0 1 1 1 1 1 0
0 0 1 1 1 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0

i've done this so far, it it doesn't quiet look like it's supposed to look.
Code:

public class massiv {

public static void main(String args[]){

//declare an array
int[][] a;
a = new int[7][7];
// print array in rectangular form
for (int r=0; r<a.length; r++) {
for        (int c=0; c<a[r].length; c++) {
a[r][c]=1;
if (r==c){
a[r][c]=0;
}
if (r>c && r<=a[r].length/2 ){
a[r][c]=0;
}
if (c>r && r>=a[r].length/2 ){
a[r][c]=0;
}
if (r>c && r>a[r].length/2 && c<a[c].length/2){

a[r][c]=0;
}

if(r<c && c>a[c].length/2 && r<a[r].length/2){
a[r][c]=0;
}

System.out.print(" " + a[r][c]);
}

System.out.println("");
}
}
}

could anyone help?
thanks
• 03-27-2012, 02:13 AM
Norm
Re: Need help to write the code
Quote:

it doesn't quiet look like it's supposed to look
Please show what the output looks like and explain what you want to change about how it looks.
• 03-27-2012, 08:57 AM
Galichka
Re: Need help to write the code
Quote:

Originally Posted by Norm
Please show what the output looks like and explain what you want to change about how it looks.

i'm sorry if i made it unclear.
i have to make a two dimensional array that should look like this
0 1 1 1 1 1 0
0 0 1 1 1 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
• 03-27-2012, 09:03 AM
gauravfzd
Re: Need help to write the code
We understood what you want to do. But Norm asked what is the output of your code?
• 03-27-2012, 09:14 AM
Galichka
Re: Need help to write the code
[QUOTE=gauravfzd;274953]We understood what you want to do. But Norm asked what is the output of your code?[/QUOT]

oh i'm sorry,
the output of my code looks like this:
0 1 1 1 0 0 0
0 0 1 1 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 1 1 0 0
0 0 0 1 1 1 0
• 03-27-2012, 01:35 PM
Norm
Re: Need help to write the code
Can you explain what is wrong with the program's output?
It looks like the output is formatted just the same as the desired output.

Is the problem in the creating of the contents of the 2D array, not how it is printed?
• 03-27-2012, 02:08 PM
Galichka
Re: Need help to write the code
Quote:

Originally Posted by Norm
Can you explain what is wrong with the program's output?
It looks like the output is formatted just the same as the desired output.

Is the problem in the creating of the contents of the 2D array, not how it is printed?

Yes, Norm, the problem is in creating the content of the 2Darray. as i've said the content should be:
0 1 1 1 1 1 0

0 0 1 1 1 0 0

0 0 0 1 0 0 0

0 0 1 1 1 0 0

0 1 1 1 1 1 0
• 03-27-2012, 02:15 PM
JosAH
Re: Need help to write the code
Take a bit more mahematical approach: think of an x-axis and y-axis through the middle of your matrix; a bit of fiddling shows that for abs(x) < abs(y) the matrix element should be 1, otherwise it should be zero. Your matrix runs from index elements 0 up to (including) 6; your axes run from -3 up to (including) 3, so you have to subtract 3 from both index numbers before you do the test. This explanation was according to your original post; your latest example is a bit different (but similar).

kind regards,

Jos
• 03-27-2012, 02:15 PM
Norm
Re: Need help to write the code
Can you explain what the algorithm is for creating the contents of the array?
For example:
Given the row and column index values, what value should the element at those indexes have?
• 03-27-2012, 02:27 PM
Galichka
Re: Need help to write the code
i'm sorry about my previous post. the original post is what i need to have as an output. speaking of x and y. this is what i tried to do including length/2 - which is going from -3 to 0 and from 0 to 3. it doesn't work. the output is as at #5.
• 03-27-2012, 02:29 PM
Galichka
Re: Need help to write the code
Quote:

Originally Posted by Norm
Can you explain what the algorithm is for creating the contents of the array?
For example:
Given the row and column index values, what value should the element at those indexes have?

i'm sorry, what do you mean?
the task is i think clear enough:
make 2D array that looks like this:
0 1 1 1 1 1 0
0 0 1 1 1 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
• 03-27-2012, 02:36 PM
noobplus
Re: Need help to write the code
Quote:

Originally Posted by Galichka
Code:

if(r<c && c>a[c].length/2 && r<a[r].length/2){ //line 26
a[r][c]=0;                            //line 27
}                                            //line 28

[...this makes only a[0][1],a[0][2],a[0][3] to 1. ...](mistaken)
you should continue it upto a[0][6] //useless comment I made
Edited to add: "this makes only a[0][1],a[0][2],a[0][3] to 1." sorry, it makes a[0][4],a[0][5],a[0][6] to '0' that's the code's mistake
regards
dhilip
• 03-27-2012, 02:41 PM
JosAH
Re: Need help to write the code
Quote:

Originally Posted by Galichka
i'm sorry about my previous post. the original post is what i need to have as an output. speaking of x and y. this is what i tried to do including length/2 - which is going from -3 to 0 and from 0 to 3. it doesn't work. the output is as at #5.

So your implementation is wrong; for the values (x,y) where abs(x) < abs(y) you get your figure (if you implement it correctly).

kind regards,

Jos
• 03-27-2012, 02:41 PM
noobplus
Re: Need help to write the code
hi,
for debugging mode, make it like this..
Code:

if(r<c && c>a[c].length/2 && r<a[r].length/2){
a[r][c]=0; System.out.println("IF no.5: row="+r+"\tcolumn="+c+"\tvalue="+a[r][c]);
}

regards
dhilip
• 03-27-2012, 02:46 PM
noobplus
Re: Need help to write the code
hi,
i edited my post#12

regards
dhilip
• 03-27-2012, 02:49 PM
Norm
Re: Need help to write the code
Quote:

the task is i think clear enough:
Then you should easily be able to explain what goes in each element of the array. If you can not determine the algorithm for computing the values of the elements, then you will not be able to write the code.
• 03-27-2012, 03:42 PM
Galichka
Re: Need help to write the code
i'm sorry guys, i'm new to java and i was just looking for help to get the right output. some things that you say are confusing : )
• 03-27-2012, 03:47 PM
JosAH
Re: Need help to write the code
Draw a small grid on paper; mark the points (x,y) where abs(x) < abs(y) and admire the result ;-)

kind regards,

Jos
• 03-27-2012, 04:06 PM
Norm
Re: Need help to write the code
Look at each row and consider what goes in the elements for that row.
How do the contents of the row related to the row number?
Take the first row: 0 1 1 1 1 1 0
It starts and ends with one 0 and has 1s in between
Now look at the second row: 0 0 1 1 1 0 0
It starts and ends with two 0s with 1s in between
The relationship appears to be the number of 0s is the same as the row number.
• 03-27-2012, 04:06 PM
noobplus
Re: Need help to write the code
rather, U can make each value to '0' and change them to 1 in both directions...

Code:

public class massiv {

public static void main(String[] args)
{
int[][] a;
int r=Integer.parseInt(args[0]);
int c=Integer.parseInt(args[0]);

a = new int[ r ][c];
//make each value to zero
for(int i=0;i<r;i++)
{

for(int j=0;j<c;j++)
{

a[i][j]=0;

}
}
//make values to one in forward direction(doesn't work after a limit of half of the matrix length, so apply it in reverse direction also)
for(int i=0;i<r;i++)
{

for(int j=i+1;j<c-(i+1);j++)
{
a[i][j]=1;

}

}
//reverse direction
for(int i=r-1;i>=0;i--)
{

for(int j=(c-i);j<c-(c-i);j++) //changed 8 to c
{
a[i][j]=1;

}

}
// print array in rectangular form
for(int i=0;i<r;i++)
{

for(int j=0;j<c;j++)
{

System.out.print(a[i][j]);
}
System.out.println();
}

}
}

(give the size of the array in arguments)
this is the output for size 8
Code:

01111110
00111100
00011000
00000000
00000000
00011000
00111100
01111110

regards
dhilip
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last