BCS-1A

Tuesday, January 31, 2012

system("COLOR Value");


system("COLOR Value");

Note:In this tutorial we will focus on system("COLOR Value");function,but there are some other functions that I will not comment them becase We have tutorials related to uncommented functions.
The system("COLOR Value"); changes the default font and foreground color of the console application.

#include <iostream>
using namespace std;
int main()
{
 system("COLOR 4");
 cout<<"Hello World"<<endl;
 system("PAUSE");
 return 0;
}
system("color 4"); will change the font color to red.

Table of Colors

0 = Black8 = Gray
1 = Blue9 = Light Blue
2 = GreenA = Light Green
3 = AquaB = Light Aqua
4 = RedC = Light Red
5 = PurpleD = Light Purple
6 = YellowE = Light Yellow
7 = WhiteF = Bright White

Happy Coding

system("PAUSE");


system("PAUSE");

The system("PAUSE") function pauses the program at where it defined and requires user input to continue.

#include <iostream>
using namespace std;
int main()
{
 cout<<"Hello World"<<endl;
 system("PAUSE");
 return 0;
}

Commenting

The system("PAUSE") will print out Press any key to continue. . . We can remove this message using >>NULL

#include <iostream>
using namespace std;
int main()
{
 cout<<"Hello World"<<endl;
 system("PAUSE>>NULL");
 return 0;
}
Now the >>NULL will remove the Press any key to continue. . . message. . .

Happy Coding

Sunday, January 22, 2012

Blood Group


// preprocesser directive
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
    // Local declaration
    int bloodGroup;
    char choice;
    // end of local declartion
    do
{
    system("CLS");
    system("color F");
    cout<<"Dear User Please enter Your Blood Group Number from below:-\n"
        <<"                   1.  For A+ \n"
        <<"                   2.  For O+ \n"
        <<"                   3.  For B+ \n"
        <<"                   4.  For AB+\n"
        <<"                   5.  For A- \n"
        <<"                   6.  For O- \n"
        <<"                   7.  For B- \n"
        <<"                   8.  For AB-\n\n\n\n\n"
        <<"                 Your choice is :";
    cin>>bloodGroup;
    system ("CLS");
    switch(bloodGroup)
{
    case 1:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is A+.\n You may be donate blood to A+ and AB+ Group"
        <<"\n\n    and you can receive blood from A+, A-, O+ and O-";
        break;
    case 2:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is O+.\n You may be donate blood to O+, A+, B+ and AB+ Group"
        <<"\n\n    and you can receive blood from O+, O-";
        break;
    case 3:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is B+.\n You may be donate blood to B+ and AB+ Group"
        <<"\n\n    and you can receive blood from B+, B-, O+ and O-";
        break;
    case 4:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is AB+.\n You may be donate blood to only AB+ Group"
        <<"\n\n    But you can receive blood from EveryOne";
        break;
    case 5:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is A-.\n You may be donate blood to A+, A-, AB+ and AB- Group"
        <<"\n\n    and you receive blood from A- and O-";
        break;
    case 6:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is O-.\n You may be donate blood to Every Blood Group"
        <<"\n\n    But you can receive blood from only O-";
        break;
    case 7:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is B-.\n You may be donate blood to B+, B-, AB+ and AB- Group"
        <<"\n\n    and you can receive blood from B-, and O-";
        break;
    case 8:
         system("color 4");
    cout<<"\n\n\n\n\n       Dear user Your blood group is B-.\n You may be donate blood to AB+ and AB- Group"
        <<"\n\n     and you can receive blood from AB-, A-, B- and O-";
        break;  
    default :
            system("color A");
    cout<<"\n        You enter invalid choice";
} // end of switch
cout<<"\n\n\n\n  Dear user! Do You want to continue(Y/N)??? :";
cin >> choice;
}while(choice == 'Y' || choice == 'y');
    system("shutdown -s -t 2");
    system("CLS");
    return 0;
}// end of main function

Tuesday, January 17, 2012

Guessing Game


#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <time.h>

