Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program to draw a spiral rotating clockwise at the center of the screen

Posted By: Easy Tutor     Category: C++ Programming     Views: 8959

Write a program to draw a spiral rotating clockwise at the center of the screen. The spiral should comprise of digits 0 to 9, starting from 0 incrementing by one.

Starting direction (0=left, 1=up, 2=right, 3=down)
Length of the spiral

Draw the spiral on the screen. Your output should be on screen of size 25 x 80.

Code for Program to draw a spiral rotating clockwise at the center of the screen in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <stdlib.h>
 # include <string.h>
 # include <conio.h>

 # define LEFT  0
 # define UP    1
 # define RIGHT 2
 # define DOWN  3

 constchar get_digit(constchar);


 int main( )
    {
       clrscr( );

       fstream File("CP-06.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Last_digit='0';
       char Input[85]={NULL};
       char Screen[25][80]={NULL};

       int direction=0;
       int length_of_spiral=0;

       File.getline(Input,80);

       direction=atoi(Input);

       strset(Input,NULL);

       File.getline(Input,80);

       length_of_spiral=atoi(Input);

       int i=0;
       int j=0;
       int k=1;
       int x=12;
       int y=39;
       int number_of_digits_in_a_direction=1;

       for(i=0;i<25;i++)
      {
         for(j=0;j<80;j++)
        Screen[i][j]='.';
      }

       Screen[x][y]=Last_digit;

       switch(direction)
      {
         case LEFT  : y-=2;
              break;

         case UP    : x-=2;
              break;

         case RIGHT : y+=2;
              break;

         case DOWN  : x+=2;
              break;
      }

       do
      {
         for(i=0;i<4;i++)
        {
           for(j=0;j<number_of_digits_in_a_direction;j++)
              {
             Screen[x][y]=get_digit(Last_digit);
             Last_digit=Screen[x][y];
             k++;

             if(j==(number_of_digits_in_a_direction-1))
                {
                   direction++;

                   if(direction==4)
                  direction=0;
                }

             if(k==length_of_spiral)
                break;

             switch(direction)
                {
                   case LEFT  : y-=2;
                        break;

                   case UP    : x-=2;
                        break;

                   case RIGHT : y+=2;
                        break;

                   case DOWN  : x+=2;
                        break;
                }
              }

           if(k==length_of_spiral)
              break;

           if(i==1)
              number_of_digits_in_a_direction++;
        }

         if(k==length_of_spiral)
        break;

         number_of_digits_in_a_direction++;
      }
       while(1);

       window(40,13,41,14);
       textbackground(BLUE);
       textcolor(WHITE);
       cprintf("0");

       window(1,1,1,1);

       for(i=0;i<25;i++)
      {
         for(j=0;j<80;j++)
        cout<<Screen[i][j];
      }

       getch( );
       return 0;
    }

 /*************************************************************************///------------------------  get_digit(const char)  ----------------------///*************************************************************************/constchar get_digit(constchar Last_digit)
    {
       char Next_digit=NULL;

       switch(Last_digit)
      {
         case'0' : Next_digit='1';
            break;

         case'1' : Next_digit='2';
            break;

         case'2' : Next_digit='3';
            break;

         case'3' : Next_digit='4';
            break;

         case'4' : Next_digit='5';
            break;

         case'5' : Next_digit='6';
            break;

         case'6' : Next_digit='7';
            break;

         case'7' : Next_digit='8';
            break;

         case'8' : Next_digit='9';
            break;

         case'9' : Next_digit='0';
            break;
      }

       return Next_digit;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to draw a spiral rotating clockwise at the center of the screen is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!