Search This Blog

Friday, 30 November 2012

How to make the numbers fall in console?

#include<iostream>
#include<windows.h>
using namespace std;
//Function to move the cursor.
void gotoxy(int x, int y)
{
HANDLE console_handle;
COORD cursor_coord;
cursor_coord.X=x;
cursor_coord.Y=y;
console_handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(console_handle,cursor_coord);
}

int main()
{
for (int row = 0; row<25; row++)
{
gotoxy(25,row);
cout<<"5";
Sleep(200);
gotoxy(25,row);
cout<<" ";
}
}
//=========================================================================
Now, it's easy if you want to change your column. Just add a simple condition.
I recommend, before you goto the following program, try to think, what the condition can be?

//Program to drop a number in different column.
#include<iostream>
#include<windows.h>
using namespace std;
//Function to move the cursor.
void gotoxy(int x, int y)
{
 HANDLE console_handle;
 COORD cursor_coord;
 cursor_coord.X=x;
 cursor_coord.Y=y;
 console_handle=GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleCursorPosition(console_handle,cursor_coord);
}
int main()
{
 int column = rand() % 80; //80 because Total columns are 0-79. i.e. less than 80.
 for (int row = 0; row<25; row++)
 {
  gotoxy(column,row);
  cout<<"5";
  Sleep(200);
  gotoxy(column,row);
  cout<<" ";
  //Now we add another condition so that our number again comes at first row and a different
  if (row == 24) // last line of console screen.
  {
   row = 0;
   column = rand() % 80;
  }
 }
}
//========================================================================

Similarly you can increase the number of Numbers falling downward.
//Program for falling two numbers in changing columns.
#include<iostream>
#include<windows.h>
using namespace std;
//Function to move the cursor.
void gotoxy(int x, int y)
{
 HANDLE console_handle;
 COORD cursor_coord;
 cursor_coord.X=x;
 cursor_coord.Y=y;
 console_handle=GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleCursorPosition(console_handle,cursor_coord);
}
int main()
{
 int column = rand() % 80; //80 because Total columns are 0-79. i.e. less than 80.
 int column1 = rand() % 80;
 for (int row = 0; row<25; row++) 
 {
  gotoxy(column,row);
  cout<<"2";//first number.
  gotoxy(column1,row);
  cout<<"9";
  Sleep(200);
  gotoxy(column,row);
  cout<<" ";
  gotoxy(column1,row);
  cout<<" ";
  //Now we add another condition so that our number again comes at first row and a different
  if (row == 24)
  {
   row = 0;
   column = rand() % 80;
   column1 = rand() % 80;
  }
 }
}

Thursday, 29 November 2012

Variable Data Type: Bool in C++

Bool is a variable data type which can store either 1 or 0.


#include<iostream>
using namespace std;
int main()

{
bool j,k,m;
bool i = j = k = m = 0;
cout<<j<<endl<<i<<endl<<k<<endl<<m<<endl;
return 0;
}
//===============================================================================


#include<iostream>
using namespace std;
int main()

{
bool j,k,m;
bool i = j = k = m = 1;
cout<<j<<endl<<i<<endl<<k<<endl<<m<<endl;
return 0;
}
//===============================================================================

Monday, 26 November 2012

Program to make a word appear for 1 second and then it disappears.


#include <iostream>
#include <windows.h>
using namespace std;
 int main()


 {
cout<<"  a";
Sleep(1000);
cout<<"\b ";
cout<<endl;

return 0;
 }
//You can extend this to get your required result.

2-D arrays

Program to assign a value (say 5) to 1st column of 4 by 3 array. 4 = rows & 3 = columns.


#include <iostream>
using namespace std;
int main()
{
int Rehan[4][3]; // This is the way to declare 2-d array named Rehan.

for (int i = 0; i <=3; i++) //This loop performs the following assigning of values.
Rehan[i][0] = 5;
//Rehan[0][0] = 5;
//Rehan[1][0] = 5;
//Rehan[2][0] = 5;
//Rehan[3][0] = 5;

for ( i = 0; i<=3; i++)
cout<<Rehan[i][0]<<endl;
//if we don't use loop then we had to do following.
//cout<<Rehan[0][0]<<endl;
//cout<<Rehan[1][0]<<endl;
//cout<<Rehan[2][0]<<endl;
//cout<<Rehan[3][0]<<endl;

return 0;
}

