Thread: Nested loop Character box

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    8

    Nested loop Character box

    Hi i need a little help on a program im working on it ask the user for a keyboard character and a number of rows and columns
    to look like this

    RRRRR
    R___R
    R___R
    R___R
    RRRRR
    and also this in another program
    rrrrrr
    rrrrrr
    rrrrrr
    rrrrrr
    I currently have
    <html><body>
    <script type="text/javascript">


    // Declare variables and constants
    var userInputText; // user input to go
    var numRow; // number of the rows
    var NumCol; // number of the columns
    var ES = ""; // empty string




    userInputText = prompt("Enter a keyboard character",ES);
    numRow = prompt("Enter the number of rows",ES);
    NumCol = prompt("Enter the number of columns",ES);


    //need a nested loop to draw the character box




    </script>
    </body>
    </html>

    if any one can help me finish this program i would appreciate it

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    I found this if some one could help me covert it to fit into my script above id appreciate it


    void printsquare ( int width, int height, char c )
    {
    int i;
    int j;

    for ( i = 0; i < width; i++ )
    putchar ( c );
    putchar ( '\n' );
    for ( i = 1; i < height; i++ ) {
    putchar ( c );
    for ( j = 1; j < width - 1; j++ )
    putchar ( ' ' );
    printf ( "%c\n", c );
    }
    for ( i = 0; i < width; i++ )
    putchar ( c );
    putchar ( '\n' );
    }

    int main ( void )
    {
    printsquare ( 5, 5, '*' );

    return 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this supposed to be C or Javascript?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Is this supposed to be C or Javascript?
    javascript

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. Thread moved to Tech Board.

    Anyway, instead of just finding code online and trying to convert it, why don't you spend some effort to understand the algorithm and then implement it in Javascript?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Right. Thread moved to Tech Board.

    Anyway, instead of just finding code online and trying to convert it, why don't you spend some effort to understand the algorithm and then implement it in Javascript?
    im not trying to become a programmer i just needed some help converting it i understand simple javascript this character box is just very complexed and since it looked so similar to javascript i thought someone could help me fix it real quick

    srry but this was the closest site i found to getting help with javascript
    Last edited by golf_fanatic; 04-25-2012 at 09:56 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by golf_fanatic
    im not trying to become a programmer
    That is besides the point. Pretty much all students learn mathematics at some level, but obviously "I'm not trying to become a professional mathematician" is a lame excuse for a student trying to avoid doing maths homework himself/herself.

    Quote Originally Posted by golf_fanatic
    i just needed some help converting it i understand simple javascript this character box is just very complexed and since it was similiar to c i thought someone could help me fix it real quick
    Is the code that you are trying to write supposed to be C or Javascript? Is this conversion the objective of your assignment?

    I mean, I don't need the C code to implement the Javascript code, and I don't need the Javascript code to implement the C code, so really, the "conversion" is not important, unless that is the objective of the assignment. I suspect that the objective of the assignment is to get you to come up with an algorithm to solve this problem, then implement that algorithm. To this end you could look at existing code in another programming language that does something similiar, but you should seek to understand, not convert.

    Quote Originally Posted by golf_fanatic
    srry but this was the closest site i found to getting help with javascript
    Dunno, most people find this place for C or C++ rather than Javascript.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    i rlly dont know why i even took the class its not required in my business degree i just always wanted to know a little bit of programming so i took pricncliples of programming i believed i accomplished that
    this is the only assignment i can not figure out
    I was suppose to write a program in java script that ask for the user for a keyboard character and a number of rows and a number of columns
    to accomplish this
    rrrrr
    r__r
    r__r
    rrrrr
    but the inside should be blank

    so if u could bold all the items i need to change in the C++ code ill try and covert it my self and ill resumbit it here so u can check it
    Code:
    int i;
    int j;
    
    
    for ( i = 0; i < width; i++ )
    putchar ( c );
    putchar ( '\n' );
    for ( i = 1; i < height; i++ ) {
    putchar ( c );
    for ( j = 1; j < width - 1; j++ )
    putchar ( ' ' );
    printf ( "%c\n", c );
    }
    for ( i = 0; i < width; i++ )
    putchar ( c );
    putchar ( '\n' );
    }
    
    
    int main ( void )
    {
    printsquare ( 5, 5, '*' );
    
    
    return 0;
    }
    Last edited by golf_fanatic; 04-25-2012 at 10:20 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by golf_fanatic
    I was suppose to write a program in java script that ask for the user for a keyboard character and a number of rows and a number of columns
    Ah, so you're writing Javascript.

    The basic idea is that given r rows and c columns for a character x, assuming that both r and c are at least 2:
    Code:
    Loop c times
        Print x
    Print a new line character
    Loop (r - 2) times
        Print x
        Loop (c - 2) times
            Print a space
        Print x
        Print a new line character
    Loop c times
        Print x
    [Optionally, print a new line character]
    (I use the indentation to denote the body of the loop.)
    Notice that the idea of (r - 2) and (c - 2) is that you are printing a line before and a line after the outer loop, or the character x before and after the inner loop, so you need to account for them.

    Note that with respect to HTML, you will likely need to print within <pre></pre> tags in order for the spaces and blank lines to be significant.
    Last edited by laserlight; 04-25-2012 at 10:40 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Ah, so you're writing Javascript.

    The basic idea is that given r rows and c columns for a character x, assuming that both r and c are at least 2:
    Code:
    Loop c times
        Print x
    Print a blank line
    Loop (r - 2) times
        Print x
        Loop (c - 2) times
            Print a space
        Print x
        Print a blank line
    Loop c times
        Print x
    (I use the indentation to denote the body of the loop.)
    Notice that the idea of (r - 2) and (c - 2) is that you are printing a line before and a line after the outer loop, or the character x before and after the inner loop, so you need to account for them.

    Note that with respect to HTML, you will likely need to print within <pre></pre> tags in order for the spaces and blank lines to be significant.
    thx for explaining it for me
    i got this so far in java script i just need to change the non workign variables into my variables i believe
    Code:
    {
    int i;
    int j;
    
    
    for (i = 0; i < width; i++)
    System.out.print(c);
    System.out.print('\n');
    for (i = 1; i < height; i++)
    {
    System.out.print(c);
    for (j = 1; j < width - 1; j++)
    System.out.print(' ');
    System.out.printf("%c\n", c);
    }
    for (i = 0; i < width; i++)
    System.out.print(c);
    System.out.print('\n');
    }
    
    
    int main()
    {
    printsquare(5, 5, '*');
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by golf_fanatic
    i got this so far in java script i just need to change the non workign variables into my variables i believe
    Err... System.out.print? I recall document.write for Javascript.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    lol thats my fault the converter did that and i just changed it
    Code:
    for (i = 0; i < width; i++)
    document.write(c);
    document.write('\n');
    for (i = 1; i < height; i++)
    {
    document.write(c);
    for (j = 1; j < width - 1; j++)
    document.write(' ');
    document.write("%c\n", c);
    }
    for (i = 0; i < width; i++)
    document.write(c);
    document.write('\n');
    }
    k now i just got to get it working using my variables i believe

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sigh, I really don't support this "conversion" business. It is totally the wrong way to do homework, and it will kill you in a test/exam. Understand the algorithm, don't just convert code blindly.

    By the way, you should indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    by the way this was the final for my programming course and if i got the program working i got a 100 on my final i just emailed my instructor and he will help me finish the making the code work so thank you for all your time u wasted on me
    basically this was my last assignment i will ever do involving programming

    in case u wanted to see what i got so far now
    Code:
    {
    for (i = 0; i < numCol; i++)
    document.write(c);
    document.write('\n');
    for (i = 1; i < numRow; i++)
    {
    document.write(c);
    for (j = 1; j < numCol - 1; j++)
    document.write(' ');
    document.write("%c\n", c);
    }
    for (i = 0; i < numCol; i++)
    document.write(c);
    document.write('\n');
    }
    since you had the ability to move my thred you can go aheadand close it if u want i pretty much got enough work done for a passing grade so far just gonna try for the 100
    Last edited by golf_fanatic; 04-25-2012 at 11:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  2. nested for loop
    By aromash in forum C Programming
    Replies: 4
    Last Post: 10-25-2010, 02:41 AM
  3. Help About Nested Loop
    By Antigloss in forum C Programming
    Replies: 16
    Last Post: 10-17-2006, 10:18 AM
  4. Nested for loop
    By gjack72 in forum C++ Programming
    Replies: 4
    Last Post: 10-26-2005, 12:44 PM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM

Tags for this Thread