Solved

c++ zigzag pattern setw

Posted on 2011-09-15
4
1,376 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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you all for your help.
0
 

Expert Comment

by:DK88
Comment Utility
How could this program be stopped by entering Ctrl+C?
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

763 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

12 Experts available now in Live!

Get 1:1 Help Now