Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree5Likes

Thread: Need help to write the code

  1. #1
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default 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[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
    Last edited by Norm; 03-27-2012 at 02:13 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help to write the code

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

  4. #4
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: Need help to write the code

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

  5. #5
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default 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

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help to write the code

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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default 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
    Norm likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default 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.

  11. #11
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help to write the code

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

  12. #12
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Need help to write the code

    Quote Originally Posted by Galichka View Post
    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[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
    Last edited by noobplus; 03-27-2012 at 02:49 PM.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help to write the code

    Quote Originally Posted by Galichka View Post
    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
    noobplus likes this.
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default 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

  15. #15
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Need help to write the code

    hi,
    i edited my post#12

    regards
    dhilip

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default 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.
    noobplus likes this.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default 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 : )

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default 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.
    Galichka likes this.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default 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[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
    Java Code:
    01111110
    00111100
    00011000
    00000000
    00000000
    00011000
    00111100
    01111110
    regards
    dhilip
    Last edited by noobplus; 03-27-2012 at 04:14 PM.
    Galichka likes this.

Page 1 of 2 12 LastLast

Similar Threads

  1. What are you using to write your code?
    By CaptainMorgan in forum New To Java
    Replies: 996
    Last Post: 09-27-2014, 02:32 AM
  2. Got the idea but cannot write in code... help me
    By u4722839 in forum New To Java
    Replies: 11
    Last Post: 09-09-2011, 03:33 AM
  3. Write XML-Code to console with SAX
    By enne87 in forum XML
    Replies: 6
    Last Post: 11-21-2010, 01:43 AM
  4. how do i write this code?
    By Libertyman in forum New To Java
    Replies: 23
    Last Post: 06-22-2010, 12:43 PM
  5. How To Write Unmaintainable Code
    By tim in forum Forum Lobby
    Replies: 2
    Last Post: 02-07-2010, 09:19 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
  •