Results 1 to 15 of 15

Thread: help me out

  1. #1
    pranesh is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default help me out

    1. Write a program to produce a similar output for n characters, where n is a odd number. The below output is for n=7.
    Java Code:
    A B C D E F G F E D C B A
    A B C D E F     F E D C B A
    A B C D E           E D C B A
    A B C D                 D C B A
    A B C                       C B A
    A B                             B A
    A                                   A
    Last edited by pranesh; 04-26-2014 at 01:36 PM. Reason: added [code] ... [/code] tags

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

    Default Re: help me out

    I have never seen Donald Duck drawn that badly ... please fix it so we can see what the output is supposed to be.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: help me out

    Hi,

    I understood that you want this output:

    printing n characters and then the first n-1 in backwards
    printing n-1 characters and then 1*2 blanks and then n-2 characters.
    ...
    printing n-c characters then 1*n blanks and then n-(c+1) characters backwards
    and so on till c is n-1

    So is this the algorithm? Maybe the way how I wrote it down can help you to automate it with a loop.
    Maybe try printing numbers first instead of characters ...

    With kind regards,

    Konrad

  4. #4
    XeRoFlaWz is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: help me out

    It seems like he is trying to print out the shape of a pyramid. Nested for loops, using a range of characters. Such that the output looks like

    Java Code:
    a  b  c  d  e  f  g
    a  b  c      e  f  g
    a  b             f  g
    a                   g
    So it's the outline of a pyramid.

  5. #5
    XeRoFlaWz is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: help me out

    Yeah, that's impossible to get right.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: help me out

    Assuming you aren't using recursion, it only takes a single loop. Check out the StringBuilder class. There are some methods there that could be useful.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    pranesh is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: help me out

    Quote Originally Posted by kneitzel View Post
    Hi,

    I understood that you want this output:

    printing n characters and then the first n-1 in backwards
    printing n-1 characters and then 1*2 blanks and then n-2 characters.
    ...
    printing n-c characters then 1*n blanks and then n-(c+1) characters backwards
    and so on till c is n-1

    So is this the algorithm? Maybe the way how I wrote it down can help you to automate it with a loop.
    Maybe try printing numbers first instead of characters ...

    With kind regards,

    Konrad
    Sir i could able to understand the concept but i couldnt able to write down the code properly that is the reason i asked for help. Can u help me with the code

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: help me out

    If you have the concept that might be a starting point for working on the code.
    Can you list the steps the program must take to write out the characters on the first line?
    And the same for the second line
    etc to the last line.

    Now look at the steps for each line and see what the pattern is. What changes from line to line.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    pranesh is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: help me out

    import java.io.*;

    class Alpha{
    public static void main(String args[])throws IOException
    {
    DataInputStream in=new DataInputStream(System.in);
    int n;
    System.out.println("enter the pattern value");
    n=Integer.parseInt(in.readLine());
    int a=n+64;
    for(int h=0;h<n;h++)
    {
    for(int i=65;i<=a;i++){
    System.out.printf("%c",i);
    System.out.printf("\t");
    }
    System.out.print("");
    for(int j=a;j>=65;j--)
    {
    System.out.printf("%c",j);
    System.out.printf("\t");
    }
    System.out.println("");
    a--;
    }
    }}

    can help resolve it for me

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: help me out

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

    Please post the program's output so we can see what it does.
    I don't see any comments so I don't know what the sections of code are trying to do.

    The code would be more understandable if 'A' were used in place of the magic number 65
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    pranesh is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: help me out

    Java Code:
    import java.io.*;
    class Alpha{
    public static void main(String args[])throws IOException
    {
    DataInputStream in=new DataInputStream(System.in);
    int n;
    System.out.println("enter the pattern value");
    n=Integer.parseInt(in.readLine());
    int a=n+64;
    for(int h=0;h<n;h++)
    {
    for(int i=65;i<=a;i++){
    System.out.printf("%c",i);	
    System.out.printf("\t");
    }	
    System.out.print("");
    for(int j=a;j>=65;j--)
    {
    System.out.printf("%c",j);	
    System.out.printf("\t");
    }
    System.out.println("");
    a--;
    }
    }}
    Required output
    Java Code:
    A B C D E F G F E D C B A
    A B C D E F     F E D C B A
    A B C D E            E D C B A
    A B C D                   D C B A
    A B C                          C B A
    A B                                 B A
    A                                        A
    Last edited by Norm; 04-26-2014 at 04:05 PM. Reason: added code tags for output

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: help me out

    The code has lost all its formatting. The nested statements should be indented to show the logic.

    Where are the comments describing what each section of code is to do?
    What is the variable: a used for? The name of a variable should describe what the variable holds.

    You've posted the Required output.
    What is the current output from the program?

    The output needs to be wrapped with code tags to preserve the embedded spaces.
    Last edited by Norm; 04-26-2014 at 01:47 PM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: help me out

    Hi,

    the output seems to be the current output. At least that is what I go when I was running the code.

    So one of the missing points are the spaces in the middle. You used tab instead of spaces which is quite good.
    So what you have right now is the outer loop which prints the lines. Inside you have 2 loops that are writing the characters.
    Between these 2 loops you have to write some tabs. The first run through the loop (h is 0) you have to write no tabs. Then 2 characters are gone so next time (h=1) you have to write 2 tabs. Then 4 characters are gone and you have to write 4 tabs.
    So do you see a possible logic to get the number of tabs printed?

    With kind regards,

    Konrad

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: help me out

    Why are you printing out empty strings? (e.g. "").

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: help me out

    One is to get the newline. It'd be nice if there were comments to see why the code does what it does.
    If you don't understand my response, don't ignore it, ask a question.

Posting Permissions

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