Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "diamond" problem

Reply
Thread Tools

"diamond" problem

 
 
jasson118@hotmail.com
Guest
Posts: n/a
 
      03-01-2007
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.

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-01-2007
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
 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      03-01-2007

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/


 
Reply With Quote
 
jasson118@hotmail.com
Guest
Posts: n/a
 
      03-01-2007
#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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-01-2007
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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-01-2007
> 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'
 
Reply With Quote
 
jasson118@hotmail.com
Guest
Posts: n/a
 
      03-01-2007

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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-01-2007
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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-01-2007
wrote:
> #include "stdio.h"
> #include "math.h"


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

--
Ian Collins.
 
Reply With Quote
 
jasson118@hotmail.com
Guest
Posts: n/a
 
      03-01-2007

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57