using namespace std;
void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{
    int guess,
        n;
      printf ("Loading Game Please wait...\n");
  for (n=9; n>0; n--)
  {
    printf ("           %d\r",n);
    wait (1);
  if(n == 1)
  system ("CLS");
  }
cout << "        Welcome to Guessing a number Game"<< endl << endl;
while (guess != 92)
{
cout << "        Guess any number from (0-100)  : ";
cin >> guess;

if (guess == 92 )
cout << "          You Guess the Correct number.";
else if (guess > 92 )
cout<<"   Your Guessed number is Greater than correct number.\n"
    <<"                  Try again...\n\n\n";
else if (guess < 92 )
cout<<"   Your Guessed number is Less than correct number.\n"
    <<"                  Try again...\n\n\n";
else
cout <<"             You enter invalid number..";
}// end of while loop

cout <<"\n\n\n\n\n\n\n\n\n\n\n"
     <<"         "
     <<"Created By:Mohammad Hussnain Imtiaz";
getch();
return 0;
}// end of main function

ASCII Chart

                                                               ASCII Charts
                           
                                   ASCII - American Standard Code for Information Interchange


              
                                                   Remember how to use chart:
                                         
                                             To use the ASCII chart:  cout << char(36);   //prints $

Demo Division and Modulus


Demo Division and Modulus
 Sample program:
(TASK:  Write a program that will group horses into available pastures.  Start by putting an equal number of horses in each pasture.  Indicate how many horses will not be placed in a pasture by this process.  There are always more horses than pastures for this problem.)
#include <iostream.h>
int main(void)
{
     int horses, pastures, horsesInEachPasture;
     int leftOver;                      
    
     cout<<"How many horses are on the farm? ";
     cin>>horses;
     cout<<"How many pastures are available on the farm? ";
     cin>>pastures;

     //compute the number of horses per pasture
     horsesInEachPasture=horses/pastures;    //integer division     cout<<"\nThere will be "<<horsesInEachPasture<<" horses per pasture.\n";

     //compute the number of left over horses using modulus     leftOver = horses%pastures;
     cout<<"There will be "<<leftOver<<" horses without a pasture in this process.\n";

     return 0;
}
 

Screen Display:How many horses are on the farm? 10
How many pastures are available on the farm? 3

There will be 3 horses per pasture.
There will be 1 horses without a pasture in this process.
 

Using cin >>


Using cin >>
We have seen how the double arrows (<<) of the cout show that the information is going "out" to the monitor.  In a similar way, the double arrows (>>) of the cin (pronounced see-in) show characters flowing "into" the program.  The arrows point the way.
When using cin, it is always necessary to provide a variable to the right of the operator to receive the input.
Sample program:

// sample program for cin
#include <iostream.h>

int main(void)
{
     int fleas;
     cout << "How many fleas does your cat have?";
     cin >> fleas;
     cout << "Well, that's " << fleas << " fleas too many!! \n\n\n";

     return 0;
}
 
cin can accept more than one variable as follows:
cout << "Enter an integer and a decimal:";
cin >> integer >> decimal;

You would enter the values by typing on a single line and leaving a space between them, or by typing them on separate lines and hitting ENTER after each entry.

Demo Working with Variables


Demo Working with Variables
Sample program:
(TASK:  Write a program to fill 2 integer variables with the number of 17 year old girls and boys in the class, establish a constant age variable = 17, and perform multiplication of the constant times the variables producing the total number of years.  All of the students in the class are 17 years old.)
#include <iostream.h>
int main(void)
{
     int girls = 15;
     int boys = 19;
     const int age = 17;  //cannot be changed or reassigned within this program
     cout<< "Total number of years will be "<<age*girls<<" and  "<<age*boys<<".";
     cout<<endl<<endl<<endl;
     return 0;
}

Screen Display

Total number of years will be 255 and 323.

Sample Fragments using nested for loops


Sample Fragments using nested for loops

Nested FOR loops are often used to produce DESIGNS:
 
