building a spiral

i got stuck in building a spiral she has to be between 1 and 19 if i press 1 iget a spiral 4X5
this is what i managed to do:
if (option==2)
printf("Enter the size of the spiral (1<=N<=19):");
scanf("%d",&N);
if(N>19 || N<1)
printf ("Input incorrect! Please try again.\n");
else { N=4*N;
for (row=0;row!=N;row++) {
for (collum=0;collum!=N+1;collum++)
printf("*");
printf("\n");
}
If I'm not wrong you want to print out an hourglass of *s? Try changing collum=0 to collum=row
Do you want to make an ASCII spiral??

* *******
* * *
* * *** *
* * * * *
* * * *
* ***** *
* *
*********

this is what I'm supposed to get for pressing 2 (there's a problem of showing the spiral here its suppose to look like turebalance)
Last edited on
You mean it supposed to look like:

********
-*******
--******
---*****
----****
-----***
------**
-------*
?
If yes, you should consider the space every line, increase it by 1. To print a space you should write: printf(" "); putting it in for loop:
1
2
for(int i=0; i< counter; i++)
   printf(" ");


The counter initialized as zero and increase every time by one.
Pay attention also to the number of characters in every line, it decreases by one.

What do you mean by its size is between 1 and 19 ? The number of lines maybe?
Last edited on
no it suppose like a spiral you know when you get hypnotized
Ohh.. I like what you're doing!
What I drew here isn't punctual and clear, but check the spaces see how it changes in every line and how to set asterisks

______**
____*
__*___****
_*__*______*
*__*___*_*__*
*___*__*__*__*
_*___*___*___*
__*____*____*
___*_______*
_____*____*
_______**

I suggest for you easier thing, what about using X,Y axes?
Then you can consider the center of the spiral as (0,0) and divide X,Y axes to 4 parts, draw every part alone as quarters of circles...
Last edited on
Hy easyz!
So it was an ASCII spiral.
Here's a small code that makes a quarter circle.I knopw you wanted a spiral, but a spiral is just a circle whit variable radius.You can change the program to fit your needs.
The idea is that the user gives the radius and then the program makes a quarter of a circle of R points.For example, if R=10, then the program will make a quarter circle of 10 points.
The coordinates are calculated in cartezian
system, not polar, because polar isn't suited.And the x coordinates are always 1 apart.That means that for R=10, the x coordinates are 0,1,2,3,4,5,6,7,8,9.So they are equally distanced.But the y ones are not of course.Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  int R=0; // the radius
  int i=0;
  int j=0;
  int yc[100]; // the y coordinates, the x coordinates are i as it increments
  int dy[100]; // the distance between the y coordinates, to know how many endl are between them

  cout<<"Introduce the radius of the spiral"<<endl;
  cin>>R;

  dy[0]=0; // the first difference was 0

  cout<<endl<<"The y coordinates"<<endl; // just to see the values calculated
  for(i=0;i<=R;i++) // calculating y coordinates, as you can see i represents x
  {
    yc[i] = (int) sqrt( R*R - i*i );
    cout<<yc[i]<<",";
  }

  cout<<endl;
  cout<<"The dy coordinates"<<endl; // to see the values calculated

  for(i=0;i<R;i++) // calculating the difference between 2 consecutive y coordinates
  {
    dy[i+1] = yc[i] - yc[i+1];
    cout<<dy[i]<<",";
  }

  cout<<endl;

  for(i=0;i<R;i++) // displaying the quarter circle
  {
    for(j=0;j<=i;j++) // adding the spaces before the *
    {
      cout<<"   "; // there were more spaces added because one doesn't seem enough
    }
    cout<<"*"; // ading the *

    for(j=0;j<=dy[i];j++) // adding the empty lines to create a hight
    {
      cout<<endl;
    }
  }


  return 0;
}


You probably will need to adjust the number of empty spaces and of new lines(end of lines) you display.
For example if you want 2 more empty spaces just ad 2 to i.Like this:
1
2
3
4
for(j=0;j<=i + 2;j++) // adding the spaces before the *
{
   cout<<"   "; // there were more spaces added because one doesn't seem enough
}

The program does have some errors sometimes, maybe those aren't errors but are related to the fact that the screen is limited.For small R it work pretty well.

Anyway, why such a weird program??What do you need for??
Console graphics is very limited.Don't waste your time on it.Instead, use that time to learn a GUI library such WxWidgets, FLTK, gtkmm, even QT.That is if you don't already nkow one and your are doing this program as an experiment.
Last edited on
Hi,
I'm joining his question, I too need to build an ascii spiral with no arrays :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
* ***********************
* *                     *
* * ******************* *
* * *                 * *
* * * *************** * *
* * * *             * * *
* * * * *********** * * *
* * * * *         * * * *
* * * * * ******* * * * *
* * * * * *     * * * * *
* * * * * * *** * * * * *
* * * * * * * * * * * * *
* * * * * *   * * * * * *
* * * * * ***** * * * * *
* * * * *       * * * * *
* * * * ********* * * * *
* * * *           * * * *
* * * ************* * * *
* * *               * * *
* * ***************** * *
* *                   * *
* ********************* *
*                       *
*************************


this is 6 order spiral. it needs to be untill 19'th oreder.
I know I can't keep the information of what to draw and what not. so I need to find a formula or a special series that does that.
any help ?
what's the basic logic in building that ? how do I start ?
Topic archived. No new replies allowed.