Now lets make a program for the same number of rows and columns. Storing 5 in the second column of array and then printing  the whole array. Sounds Interesting, it's.

#include <iostream>
using namespace std; 
int main()
{
int Rehan[4][3]={0};
for (int i = 0; i <=3; i++)
Rehan[i][1] = 5; 
//What happens within the loop:
//Rehan[0][1] = 5;
//Rehan[1][1] = 5;
//Rehan[2][1] = 5;
//Rehan[3][1] = 5;

//Now we want to print whole of the array.
for (int rows = 0; rows<=3; rows++)
{
for (int columns = 0; columns <= 2; columns++)
cout<<Rehan[rows][columns]<<" ";
cout<<endl;
}
return 0;
}


Sunday, 25 November 2012

What is '&', lets clearify our concepts.



You can see a program at your right side.
It's simple.

Point to be noted: We say b = a;
When we increase b, the value of b will be increased but a will not change. Why? because b = a means that value of a will be stored in b. When we print 'a', the original value of a is printed.

In the second program, &b = a means that value of a is stored in b and "by changing b value of a will be changed".
In other words b and a correspond to same number (which is 1 initially).


Saturday, 24 November 2012

getch()


#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
cout<<"Hi"<<endl;
getch();         //Screen is paused till the user presses a button.
cout<<"By"<<endl;
}

=========================================================================================

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
cout<<"Hi"<<endl;
int a = getch();         //Screen is paused till the user presses a button and that pressed button's ASCII will be stored in a.
cout<<a;
}

Did you see how useful is it?
Once you have stored the ASCII value, you can compare it. Let me clarify what I mean.


#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
cout<<"Hi"<<endl;
int a = getch();         //Screen is paused till the user presses a button and that pressed button's ASCII will be stored in a.
if (a == 97)
cout<<"How dare you to press \"a\""<<endl;
else
if (a == 98)
cout<<"How dare you to press \"b\""<<endl;
else
cout<<"You pressed a key other than A and B."<<endl;
}



Friday, 23 November 2012

How to Change the Size of output screen?

Interesting code:


#include <windows.h>

main()
{
      while(true)
      {
          Sleep(100);
          SMALL_RECT WinRect = {0, 0, rand() % 100 + 1, rand() % 70 + 1};
          SMALL_RECT* WinSize = &WinRect;
          SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), true, WinSize);
      }
}

This code was for fun, isn't it cool?
Now execute the following code:


#include <windows.h>
main()
{
          SMALL_RECT WinRect = {0, 0, 30, 57 };// 30 is for changing screen horizontally and 57 for changing vertically.
          SMALL_RECT* WinSize = &WinRect;
          SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), true, WinSize);
}


You can get your desired output screen by changing 30 and 57.

Thursday, 22 November 2012

Wednesday, 21 November 2012

Sizes of int, float, double, unsigned, short, unsigned short, long long

In addition to the int and double types, C++ has several other numeric types.
     C++ has two floating-point types. The float type uses half the storage of the double type, but it can only store 6–7 digits. Many years ago, when computers had far less memory than they have today, float was the standard type for floating-point computations, and programmers would indulge in the luxury of “double precision” only when they needed the additional digits. Today, the float type is rarely used.
     By the way, these numbers are called “floating-point” because of their internal representation in the computer. Consider numbers 29600, 2.96, and 0.0296. They can be represented in a very similar way: namely, as a sequence of the significant digits—296—and an indication of the position of the decimal point. When the values are multiplied or divided by 10, only the position of the decimal point changes; it “floats”. Computers use base 2, not base 10, but the principle is the same.


Friday, 9 November 2012

How to generate Numbers automatically within a specific Range?

Lets say we want to print Numbers between 4 and 6, including 4 and 6.

#include<iostream>
using namespace std;
int main()

