Solved

c++ zigzag pattern setw

Posted on 2011-09-15
4
1,474 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

631 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