Solved

c++ zigzag pattern setw

Posted on 2011-09-15
4
1,436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

756 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