{
int x;
for ( x = 1 ; x <= 70 ; x++ ) // you can replace 70 with Any Number.
cout<< 4+   rand() % 3 <<endl; // to Produce Numbers between 4 & 6 including 4 and 6.

cout<<endl<<endl;
return 0;
}


Mod of 3 with any random number will give value less than 3 and greater than 0, and by adding any number from 0 to 2 with 4, we get either 4,5 or 6.

Simple!
C++: How to detect that user Pressed P button from keyboard? - Yahoo! Answers #include<iostream>
using namespace std;
int main()

{
int x;
for ( x = 1 ; x <= 70 ; x++ ) // you can replace 70 with Any Number.
cout<< 4+   rand() % 3 <<endl; // to Produce Numbers between 4 & 6 including 4 and 6.

cout<<endl<<endl;
return 0;
}


Mod of 3 with any random number will give value less than 3 and greater than 0, and by adding any number from 0 to 2 with 4, we get either 4,5 or 6.

Simple!

Monday, 5 November 2012

How to produce Completely Random Number


ctime is included to use Time.
And cstdlib (C Standard Library) is used for rand & srand.


Note: Without changing any thing, I executed my program twice, and it gave me different Output, So we get completely Random Numbers in this way.

Time is changing.




srand(time(0)), seed is changing so outcome is changing (variety of random Numbers are being generated

Difference between Rand & Srand


You can use rand without srand (but the problem will be that you'll get same output whenever you run your program). Srand is a "seed value". As you sow so shall you reap. A group of random numbers are generated with each particular value of srand. Whenever we use a specific value in srand, a specific group of numbers will be generated, and that specific group of values (so called random) can be obtained whenever we use that number in srand which we used previously.


Note: C Standard Library (cstdlib) has been included for using srand and rand statements.





















Q. What does it really mean if we use srand (5); in a program?

Ans: It means that a set of predefined operations will be carried out at 5, and then a number will be generated (using rand).
User can use this number. If called second time (using rand) again set of predefined operations will be performed to produce second number. These Operations are set in such a way that they produce random number.

Time in C++


Syntax: After #include<ctime> as header, write time(0) OR time(NULL) in the body of program.

The manpage states that, « time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). »

Sunday, 4 November 2012

Loops


Loop:

It's basically Repetition of one or more set of instructions (commands).

For Loop:



Both of the above programs give same output.

Friday, 2 November 2012

Basics

Comments:  Comments are for Programmer, he writes it in order to make his code understandable (since the code becomes complex often). C++ Compiler Ignores comments.
//Write the comment after two consective forward slashes (if your coomment is in single line)
/*Comments are written
in this way when they
occupy more than 1 line in C++ Compiler.*/


Output:

cout<<"Write here (within the inverted commas)whatever you want to see on the Screen when your Program Runs.";


cout<<"Write here whatever you want to see on the Screen when your Program Runs. "<< a; /*Prints the sentence within inverted commas as it is and then Prints the value stored in variable 'a'.*/
Usefull things for output:





Example:cout<<"\n\t I love \n\n";



Input: Prompts user to Enter Data.

cin>>Variable_Name;
Data can be entered in more than one variable:
cin>>first>>SeCond>>fifth;
(When your Program Runs) User enters something and Presses ENTER or SPACEBAR, whatever he writes before pressing Enter will be stored in variable named 'first'. User will again be prompted, he enters something and presses ENTER or SPACEBAR, that will be stored in variable named 'SeCond'. Similarly in variable fifth, the third value entered by the user will be stored.
NOTE: C++ is very strict in its CaSE  ==> 'You' & 'you' are two different variables.


Setw: Specify Number of Spaces.

In order to use this feature of C++, you'll have to include a header file in the beginning of your program source code.
#include<iomanip>
Then with in the program you can use Setw anywhere.

cout<<"I want"<<setw(20)<<"$";Note: $ sign is mixed with "Press any..."




How to seperate dollar sign from mixing to rest of line?
Simple, cout<<"I want"<<setw(20)<<"$\n\n";




This command needs small extra practice.


Setprecision: How many digits should appear after point (.) .

Also see the output by replacing fixed with scientific to know the difference.