# Thread: Need help to write the code

1. Member Join Date
Mar 2012
Posts
13
Rep Power
0

## 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.
Java Code:
```public class massiv {

public static void main(String args[]){

//declare an array
int[][] a;
a = new int;
// 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
Last edited by Norm; 03-27-2012 at 02:13 AM. Reason: added code tags  Reply With Quote

2. ## Re: Need help to write the code

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.  Reply With Quote

3. Member Join Date
Mar 2012
Posts
13
Rep Power
0

## Re: Need help to write the code 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  Reply With Quote

4. ## Re: Need help to write the code

We understood what you want to do. But Norm asked what is the output of your code?  Reply With Quote

5. Member Join Date
Mar 2012
Posts
13
Rep Power
0

## 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  Reply With Quote

6. ## 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?  Reply With Quote

7. Member Join Date
Mar 2012
Posts
13
Rep Power
0

## Re: Need help to write the code 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  Reply With Quote

8. ## 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  Reply With Quote

9. ## 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?  Reply With Quote

10. Member Join Date
Mar 2012
Posts
13
Rep Power
0

## 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.  Reply With Quote

11. Member Join Date
Mar 2012
Posts
13
Rep Power
0

## Re: Need help to write the code 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  Reply With Quote

12. ## Re: Need help to write the code Originally Posted by Galichka Java 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,a,a to 1. ...](mistaken)
you should continue it upto a //useless comment I made
Edited to add: "this makes only a,a,a to 1." sorry, it makes a,a,a to '0' that's the code's mistake
regards
dhilip
Last edited by noobplus; 03-27-2012 at 02:49 PM.  Reply With Quote

13. ## Re: Need help to write the code 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  Reply With Quote

14. ## Re: Need help to write the code

hi,
for debugging mode, make it like this..
Java 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  Reply With Quote

15. ## Re: Need help to write the code

hi,
i edited my post#12

regards
dhilip  Reply With Quote

16. ## Re: Need help to write the code

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.  Reply With Quote

17. Member Join Date
Mar 2012
Posts
13
Rep Power
0

## 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 : )  Reply With Quote

18. ## 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  Reply With Quote

19. ## 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.  Reply With Quote

20. ## Re: Need help to write the code

rather, U can make each value to '0' and change them to 1 in both directions...

Java Code:
```public class massiv {

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

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
Java Code:
```01111110
00111100
00011000
00000000
00000000
00011000
00111100
01111110```
regards
dhilip
Last edited by noobplus; 03-27-2012 at 04:14 PM.  Reply With Quote

#### Posting Permissions

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