Better sqaure?

This is a discussion on Better sqaure? within the C++ Programming forums, part of the General Programming Boards category; I have created a function that draws a square based on user input. Is there a better way or any ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Better sqaure?

    I have created a function that draws a square based on user input.
    Is there a better way or any ways I can make the code smaller but still work as
    efficently?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    // function prototype
    int draw ( int );
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       int n;
       
       cout << "Enter a side value: ";
       cin >> n;
       
       draw ( n );
       
       cin.get();  // freeze console window
       cin.ignore();
    	 
       return 0;   // indicate program ended sucsessfuly
    }
    
    // function draws a square
    int draw ( int n )
    {
       int counter = 0;
       
       do // makes sure the hieght is correct
       {
          for ( int i = 1; i <= n; i++ ) // deals with length
          {
             cout << "*";
          }
          
          cout << "\n";
          
          counter++;
          
          
       } while ( counter < n ); 
       
       return n;
    }

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    maybe you can, but I don't think it will make the program significantly faster... at least not to a level where you can see the difference...

    but if you are really eager on making it faster, go
    Code:
    gcc -S source.c
    if you are using gcc, and in the current directory, there should be a file called source.s ....
    learn a bit of assembly, and you may be able to improve it

    here's how it looks
    Code:
            .file   "test.c"
            .section        .rodata
    .LC0:
            .string "%d"
            .text
    .globl main
            .type   main, @function
    main:
            pushl   %ebp
            movl    %esp, %ebp
            subl    $24, %esp
            andl    $-16, %esp
            movl    $0, %eax
            addl    $15, %eax
            addl    $15, %eax
            shrl    $4, %eax
            sall    $4, %eax
            subl    %eax, %esp
            subl    $8, %esp
            leal    -12(%ebp), %eax
            pushl   %eax
            pushl   $.LC0
            call    scanf
            addl    $16, %esp
            movl    $0, -8(%ebp)
            movl    $0, -4(%ebp)
            jmp     .L2
    .L5:
            subl    $12, %esp
            pushl   $42
            call    putchar
            addl    $16, %esp
            leal    -4(%ebp), %eax
            incl    (%eax)
    .L4:
            movl    -12(%ebp), %eax
            cmpl    %eax, -4(%ebp)
            jl      .L5
            subl    $12, %esp
            pushl   $10
            call    putchar
            addl    $16, %esp
            movl    $0, -4(%ebp)
    .L2:
            movl    -12(%ebp), %eax
            cmpl    %eax, -8(%ebp)
            setl    %dl
            leal    -8(%ebp), %eax
            incl    (%eax)
            testb   %dl, %dl
            jne     .L4
            movl    $0, %eax
            leave
            ret
            .size   main, .-main
            .ident  "GCC: (GNU) 4.0.3 20051201 (prerelease) (Debian 4.0.2-5)"
            .section        .note.GNU-stack,"",@progbits

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Thanks for the reply, I think I will give that a shot

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Could do something like this:

    Code:
    #include <string>
    
    // [...]
    
    void square( int num )
    {
    	std::string row( num, '*' );
    
    	for ( int i=0; i<num; i++ )
    		std::cout<< row << '\n';
    }
    don't forget to
    Last edited by twomers; 10-17-2006 at 10:24 AM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    That would probably be less efficient, because of the overhead of creating a string class.

    Code:
    void print_square(int size) {
        for(int y = 0; y < size; y ++) {
            for(int x = 0; x < size; x ++) {
                cout << '*';
            }
            cout << endl;
        }
    }
    I don't see how printing a square in the manner you have done would slow your program down unless you're using a really slow computer. It's likely to be the screen itself that's causing any delay that you can see; if you want to avoid this, try ANSI escape sequences or ncurses.

    [edit]
    don't forget to
    Don't forget to end the "don't forget to" sentence! [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I've forgotten ... I think I should leave it there until I remember. I didn't read the 'and more efficient' part. Just the part with shorter code.

    I'll test it when I get home to see which is quicker.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You probably won't be able to measure the speed differences between those programs. 0 ms and 0 ms. It would be better to look at the generated assembly code and compare them (by length, if you don't know assembly). Add the -S option to GCC.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Whoops,

    using

    Code:
    void square( int num )
    {
    	std::string row( num, '*' );
    
    	for ( int i=0; i<num; i++ )
    		std::cout<< row << '\n';
    }
    
    void print_square(int size) {
        for(int y = 0; y < size; y ++) {
            for(int x = 0; x < size; x ++) {
    			std::cout << '*';
            }
    		std::cout << '\n'; // changed from endl to '\n' for testin'
        }
    }
    
    int main( void )
    {
    	double dwks_square, twomers_square;
    
    	TimeIt t;
    
    	t.StartTimer();
    	for ( int i=0; i<1000; i++ )
    		print_square( 10 );
    	t.EndTimer();
    
    	dwks_square = t.GetTime();
    
    	
    	t.StartTimer();
    	for ( int i=0; i<1000; i++ )
    		square( 10 );
    	t.EndTimer();
    	
    	twomers_square = t.GetTime();
    
    	std::cout<< "dwks average: " << dwks_square/1000 << '\n';
    	std::cout<< "twomers average: " << twomers_square/1000 << '\n';
    
    	return 0;
    }
    with Date & Time, they're both (dwks' and my functions) very close in speed. ~0.00001 seconds different in one test.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simplifing Square Roots
    By LiNeAr in forum C++ Programming
    Replies: 11
    Last Post: 10-01-2005, 08:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21