"diamond" problem

Discussion in 'C++' started by jasson118@hotmail.com, Mar 1, 2007.

  1. Guest

    i am a newber to C++ and have trouble with one of the problem from the
    book.
    can anyone able to use nested loops to display a diamond shape with "
    * ".
    *
    ***
    *****
    *******
    *********
    *******
    *****
    ***
    *
    minimize the number of printf function calls. one printf function call
    is allowed to display only one star.
    , Mar 1, 2007
    #1
    1. Advertising

  2. wrote:
    > i am a newber to C++ and have trouble with one of the problem from the
    > book.
    > can anyone able to use nested loops to display a diamond shape with "
    > * ".
    > *
    > ***
    > *****
    > *******
    > *********
    > *******
    > *****
    > ***
    > *
    > minimize the number of printf function calls. one printf function call
    > is allowed to display only one star.
    >


    To get help on code, post the code you've written so far. It's very
    difficult to help without knowing anything about competent you are, and
    seeing some of your code is the easiest way to judge that.

    I sure you wouldn't want someone to just give you the answer. You learn
    more if you have to do some work yourself.

    john
    John Harrison, Mar 1, 2007
    #2
    1. Advertising

  3. Grizlyk Guest

    wrote:
    >
    > i am a newber to C++ and have trouble with one of the problem from the
    > book. can anyone able to use nested loops to display a diamond shape


    Let you write here code that you have written and the code do not work as
    you want.

    --
    Maksim A. Polyanin
    http://grizlyk1.narod.ru/cpp_new

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
    Grizlyk, Mar 1, 2007
    #3
  4. Guest

    #include "stdio.h"
    #include "math.h"
    int main()
    {
    int line,spaces,stars,loop;

    for (line=1; line<=5; line++)
    spaces=int abs(line-5);
    for (loop=1; loop<=spaces; loop++)
    printf(" ");
    stars=line+(line-1);
    for (loop=1; loop<=stars; loop++)
    printf("*");
    return 0;
    }

    I did the first 5 lines and try to debugg it but doesn't work
    , Mar 1, 2007
    #4
  5. wrote:
    > #include "stdio.h"
    > #include "math.h"
    > int main()
    > {
    > int line,spaces,stars,loop;
    >
    > for (line=1; line<=5; line++)
    > spaces=int abs(line-5);
    > for (loop=1; loop<=spaces; loop++)
    > printf(" ");
    > stars=line+(line-1);
    > for (loop=1; loop<=stars; loop++)
    > printf("*");
    > return 0;
    > }
    >
    > I did the first 5 lines and try to debugg it but doesn't work
    >
    >


    OK, that's pretty close. The thing you missing is that you need to put
    the second for loop inside the first for loop. Now maybe you thought
    that is what you need, or maybe you weren't quite sure if it was
    necessary. Ether way this small change (adding { and } )will help

    for (line=1; line<=5; line++)
    {
    spaces=int abs(line-5);
    for (loop=1; loop<=spaces; loop++)
    printf(" ");
    stars=line+(line-1);
    for (loop=1; loop<=stars; loop++)
    printf("*");
    }

    Still not quite right because you never output a newline, but I'm sure
    you can figure that one out.

    john
    John Harrison, Mar 1, 2007
    #5
  6. > the second for loop inside the first for loop. Now maybe you thought
    > that is what you need,


    typo

    'what you did' NOT 'what you need'
    John Harrison, Mar 1, 2007
    #6
  7. Guest

    spaces=int abs(line-5); this line has a type 'int' unexpected Error
    , Mar 1, 2007
    #7
  8. wrote:
    > spaces=int abs(line-5); this line has a type 'int' unexpected Error
    >


    Change to

    spaces=abs(line-5); this line has a type 'int' unexpected Error

    john
    John Harrison, Mar 1, 2007
    #8
  9. Ian Collins Guest

    wrote:
    > #include "stdio.h"
    > #include "math.h"


    These should be <stdio.h> and <math.h>.

    --
    Ian Collins.
    Ian Collins, Mar 1, 2007
    #9
  10. Guest

    Compiling...
    P1.CPP
    Linking...
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol
    _WinMain@16
    Debug/LAB4.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    Creating browse info file...

    LAB4.exe - 2 error(s), 0 warning(s)

    i got the above message after i fixed the code
    , Mar 1, 2007
    #10
  11. wrote:
    > Compiling...
    > P1.CPP
    > Linking...
    > LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol
    > _WinMain@16
    > Debug/LAB4.exe : fatal error LNK1120: 1 unresolved externals
    > Error executing link.exe.
    > Creating browse info file...
    >
    > LAB4.exe - 2 error(s), 0 warning(s)
    >
    > i got the above message after i fixed the code
    >


    That's because you are using your compiler incorrectly.

    Specifically you are compiling a Windows program, when you need to tell
    you compiler to compile a 'console' program. You do this when you create
    the project, so start again, creating a new project.

    I'm feeling generous this time, but questions about how to use a
    compiler are off topic on this group. We only talk about the C++
    language here. In future ask such questions on a Windows programming group.

    john
    John Harrison, Mar 1, 2007
    #11
  12. <> schrieb im Newsbeitrag
    news:...
    > #include "stdio.h"
    > #include "math.h"
    > int main()
    > {
    > int line,spaces,stars,loop;
    >
    > for (line=1; line<=5; line++)
    > spaces=int abs(line-5);
    > for (loop=1; loop<=spaces; loop++)
    > printf(" ");
    > stars=line+(line-1);
    > for (loop=1; loop<=stars; loop++)
    > printf("*");
    > return 0;
    > }


    since this is C++, not C, I suggest:

    #include <string>
    #include <algorithm>
    int main(int, char**)
    {
    const int w=6;
    for(int i=0; i<=w; ++i)
    {
    int j= i>w/2 ? w-i : i;
    std::string spc, star;
    spc.resize(w/2-j,' ');
    star.resize(j*2+1, '*');
    std::cout << spc << star << std::endl;
    }
    }
    Gernot Frisch, Mar 1, 2007
    #12
  13. Guest

    the completed solution:

    #include "stdio.h"
    #include "math.h"
    int main()
    {
    int line,spaces,stars,loop;

    for (line=1; line<=5; line++)
    {
    spaces= abs(line-5);
    for (loop=1; loop<=spaces; loop++)
    printf(" ");
    stars=line+(line-1);
    for (loop=1; loop<=stars; loop++)
    printf("*");
    printf("\n");

    }
    for (line=1; line<=4; line++)
    {
    spaces= line;
    for (loop=1; loop<=spaces; loop++)
    printf(" ");
    stars=9-2*spaces;
    for (loop=1; loop<=stars; loop++)
    printf("*");
    printf("\n");
    }
    return 0;
    }
    , Mar 1, 2007
    #13
  14. On Mar 1, 3:44 am, wrote:
    > i am a newber to C++ and have trouble with one of the problem from the
    > book.
    > can anyone able to use nested loops to display a diamond shape with "
    > * ".
    > *
    > ***
    > *****
    > *******
    > *********
    > *******
    > *****
    > ***
    > *
    > minimize the number of printf function calls. one printf function call
    > is allowed to display only one star.


    bah! I though the topic was about diamond inheritance :(
    Diego Martins, Mar 1, 2007
    #14
  15. David Harmon Guest

    On 1 Mar 2007 07:20:04 -0800 in comp.lang.c++,
    wrote,
    >the completed solution:

    [snip]
    But hardly the minimum number of printf().
    Merge the two main loops into one line<=9,
    taking advantage of the abs(line-5),
    and using the correct stars=9-2*spaces.
    David Harmon, Mar 1, 2007
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler

    virtual inheritance / dreaded diamond problem

    Alexander Stippler, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    1,870
    Alexander Stippler
    Jul 14, 2003
  2. Tony Johansson

    the diamond problem

    Tony Johansson, Aug 17, 2005, in forum: C++
    Replies:
    5
    Views:
    397
  3. Pallav singh

    Template -- Diamond ring Problem

    Pallav singh, Dec 27, 2007, in forum: C++
    Replies:
    6
    Views:
    528
    Pavel Shved
    Dec 27, 2007
  4. thomas
    Replies:
    5
    Views:
    593
    Gert-Jan de Vos
    Nov 27, 2009
  5. sharmamandy
    Replies:
    0
    Views:
    987
    sharmamandy
    Sep 10, 2010
Loading...

Share This Page