Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ question

Options
  • 04-08-2001 12:49am
    #1
    Registered Users Posts: 255 ✭✭


    Heres a question Im stuck on:

    How would I display a hollow box displayed below if i was to enter integer n as the n x n side, eg I enter 6 I get:
    ******
    * *
    * *
    * *
    * *
    ******

    I would appreciate any help with this.


Comments

  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Dave,

    here's da C++ code ta do this
    #include <iostream.h>
    
    void main()
    {
    	int n=0;
    
    	cout << "Enter a number: ";
    	cin >> n;
    
    	for(int counter=0; counter < n; counter++)
    		cout << '*';
    
    	cout << '\n';
    
    	for(counter=0; counter < n-2; counter++)
    		cout << "**\n";
    
    	for(counter=0; counter < n; counter++)
    		cout << '*';
    
    }
    

    ;-phobos-)


  • Registered Users Posts: 255 ✭✭hertz


    Cheers Weston,
    The box didnt come out properly , duuhh

    Get it from the C++ bible yeah.


  • Closed Accounts Posts: 1,193 ✭✭✭Kix


    Hey Phobos,

    I think you missunderstood what he wanted. If I can steal and modify your original code, I think this is what he wants:
    #include <iostream.h>
    void main()
    {
      int n=0;
      cout << "Enter a number: ";
      cin >> n;
    
      for( int counter=0; counter < n; counter++ ) 
        cout << '*';
    
      cout << '\n';
     
      for( counter=0; counter < n-2; counter++ )
      {
        cout << "*";
        // Spaces between sides of box...
        for( int counter2=0; counter2 < n-2; counter2++ )
          cout << " ";
        cout << "*\n";
      }
    
      for( counter=0; counter < n; counter++ )
        cout << '*';
      cout << "\n";
    }
    

    This should give the following (assuming fixed width font):
    ******
    *    *
    *    *
    *    *
    *    *
    ******
    
    


    K

    EDIT: Added trailing newline...



    [This message has been edited by Kix (edited 04-08-2001).]


  • Registered Users Posts: 255 ✭✭hertz


    Thanks kix,
    but i tested it and it dont work properly, i get this:
    ******
    *....*
    ******
    (just ignore dots in hollow of box)
    its to do with the nested for loop for the spaces in the box.

    Any other solutions would be of use big time,
    cheers people.


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Soz about that Dave, I wasn't sure what you wanted the result to look like. So I wrote it again, and this time I broke it down in to functions to make it easier to understand.
    #include <iostream.h>
    
    void doHollowLine(int width);
    void doFullLine(int width);
    
    void main()
    {
    	int n=0;
    
    	cout << "Enter a number: ";
    	cin >> n;
    
    	doFullLine(n);
    
    	// the (n - 2) is used throughout to remove the perimeter characters
    	for(int counter=0; counter < n - 2; counter++)
    		doHollowLine(n);
    
    	doFullLine(n);
    
    }
    
    void doHollowLine(int width)
    {
    	cout << '*';
    	for(int counter=0; counter < width-2; counter++)
    		cout << ' ';
    	cout << "*\n";
    }
    
    void doFullLine(int width)
    {
    	for(int counter=0; counter < width; counter++)
    		cout << '*';
    	cout << '\n';
    }
    

    It will draw the box @ the entered (width/height) (n) value.

    ;-phobos-)
    PS: And No Dave, I didn't get it from Programmers Bible. I got it from 1st year muppets guide to structured programming with C++ systax!!. ;p

    [This message has been edited by phobos (edited 05-08-2001).]


  • Advertisement
  • Closed Accounts Posts: 1,193 ✭✭✭Kix


    <font face="Verdana, Arial" size="2">Originally posted by Kix:
    ...assuming fixed width font...</font>

    I stand by my code. smile.gif



  • Closed Accounts Posts: 218 ✭✭Void


    Please refrain from posting your homework on this board. Not that anyone would do such a thing...


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Void,

    I know the rules state that posting "homework" on the boards is not allowed. But I have to admit, that I like helping ppl when I can, and go against the idea of banning it.

    n00b programmers like Hertz who is just starting out, needs all the help he can get. Also n00b coders will be posting very small programming problems such as the example in this thread, which isn't really much to ask. Also I think when it comes to programming, it is beneficial for other coders to see what other ppl are doing and help out where possible. I'm in my FY of BSc. in CS. So helping and teaching n00bs will be great experience for me, so @ the end of the day everybody wins.

    I feel this is a valid argument and would like to hear your feedback. smile.gif

    ;-phobos-)


  • Closed Accounts Posts: 1 GennyzsakSam


    Hi All,
    here's another solution I'd prefer. It's still don't bother proportional font styles, but I think it's a bit improved.
    of course it could be modified to be real general (without maximum with I didn't take care).

    #include <iostream.h>

    int main(int argc, char* argv[])
    {
    static char szSpace[] = " ";
    static char szStar[] = "************************************";

    int ciLen = sizeof( szSpace );
    int n=0;

    cout << "Enter a number: ";
    cin >> n;

    cout << (szStar + ciLen - n - 1) << endl;

    for (register int i=0; i < n-2; ++i )
    {
    cout << '*' << (szSpace + ciLen - n + 2 - 1)<< '*' << endl;
    }

    cout << (szStar + ciLen - n - 1) << endl;

    return 0;
    }


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    <font face="Verdana, Arial" size="2">Originally posted by phobos:
    Void,

    I know the rules state that posting "homework" on the boards is not allowed. But I have to admit, that I like helping ppl when I can, and go against the idea of banning it.
    </font>

    I'm with Void. I have no problem helping someone but you have moderate between making them work out the answer and actually giving them the complete answer.

    For example, he said "Here is a question I am stuck on" but doesn't cite any code. Only the finished thing.

    In this instance I can only assume that he hasn't tried to create the code himself.

    Now if he had quoted some of his code it would of been a different matter.

    Kix's code is actually correct, and hertz pointing out it's wrong proves he has no idea what that code is doing.

    In that instance I wouldn't bother posting a complete solution but slightly answer thier question to see if they can solve it themselves.




  • Advertisement
  • Closed Accounts Posts: 1,193 ✭✭✭Kix


    I suppose that I'm a bit thick. I never thought it might be homework.

    K


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    TBH honest I don't know why he couldn't get Kix's or My code to work, coz they both work. I have compiled both of them here and they work. So it must be down to how he's using the compiler.

    Dave you are using MSVC++ 6.0, yeah?. You do realise you must create a workspace for your project (ie this program) an then run it inside that. You can't simply open a blank ASCII template, insert your code, and try to compile it.
    <font face="Verdana, Arial" size="2">
    Now if he had quoted some of his code it would of been a different matter.
    </font>

    Yeah OK, I agree with that. But here is a suggestion.

    [SUGGESTION]
    Since there are so many rules on these boards as to what you can and can't post. Why don't you send a list of them with the users login name and password combination upon subscription. This way n00bs will take more time in constructing valid posts.
    [/SUGGESTION]

    I know it is common sense not to post "homework" with out some sort of effort being made by the original poster. But I'm sure if hertz knew that, he would have posted his code. I know for a fact that he attempted this himself, before he originally posted.

    Christ why is everything becoming so political lately!. It used to about the posts MAN!, now it's about pollitically correct cyber EGOs(c) phobos 2001. tongue.gif

    ;-phobos-)


  • Closed Accounts Posts: 218 ✭✭Void


    Sorry for the late reply, I've been busy.

    We do NOT have loads of rules on this board. Not that I know of anyway. I have never moderated a post on this board (despite temptation), so don't try to depict me as a fascist. Come to think, I don't even know why I'm a moderator. But anyway, what Hobbes said is right (even if he is a twat).

    Hertz posted a problem and Phobos posted the answer. This was the wrong approach. Phobos, you obviously took some time (obviously minimal, coz he is so l33t) to concoct an informative response. The response in question was a bit too informative. Phobos posted no explanation, just code, demonstrating a complete lack of understanding regarding the educational process. Nobody has ANY problem with anybody trying to help someone else. However, you should offer tips and hints rather than the entire solution.

    Learning how to program is a difficult, frustrating process, but the hours spent grappling with insurmountable enigmas are ultimately worthwhile.

    I should really lock this post now, but I couldn't be bothered. In future if I see blatant homework postings, I will act appropriately. There are commercial websites out there that let kids post their homework for free fúcking answers.


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    I can agree with every single post in this thread, but once again I have a BUT! (and it's not contradictory)

    I know I should have commented and took a more tutorial based approach. But I agree that I could have waffled on for ages about developing and analysing code. So in that case I'm not going to do ppl's homework.

    Ah crap!, I came in here to argue a point and now through some sort of mindphuck process I totally agree with you. Right I don't think we should argue any more about this subject (all is clear). But don't close it, because hertz may want to take a different approach @ asking further questions.

    ;-phobos-)


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    <font face="Verdana, Arial" size="2">Originally posted by Void:
    But anyway, what Hobbes said is right (even if he is a twat).</font>

    Oh gee thanks void. What did I do to annoy you?


  • Registered Users Posts: 255 ✭✭hertz


    My explanation:
    Im a new to programming, And the question I asked was based on a exam question. I did attempt it before posting on the boards, I just felt that putting on to much useless code would simply take up to much room. And thanks to phobos and kix for their help, Im sorry for snapping back by saying your code didnt work. I was the stupid one for not using visual studio properly as I never do, I use Linux to program on and that dosent have any of that project workspace stuff.
    And I didnt know not to post blatant questions like that on the boards so sorry there.
    This is a cool site and I didnt mean to cause what I did.

    Hertz.


Advertisement