Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Exercise problem

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exercise problem

    Hello .. I would like to help with this exercise. I think it has to do with recursion .... but I do not have a clear idea.

    A magic carpet of order n (0 ≤ n ≤ 5 ) and parameter k (0 ≤ k <9 ) `
    and a 3^n× 3^n matrix , to binary values ​​, defined inductively as follows:
    • if n = 0 ( 1 × 1 matrix ) , then the only element of the array is 1 ;
    • if n> 0 (matrix 3^n × 3^n ) , consider a subdivision of the matrix in 9
    blocks of size 3^n- 1 × 3^n- 1 each. Consider numbered from 0 to 8
    the blocks , in this way :
    0 1 2
    3 4 5
    6 7 8
    The block number j (0 ≤ j <9 ) `
    and in turn
    - A block of elements all equal to zero , if j = k ;
    - A magic carpet of order n - 1 and parameter k , if j = k .
    You want to create a Java program that data nek print a representation
    text of a magic carpet of order n and parameter k .
    Input format . The input `
    and structured in the following way :
    • a line containing two integers n and k , separated by a space.
    Assume that nek satisfying constraints 0 ≤ n ≤ 5, 0 ≤ k <9 .
    Output Format . You should print a magic carpet of order n parameter
    k , according to the following code:
    • the values ​​equal to 0 are represented by the character ' . ' ;
    • the values ​​equal to 1 are represented by the character ' * ' .
    The values ​​in the same row are not separated by spaces. Each row of the matrix is
    and
    completed by the usual newline ' \ n ';


    for example if I choose n = 2 and k = 4 this print

    *********
    *.**.**.*
    *********
    ***...***
    *.*...*.*
    ***...***
    *********
    *.**.**.*
    *********

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    Quote Originally Posted by Norm View Post
    import java.io.*;
    import java.util.*;




    class Tappeto_Magico {
    *int[][] m;
    *int k;
    *int num=1;
    *Tappeto_Magico(int n,int k){
    * * *this.m=new int[(int)Math.pow(3,n)][(int)Math.pow(3,n)];
    * * *this.k=k;
    *}
    void matrice(int n,int k,int i1,int j1,int h){
    * * if(n==0){
    * * * *m[0][0]=0;
    * * * *return;
    * * }else{
    * * if(n==1){
    * * * *int cas=0;
    * * * * *for(int i=i1;i<3;i++){
    * * * * * *for(int j=j1;j<3;j++){
    * * * * * * * *if(cas==k){
    * * * * * * * * m[i][j]=0; **
    * * * * * * * *}
    * * * * * * * *cas++;
    * * * * * *}
    * * }
    * * }else{
    * * * * for(int g=0;g<9;g++){
    * * * * * * int cas1=0;
    * * * * * * for(int i=i1;i<m.length/(Math.pow(3,h));i++){
    * * * * * *for(int j=j1;j<m.length/(Math.pow(3,h));j++){
    * * * * * * * *if(cas1==g){
    * * * * * * * * * *m[i][j]=0;
    * * * * * * * *}
    * * * * * * * *cas1++;
    * * * * * * *
    * * * * * *}
    * * * * * *}
    * * * * * * *matrice(n-1,k,i1,j1,++h);
    * * * * * * }
    * * }
    * * }
    }
    void stampa(){
    * * * *for(int i=0;i<m.length;i++){
    * * * * * *for(int j=0;j<m.length;j++){
    * * * * * * * *System.out.print(m[i][j]);
    * * * * * *}
    * * * * * * System.out.print("\n");
    * * * * * *}
    }
    *public static void main(String []args) throws IOException{
    * * BufferedReader br=new BufferedReader( new InputStreamReader(System.in));
    * * String s=br.readLine();
    * * StringTokenizer st=new StringTokenizer(s);
    * * int n=Integer.parseInt(st.nextToken());
    * * int k=Integer.parseInt(st.nextToken());
    * *Tappeto_Magico t=new Tappeto_Magico(n,k);
    * *t.matrice(n,k,0,0,1);
    * *t.stampa();
    * *
    * **
    } * *
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Remove all the leading *s on each line, but preserve the indentation of the nested statements.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    <import java.io.*;
    import java.util.*;
     
    class Magic_Carpet {
     int[][] m;
     int k;
     int num=1;
     Magic_Carpet(int n,int k){
         this.m=new int[(int)Math.pow(3,n)][(int)Math.pow(3,n)];
         this.k=k;
     }
    void matrix(int n,int k,int i1,int j1,int h){
        if(n==0){
           m[0][0]=0;
           return;
        }else{
        if(n==1){
           int cas=0;
             for(int i=i1;i<3;i++){
               for(int j=j1;j<3;j++){
                   if(cas==k){
                    m[i][j]=0;   
                   }else{
                       m[i][j]=1;
                   }
                   cas++;
               }
        }
        }else{
            int cas1=0;
            int rig=0;
            int col=0;
            for(int g=0;g<9;g++){         
                for(int i=i1;i<m.length/(Math.pow(3,h));i++){
               for(int j=j1;j<m.length/(Math.pow(3,h));j++){
                   if(cas1==g){
                       m[i][j]=0;
                   }
                   cas1++;
               }
               }
                rig++;
                col++;
                 if(rig==3){
                  j1=j1*2;
              }
              if(col==3){
     
              }
                 matrix(n-1,k,i1,j1,++h);
                }
        }
        }
    }
    void print(){
           for(int i=0;i<m.length;i++){
               for(int j=0;j<m.length;j++){
                   System.out.print(m[i][j]);
               }
                System.out.print("\n");
               }
    }
    public static void main(String []args) throws IOException{
    BufferedReader br=new BufferedReader( new InputStreamReader(System.in));
    String s=br.readLine();
    StringTokenizer st=new StringTokenizer(s);
    int n=Integer.parseInt(st.nextToken());
    int k=Integer.parseInt(st.nextToken());
    Magic_Carpet t=new Magic_Carpet(n,k);
    t.matrix(n,k,0,0,1);
    t.print();   
    }    
    }
    >

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    Can you explain what your problem is?
    If the output is wrong, post the output and add some comments saying what is wrong with the output.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    I try to write the algorithm to print the magic carpet using recursion ... but I lose indices .... I thought that for the initial matrix 3 ^ n I have to cut it into 9 parts and then each part in other 9 shares and so on and on each submatrix I have to check if the block is equal numbered ako not.
    printing of my code just makes me the basic step . I press
    111000000000000000000000000
    101000000000000000000000000
    111000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000
    000000000000000000000000000

    this with n=3 and k=4!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    The code posted in post#5 does not compile without errors and can not be executed for testing.
    Please post the same code that you are working with. The code in post#3 and post#5 are different!!!
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    now i work with post#5...

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    OK.
    For easier and faster testing, hardcode the input values like this:
    String s= "3 4"; //br.readLine();             //<<<<<<<<
    That would make sure that we are all using the same data for testing.

    You need to post what the program's output should look like.


    The code is poorly formatted. The nesting is bad, the { and } don't line up to show nesting levels making the code very hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    <import java.io.*;
    import java.util.*;
     
    class Magic_Carpet {
    int[][] m;
    int k;
    int num=1;
    Magic_Carpet(int n,int k){
    this.m=new int[(int)Math.pow(3,n)][(int)Math.pow(3,n)];
    this.k=k;
    }
    void matrix(int n,int k,int i1,int j1,int h){
    if(n==0){
    m[0][0]=0;
    return;
     }else{
     if(n==1){
     int cas=0;
      for(int i=i1;i<3;i++){
       for(int j=j1;j<3;j++){
        if(cas==k){
        m[i][j]=0;   
        }else{
        m[i][j]=1;
        }
        cas++;
        }
       }
      }else{ 
      int cas1=0;
      int rig=0;
      int col=0;
      for(int g=0;g<9;g++){         
       for(int i=i1;i<m.length/(Math.pow(3,h));i++){
        for(int j=j1;j<m.length/(Math.pow(3,h));j++){
        if(cas1==g){
        m[i][j]=0;
        }
        cas1++;
        }
       }
       rig++;
       col++;
       if(rig==3){
       j1=j1*2;
       }
       if(col==3){
       }
       matrix(n-1,k,i1,j1,++h);
       }
    }
     for(int i=0;i<m.length;i++){
      for(int j=0;j<m.length;j++){
     System.out.print(m[i][j]);
      }
     System.out.print("\n");
     }
    }
    public static void main(String []args) throws IOException{
    BufferedReader br=new BufferedReader( new InputStreamReader(System.in));
    String s=br.readLine();
    StringTokenizer st=new StringTokenizer(s);
    int n=Integer.parseInt(st.nextToken());
    int k=Integer.parseInt(st.nextToken());
    Magic_Carpet t=new Magic_Carpet(n,k);
    t.matrix(n,k,0,0,1);
    t.print();   
    }    
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    That formatting is not any better. The }s should be in the same column vertically below the start of the line with the { it pairs with.

    The main() method is not formatted at all.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    I try to post it in the right way. My problem is that I need the solution within 12 hours .... so please help me because they do not really know how to do ... I do not know if my code is completely wrong or not ... so if you have a solution to the problem could post?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    Too many statements are not indented properly. Only the first and last part of the class should be in the first column. Others should be indented.
    Statements inside {} should be indented 3-4 spaces.

    What should the output look like?

    The code has lots of computations and decisions and there are no comments saying why the code does what it does. Without some comments describing what the statements in the program are trying to do, it will be hard to find anyone that will try to work with this code.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    do you mean by power? What is not clear? the exercise asks you to print the magic carpet with power n and k we have chosen that would be the square of the matrix numbered ... I posted my code to figure out where I'm wrong but maybe I have it all wrong then ignoring my code look for a solution for this exercise.... thanks!

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    ignoring my code look for a solution for this exercise.
    Sorry, I won't be able to do work on your project, I have projects of my own that I'd rather do.

    I'll try to help you get your program to work, but you'll have to do a bit more.
    Like show what the output is supposed to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    Thank you anyway. The output is an array that contains dots or zeros according to box k. The solution that I thought I was taking my first block of the matrix 3 ^ n then the first matrix 3 ^ n-1 (the first of the 9 sub-matrices) and then on it recursively call their 9 sub-matrices and so on ... but I have difficulty in writing the code.
    for example, if I choose n = 2 and k = 4, the output is the following matrix:

    *********
    *.**.**.*
    *********
    ***...***
    *.*...*.*
    ***...***
    *********
    *.**.**.*
    *********

    dots must be below each asterisk but because of the character they are not!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    Where does the program print the *s and the .s? I don't see any in the code.

    The output you posted was all 1s and 0s. The program did not print any *s or any .s
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise problem

    I'm sorry! I forgot to say, but the point is equal to 0 instead of the asterisk is equal to 1!


    in my first post there are all the details!

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise problem

    That makes it harder to understand when the desired output doesn't look anything like the actual output. Why didn't your desired output have 0s and 1s?

    Start with the actual output. What is wrong with the first line that is printed? What should be on the first line?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Exercise
    By keepStriving in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 06:58 PM
  2. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 09:40 PM
  3. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM
  4. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM
  5. Replies: 4
    Last Post: May 15th, 2011, 06:03 AM