Solved

c++ zigzag pattern setw

Posted on 2011-09-15
4
1,385 Views
Last Modified: 2013-03-08
Hello, i'm stuck ones again.... the code should generate a zigzag something like this

Please enter the maximum field width: 9
Enter the microseconds to pause between asterisks: 50000
*
 *
  *
   *
    *
     *
      *
       *
        *
       *
      *
     *
    *
   *
  *
 *
*
 *
  *
   *
    *
     *
      *
       *
        *
       *
      *
     *
    *
   *
  *
 *
*
 *
  *
   *
    *
     *
      *
       *
        *
       *
      *
     *
    *
   *
  *
 *
*
^C
my code below kinds of works but i don't know what i'm doing wrong any help would be great thanks

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main(void)
{

  int width;
  int interval;
  cout <<"Please enter the maximum field width: ";
  cin>> width;
  cout <<"Enter the micrososeconds to pause between asterisks: ";
  cin >> interval;

int n =1;

while (n <=width)
{
   cout <<(setw(n)) << "*" <<endl;
   n++;

}

while (true)

{

for(n =1; n<=width; n++)
cout << setw(n) <<"*" <<endl;

for(n=width; n > 1; n--)
cout << setw(n) <<"*" <<endl;
usleep(interval);


}

}

Open in new window

0
Comment
Question by:businessesatoz
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 36545343
Well, the only thing that is wrong is that you are printing the 'topmost' star twice, so when counting doen, start at 'width - 1', i.e.
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main(void)
{

  int width;
  int interval;
  cout <<"Please enter the maximum field width: ";
  cin>> width;
  cout <<"Enter the micrososeconds to pause between asterisks: ";
  cin >> interval;

int n =1;

while (n <=width)
{
   cout <<(setw(n)) << "*" <<endl;
   n++;

}

while (true)

{

for(n =1; n<=width; n++)
cout << setw(n) <<"*" <<endl;

for(n=width - 1; n > 1; n--) // <-- here
cout << setw(n) <<"*" <<endl;
usleep(interval);


}

return 0; // that should be added as well
}

Open in new window

0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 250 total points
ID: 36547320
You have not explained your problem properly.
When I run your program, I get the following o/p:

Please enter the maximum field width: 5
Enter the micrososeconds to pause between asterisks: 100000
*
 *
  *
   *
    *
*
 *
  *
   *
    *
   *
  *
 *
*
I see that, in the beginning, you are having one slope and the opposite slope is not there.
After that, everything is ok.
If this is the problem, then remove the following lines (from line#18 to 23 in your post):
 
while (n <=width)
{
   cout <<(setw(n)) << "*" <<endl;
   n++;

}

Open in new window


And your code runs forever.
I think, you are doing that on purpose.
0
 

Author Closing Comment

by:businessesatoz
ID: 36558071
thank you all for your help.
0
 

Expert Comment

by:DK88
ID: 38967524
How could this program be stopped by entering Ctrl+C?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Compile GLUT with Visual Studio 2015 1 108
best sources to up-to-date in C++? 8 70
Unable to start eclipse ? 17 128
What is sub-make ? 2 38
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now