"diamond" problem

J

jasson118

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.
 
J

John Harrison

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
 
G

Grizlyk

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/
 
J

jasson118

#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
 
J

John Harrison

#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
 
J

John Harrison

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
 
J

jasson118

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
 
J

John Harrison

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
 
G

Gernot Frisch

#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;
}
}
 
J

jasson118

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;
}
 
D

Diego Martins

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 :(
 
D

David Harmon

On 1 Mar 2007 07:20:04 -0800 in comp.lang.c++, (e-mail address removed)
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top