Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is a homework question I got for my programming course at school, and I'm kind of lost, so please help. Here is the question:

Write an application program that prints a diamond of asterisks (*) on the screen. The number of lines in the diamond is given by the user. As an example, if the user requested a diamond with 7 lines, the following would be displayed.

Here is what I have so far:

{
  int nlines;
  int nsp;
  cout << "enter the number of lines (must be more then one)" << endl;
  cin >> nlines;
  while((nlines)<=0)
  {
    cout << "enter the number of lines (must be more then one)" << endl;
    cin >> nlines;
  }
  nsp=(nlines - 1)/2;
  for(int currspace = 1; currspace <= nsp; currspace++)
  {
    cout << " ";
  }
  cout << "*" << endl;
  for( int currentline = 0; currentline < nlines; currentline++)
  {
    for( int currentaster = 0; currentaster <= currentline; currentaster++)
      cout << "*";
    cout << endl;
  }
  return 0;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
349 views
Welcome To Ask or Share your Answers For Others

1 Answer

I won't try to give a complete answer since it's homework, but my immediate advice would be to try to break the task down into some sub-tasks, each of which is somewhat simpler. For example, I'd start with a small function that did nothing but print out one line of asterisks of a specified length with a specified justification. From the looks of things, you can probably handle that pretty easily, and one you have it, the rest is quite a bit more straightforward.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...