Tuesday 13 August 2013

Moving Fan

Moving Fan:

In this post of shapes in c you will be able to construct the code for a moving fan in C++ using "*".

Code:

#include<iostream>
#include<Windows.h>
using namespace std;
void fan1(const int size);
void fan2(const int size);
int main()
{
int size, time;
cout<< "Enter the size of fan" << endl;
cin>>size;
cout<< "For how many seconds you want to switch on the fan" << endl;
cin>>time;
system("cls");
for(int i=0 ; i<2*time ; i++)
{
fan1(size);
Sleep(500);
system("cls");
fan2(size);
Sleep(500);
system("cls");
}
return 0;
}
void fan1 (const int size)
{
for(int i=0 ; i<=size ; i++)
{
for(int j=0 ; j<=i ; j++)
{
if(i!=size)
{
if(j==0 || j==i)
cout<< "* ";
else
cout<<"  ";
}
else if (i==size )
cout<< "* ";
}
for(int spaces=i ; spaces<size ; spaces++)
cout<<"  ";
for(int j=i ; j<=size ; j++)
{
if(i==0)
cout<< "* ";
else 
if(j==i || j==size)
cout<<"* ";
else
cout<<"  ";
}
cout<<endl;
}
for(int m=0 ; m<=size ; m++)
{
for(int spaces1=m ; spaces1<size ; spaces1++)
cout<<"  ";
for(int n=0; n<=m ; n++)
{
if(m==size)
cout<<"* ";
else 
if(n==0 || n==m)
cout<<"* ";
else 
cout<<"  ";
}
for(int spaces1=0 ; spaces1<m ; spaces1++)
cout<<"  ";
for(int n=m; n<=size; n++)
{
if(m==0)
cout<<"* ";
else
if(n==m || n==size)
cout<<"* ";
else 
cout<<"  ";
}
cout<<endl;
}
}
void fan2 (const int size)
{
for(int i=0 ; i<=size ; i++)
{
for(int j=0 ; j<i ; j++)
cout<<"  ";
for(int spaces=i ; spaces<=size ; spaces++)
{
if(i==0)
cout<<"* ";
else
if(spaces==i || spaces==size)
cout<<"* ";
else 
cout<<"  ";
}
for(int j=i ; j<size ; j++)
cout<<"  ";
for(int j=0; j<=i ; j++)
{
if(i==size)
cout<<"* ";
else 
if(j==0 || j==i)
cout<<"* ";
else 
cout<<"  ";
}
cout<<endl;
}
for(int m=0 ; m<=size ; m++)
{
for(int spaces1=m ; spaces1<=size ; spaces1++)
{
if (m==0)
cout<<"* ";
else
if(spaces1==m || spaces1==size)
cout<<"* ";
else
cout<<"  ";
}
for(int n=0; n<m ; n++)
cout<<"  ";
for(int spaces1=0 ; spaces1<=m ; spaces1++)
{
if(m==0 || m==size)
cout<<"* ";
else
if(spaces1==0 || spaces1==m)
cout<<"* ";
else 
cout<<"  ";
}
cout<<endl;
}
}




5 comments:

  1. First of all it shows 22 errors
    after adding .h after iostream still it shows 10 errors
    "Unable to open Windows.h
    using 7th version of turbo C++
    Please Correct it

    ReplyDelete
    Replies
    1. We didn't checked it on Turbo C++. This is a code for Virtual studio versions. We'll check it and soon update you with it!!! :)

      Delete
  2. oh, wow It is working now
    I have visual studio 2008 I have run this program on it and it is working now
    Thank You so much
    Please keep these programs coming
    :)

    ReplyDelete
    Replies
    1. I am glad that the code of moving fan worked on your side :)
      Of course, we'll keep on coming with new shapes in c and c tutorial for beginners!!!

      For latest version of visual studio, visit this page:

      http://shapesinc.blogspot.com/2013/09/c-compiler-visual-studio-2012.html

      Delete
    2. Can you send me the alogrithm of this code plz

      Delete