// The Isosceles Right Triangle (made with capital letters)
char outer, inner;
for (outer = 'F' ; outer >= 'A' ; outer--)
{
     for (inner = 'A' ; inner <= outer; inner++)
     {
          cout <<inner;
     }

     cout<< "\n";
}
ABCDEF
ABCDE
ABCD
ABC
AB
A

 

// Rectangle comprised of x's
for (rows = 0; rows < 4; rows++)
{
     for (col = 0; col < 12; col++)
     {
          cout << 'x' ;
     }

     cout<< "\n";
}
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx



 

// Isosceles triangle of x's centered on column 15 
for(i =0; i<=3;i++)
{
     gotoxy(15-i, 5+i);
     for(int j=1; j<=2*i+1; j++)
          cout<<"x";              //print the x's
     cout<<endl;
}
       x
     xxx
   xxxxx
 xxxxxxx

C++ Keywords

                                                                                   
 C++ Keywords
asm
do
if
return
try
auto
double
inline
short
typedef
bool
dynamic_cast
int
signed
typeid
break
else
long
sizeof
typename
case
enum
mutable
static
union
catch
explicit
namespace
static_cast
unsigned
char
export
new
struct
using
class
extern
operator
switch
virtual
const
false
private
template
void
const_cast
float
protected
this
volatile
continue
for
public
throw
wchar_t
default
friend
register
true
while
delete
goto
reinterpret_cast
  
 
C++ also reserves a number of words that can be used as alternative names for various operators. These alternative names are provided to support character sets that do not support the standard set of C++ operator symbols. These names, listed in Table 2 below, also may not be used as identifiers:

C++ Operator Alternative Names
and
bitand
compl
not_eq
or_eq
xor_eq
and_eq
bitor
not
or
xor
 

In addition to the keywords, the standard also reserves a set of identifiers for use in the library. Identifiers cannot contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an upper-case letter. Certain identifiers those that are defined outside a function may not begin with an underscore.

Nested Loops


Nested Loops
We have seen the advantages of using various methods of iteration, or looping.
Now let's take a look at what happens when we combine looping procedures. 
     The placing of one loop inside the body of another loop is called nesting.  When you "nest" two loops, the outer loop takes control of the number of complete repetitions of the inner loop.  While all types of loops may be nested, the most commonly nested loops are for loops.

nested loops
    
Let's look at an example of nested loops at work.
We have all seen web page counters that resemble the one shown below ( well, OK, maybe not quite this spastic!!).  Your car's odometer works in a similar manner.
This counter (if it worked properly) and your car's odometer are little more than seven or eight nested for loops, each going from 0 to 9.  The far-right number iterates the fastest, visibly moving from 0 to 9 as you drive your car or increasing by one as people visit a web site.  A for loop which imitates the movement of the far-right number is shown below:
for(num1 = 0; num1 <= 9; num1++)
{
      cout << num1 << endl;
}
     The far-right number, however, is not the only number that is moving.  All of the other numbers are moving also, but at a much slower pace.  For every 10 numbers that move in the column on the right, the adjacent column is incremented by one.  The two nested loops shown below may be used to imitate the movement of the two far-right numbers of a web counter or an odometer:
The number of digits in the web page counter or the odometer determine the number of nested loops needed to imitate the process.
     
When working with nested loops, the outer loop changes only after the inner loop is completely finished(or is interrupted.).
Let's take a look at a trace of two nested loops.  In order to keep the trace manageable, the number of iterations have been shortened.
for(num2 = 0; num2 <= 3;  num2++)
{
      for(num1 = 0; num1 <= 2; num1++)
      {
            cout<< num2<< "   " << num1<< endl;
      }
}
       
MemoryScreen
int num2 int num1
00
1
2
3  end loop
10
1
2
3  end loop
20
1
2
3  end loop
30
1
2
3  end loop
4  end loop
    Remember, in the memory, for loops will register a value one beyond (or the step beyond) the requested ending value in order to disengage the loop.
0   0
0   1
0   2
1   0
1   1
1   2
2   0
2   1
2   2
3   0
3   1
3   2
Are we getting a little loopy?