Saturday, November 30, 2013

Decimal to Hexa Decimal and Octal Converter

It is the simplest method to convert a decimal number into Hexa Decimal or Octal number...
It just require two keywords...
one is "hex" (obviously without quotes :p) and the other is "oct".

Here how it works:

Output

And here is the code to make it work...


// www.cppfuzz.com (Ahmad Mukhtar)
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
using namespace std;
void main()

{
 int dec_number;
 cout << "Enter a decimal number: ";
 cin >> dec_number; // getting decimal number from user.
 cout << endl;
 cout << "Number in Hexa Decimal: " << hex << uppercase << dec_number << endl;

 /* In the above line i used hex for conversion, uppercase for uppercase alphabets in hexa decimal numbers and then
 printed it on the screen using cout */
 
 cout << "Number in Octal: " << oct << dec_number << endl; // used oct for octal conversion.
}

Thursday, November 28, 2013

Decimal to Binary Converter

Decimal to Binary Converter

Submitted by: Ibrahim Ali Khan

Code:


// Submitted by Ibrahim Ali Khan
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
using namespace std;
void main()
{
 int num,num1,i,j;
 cout << "Enter a decimal number: ";
 cin >> num;
 if (num==0||num==1)
 {
  cout << num;
  goto label1;
 }
 int array[100]; 
 for (i=0;;i++)
 {
  num1=num;
  num=num/2;
  array[i]=num1%2;
  if (num==1)
  {
   break;
  }
 }
 for (j=i;j>=0;j--)
 {
  if (j==i)
  {
   cout << "Number in Binary form: " << num;
  }
  cout << array[j];
 }
label1:cout << endl;
}

Tuesday, November 26, 2013

[Tutorial] Gimp for beginners Part 1 - Using the fuzzy select tool to make selections

Using the fuzzy select tool to change colors

I decided to make some beginner tutorials on how to use gimp. This is the first one and surely wouldn't be the last. In this tutorial I will tell you how to select an image using the fuzzy select tool and then change its color. First of all you will need Gimp and if you don't have it then download and install it from the link below

http://www.gimp.org/downloads/

Now select an image whose color you want to change, mine is shown below.













Now open the image in Gimp.







After opening the image select the fuzzy select tool, using this tool we can select part of images which have the same color and this is the reason why it is my favourite tool for selecting.





Now before you go on selecting I would recommend that you zoom appropriately so you can see the image parts whose color you need to change. Now during selecting you would see an outline of dots which will tell you which parts have been selected. Now select the part you want whose color need to be changed by left clicking on it and the selection would be made and if you want more to be added to the selection press shift and then left-click.




Now right-click on the image and choose colors and then select Hue-Saturation.... from it. Adjust the hue from it to adjust the color and if you want to make the color light adjust the lightness. Here's how my adjustment made the image look like.





Now press OK and you will get the final image mine is shown below.



Swap numbers to words





>>>>>>>>THE CODE<<<<<<<<

#include<iostream>
#include <math.h>
using namespace std;
void main()
{
   int num,i=10,check;
   cout<<"Enter number \n";
   cin>> num;
//This Part of the program breaks the number into powers
   while(true)
{
if((check=num/pow(10,i)) = = 0)
i--;
else break;
   }
//This part takes the power and gives the number on that position to the switch
   while(num)
   {

    check= num / pow(10,i);
    num= num - check*pow(10,i);
    i--;

//This part of the program takes the number and prints the word
switch(check)
{
case 1:
 cout<<"one  ";
 break;
case 2:
 cout<<"two  ";
 break;
case 3:
 cout<<"three  ";
 break;
case 4:
 cout<<"four  ";
 break;
case 5:
 cout<<"five  ";
 break;
case 6:
 cout<<"six  ";
 break;
case 7:
 cout<<"seven  ";
 break;
case 8:
 cout<<"eight  ";
 break;
case 9:
 cout<<"nine  ";
 break;
default:
cout<<"zero  ";

}
 }
cout<<endl;



//coded by cppfuzz.com  
}

Star Pyramid with while loop















>>>>>>>>>>>The Code<<<<<<<<<<<<<<<

#include <iostream>
using namespace std;
void main()
{
   
    int x,row,y=0;
    cout<<"Enter number of rows"<<endl;
    cin>>row;
    row=row*2;


    while(y<row)
    {
        ++y;
        int x=row;
        while(x>y)
        {   
            x=x-2;
            cout<<" ";
        }
       
        int z=0;
          while(z<y)
        {   
            z=z++;
            cout<<"*";
        }
        y++;
        cout<<endl;
   
    }

    //coded by cppfuzz.com

}

Star Pyramid with for loop




 >>>>>>>>>>THE CODE<<<<<<<<<<<<<<<<
 
#include <iostream>
using namespace std;
void main()
{
    int row;
    cout<<"Enter number of rows"<<endl;
    cin>>row;
    row=row*2;
    int t=0;
//This loop controls the rows
    for(int y=0;y<row;y++)
    {
        y++;
//This loop controls the spaces
        for(int x=row;x>y;x=x-2)
        {
            cout<<" ";
        }
//This loop controls the stars in each row
          for(int z=0;z<y;z++)
        {
            cout<<"*";
        }
        cout<<endl;
   
    }

    //coded by cppfuzz.com

}

Monday, November 25, 2013

[Tutorial C++] Basics of Programming Part 1

This would be the starting of basics tutorials for C++ aimed at helping beginners learn C++. The programs made in the tutorials, are made using Codeblocks which uses GNU GCC compiler.

If you have no knowledge about what is programming? then I do recommend that you read this first.

http://www.cppfuzz.com/2013/11/c-class-1-some-basic-concepts.html

http://cplus.about.com/od/introductiontoprogramming/p/programmers.htm

Required software

Before we can start to program we will require two things they would be a C++ compiler and a text editor or you can just get an IDE (Integrated Development Environment) like Codeblocks or Borland C++.

You can download codeblocks from here.

Writing your first program

The first program we will make will be a simple Hello World in which as most of you will know that we are going to print Hello World on the screen.

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


using namespace std;


int main()
{



cout<<"Hello World";



getch();
}
 
Now compile and run it, if you are on codeblocks click the button which contains the play and build image.
 
If you did everything right then you will see a window come up in which Hello World would be written.




 So by now you will want me to explain to you how this stuff works. Okay then but we will not go in much detail.
 
Let's start then, first of all we have the header files iostream and conio.h which are a kind of dictionary for the C++ as it contains the commands which tell C++ what to do. While #include is the commands which add these header files to our program so next time whenever you are going to add a header file write "#include<>" without the double quotes and then write the name of the header file between "<>". Always remember to add your header files and it should be at the start otherwise your program will fail to work.
 

#include<iostream>
#include<conio.h>
 
 
Next comes up
 
using namespace std;
 
which is something that codeblocks users should add otherwise your programs would again fail to work.
 
Finally comes the main thing which is the main function
 
int main()
{


}
 
You always add the main parts of the program that is C++ user commands in it.
 
Now comes the two functions. The first one that is cout is used to print something on the screen. Remember all the text should go inside the double quotes.
 
cout<<"Hello World";
This is a function I will leave for you guys to figure out but I will explain it in the next tutorial. Though here's a hint for it, remove it from the program and you will notice something. If you don't then well you will have to wait till the next tutorial  :).

getch();
 
See you then in the next tutorial and until then happy coding. Also feel free to post any problems or your feedback in the comments below. Also visit http://bb.waratteka.net/  for more programming tutorials.

Sunday, November 24, 2013

Menu Driven Program

Menu Driven Program

Output


Code:


#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;
int alpha;
char beta;
float side,length,width,side1,side2,side3,base,height,area;
void main()
{
    while(beta!='n')
 {

  
 label1:cout<<" press any key of your choice:\n\n"<<endl;
 cout<<"1: Perimeter Calculation"<<endl;
 cout<<"2: Area Calculation"<<endl;
 cout<<"3: Volume Calculation"<<endl;
 cout<<"4: Exit"<<endl;
 cin>>alpha;
  
 switch(alpha)
 {
  case 1:
   {    
    system("cls");
   cout<<"Press any key of your choice"<<endl;
   cout<<"1: Perimeter of a square"<<endl;
   cout<<"2: Perimeter of a rectangle"<<endl;
   cout<<"3: Perimeter of a triangle"<<endl;
   cout<<"4: Back to the Main menu"<<endl;
   cout<<"5: Exit"<<endl;
   cin>>alpha;

   switch(alpha)
   {
    case 1:
   {
    system("cls");
    
    cout<<"enter side of square:";
    cin>>side;
    cout<<"perimeter of square="<<4*side<<endl;
   } 
   break;
  
   case 2:
   {
    system("cls");
    cout<<"enter length of rectangle:";
    cin>>length;
    cout<<"enter width of rectangle:";
    cin>>width;
    cout<<"perimeter of rectangle is="<<2*(length+width)<<endl;
   }
   break;
  
   case 3:
   {
    system("cls");
    cout<<"enter sides of triangle:";
    cin>>side1>> side2>> side3;
    cout<<"perimeter of triangle is="<<side1+side2+side3<<endl;
   }
   break;

   case 4:
   {
    system("cls");
    goto label1;
   }
   break;

   case 5:
   {
    exit(alpha);
   }
   break;
    }
   } 
  break;

 case 2:
 {
  system("cls");
  cout<<" Press any key of your choice"<<endl;
  cout<<"1: Area of a square"<<endl;
  cout<<"2: Area of a rectangle"<<endl;
  cout<<"3: Area of a triangle"<<endl;
  cout<<"4: Back to the Main menu"<<endl;
  cout<<"5: Exit"<<endl;
  cin>>alpha;

  switch(alpha)
  {
   case 1:
   {
    system("cls");
    cout<<"Enter side of square:";
    cin>>side;
    cout<<"Area of square is="<<side*side<<endl;
   }
   break;

   case 2:
   {
    system("cls");
    cout<<"Enter length of rectangle:";
    cin>>length;
    cout<<"Enter width of rectangle:";
    cin>>width;
    cout<<"Area of rectangle="<<length*width<<endl;
   }
   break;
   
   case 3:
   {
    system("cls");
    cout<<"enter base and height of triangle:";
    cin>>base>> height;
    cout<<"Area of triangle is="<<1/2*(base*height)<<endl;
   }
   break;

   case 4:
   {
    system("cls");
    goto label1;
   }
   break;

   case 5:
   { 
    exit(alpha);
   }
   break;
   }
  }
  break;


 case 3:
 {
  system("cls");
  cout<<" Press any key of your choice"<<endl;
  cout<<"1: Volume of a cube"<<endl;
  cout<<"2: Volune of a box"<<endl;
  cout<<"3: Volume of a prism"<<endl;
  cout<<"4: Back to the Main menu"<<endl;
  cout<<"5: Exit"<<endl;
  cin>>alpha;
  switch(alpha)
  {
  case 1:
  {
   system("cls");
   cout<<"Enter side of cube:";
   cin>>side;
   cout<<"Volume of square is="<<side*side*side<<endl;
  }
   break;
  
  case 2:
  {
   system("cls");
   cout<<"enter length of box:";
   cin>>length;
   cout<<"enter width of box:";
   cin>>width;
   cout<<"enter height of box:";
   cin>>height;
   cout<<"Volume of box is:="<<length*width*height<<endl;
  }
  break;

  case 3:
  {
   system("cls");
   cout<<"enter area of prism:";
   cin>>area;
   cout<<"enter length of prism:";
   cin>>length;
   cout<<"Volume of prism="<<area*length<<endl;
  }
  break;

  case 4:
  {
   system("cls");
   goto label1;
  }
  break;

  case 5:
  {
   exit(alpha);
  }
  break;

 }
  }
 break;

 case 4:
 {
  exit(alpha);
 }
 break;
 }

 cout<<"Do you want to continue:(y/n)"<<endl;
 cin >> beta;
system("cls");
 
 }
}
 

Wednesday, November 20, 2013

Reverse Any Digit Number

Reverse Any Digit Number

Output

Code:


// www.cppfuzz.com (Ahmad Mukhtar)
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
#include <windows.h>
using namespace std;
void main()

{
 system ("color 8F");
 int number,a,rev=0;
 cout << "Enter a number: ";
 cin >> number;
 int temp = number;
 while (temp!=0)
 {
  a = temp % 10;
  rev = (rev * 10) + a;
  temp = temp / 10;
 }
 cout << number << " Reversed to " << rev << endl;
}

Guess Number: Game in C++

Guess Number: Game in C++

Output

Code:



// www.cppfuzz.com (Ahmad Mukhtar)
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;
void main()
{
 system("color 0A");
 int input,secret;
 char answer;
 while (answer!='n'&&answer!='N')
 {
  srand(time(NULL));
  secret = rand() % 1000 + 1;
  cout << "I have a number between 1 and 1000." << endl;
  cout << "Can you guess my number???" << endl;
  do
  {
   cout << "Please type your guess..." << endl;
   cin >> input;
   Sleep(500);
   system("Cls");
   if (input<secret)
   {
    cout << "Too low, try again." << endl;
   }
   if (input>secret)
   {
    cout << "Too high,try again." << endl;
   }
  } while (input!=secret);
  cout << endl;
  cout << "Excellent! You Guessed the number!" << endl;
  cout << "Would you like to play again (y or n)? ";
  cin >> answer;
 }
}

Tuesday, November 19, 2013

Prime Numbers

Prime Numbers

output 
Code:


// www.cppfuzz.com (Ahmad Mukhtar)
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
using namespace std;
void main()

{
 int num;
 cout << "Enter a number: ";
 cin >> num;
 cout << "Prime numbers less than " << num << " are: " << endl;
 for (int i=2;i<num;i++)
 {
  for (int j=2;j<=i;j++)
  {
   if (j<i)
   {
    if ((i%j)==0)
     break;
   }
   else
   {
    cout << i << "\t";
   }
  }
 }
 cout << endl;
}

Monday, November 18, 2013

Simple Calculator

Simple Calculator

Output

Code:


// www.cppfuzz.com
/*

  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       

*/
#include <iostream>
using namespace std;
void main()
{
 double num1,num2;
 double addition, subtraction, multiplication, division;
 int num;
 cout << "Enter first number: ";
 cin >> num1;
 cout << "Enter second number: ";
 cin >> num2;
 cout << "Press 1 for addition" << endl;
 cout << "Press 2 for subtraction" << endl;
 cout << "Press 3 for multiplication" << endl;
 cout << "Press 4 for division" << endl;
 cin >> num;
 if (num>4)
 {
  cout << "Invalid Input, Please Give a Valid Input From 1 to 4: ";
  cin >> num;
 }
 else
 {
 switch (num)
 {
 case 1:
  {
   addition = num1 + num2;
   cout << "Result of addition is: " << addition << endl;
  }
  break;

 case 2:
  {
   subtraction = num1 - num2;
   cout << "Result of subtraction is: " << subtraction << endl;
  }
  break;

 case 3:
  {
   multiplication = num1 * num2;
   cout << "Result of multiplication is: " << multiplication << endl;
  }
  break;

 case 4:
  {
   division = num1 / num2;
   cout << "Result of division is: " << division << endl;
  }
  break;

 default:
  {
   cout << "Invalid Input." << endl;
  }
  break;
 }
 }
}

Sunday, November 17, 2013

Request: Retail Price Program

Retail Price Program

This program was requested by a user, you can make a bigger program by using this logic.

Code:



// http://www.cppfuzz.com (Ahmad Mukhtar)
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
using namespace std;
void main()

{
 double product1 = 2.98;
 double product2 = 4.50;
 double product3 = 9.98;
 double product4 = 4.49;
 double product5 = 6.87;
 char answer1, answer2, answer3, answer4, answer5;
 double qop1,qop2,qop3,qop4,qop5, total, total1=0, total2=0, total3=0, total4=0, total5=0;
 cout << "Price of Product number 1 is: $" << product1 << endl;
 cout << "Price of Product number 2 is: $" << product2 << endl;
 cout << "Price of Product number 3 is: $" << product3 << endl;
 cout << "Price of Product number 4 is: $" << product4 << endl;
 cout << "Price of Product number 5 is: $" << product5 << endl;
 cout << "Do you want to buy Product 1 ? (Y/N)";
 cin >> answer1;
 switch (answer1)
 {
 case 'y': case 'Y':
  {
   cout << "How much quanity of Product 1 you want ?" << endl;
   cin >> qop1;
   total1 = qop1 * product1;
  }
  break;

 default:
  {
   cout << "You are not buying Product 1" << endl;
  }
 }
 cout << "Do you want to buy Product 2 ? (Y/N)";
 cin >> answer2;
 switch (answer2)
 {
 case 'y': case 'Y':
  {
   cout << "How much quanity of Product 2 you want ?" << endl;
   cin >> qop2;
   total2 = qop2 * product2;
  }
  break;

 default:
  {
   cout << "You are not buying Product 2" << endl;
  }
 }
 cout << "Do you want to buy Product 3 ? (Y/N)";
 cin >> answer3;
 switch (answer3)
 {
 case 'y': case 'Y':
  {
   cout << "How much quanity of Product 3 you want ?" << endl;
   cin >> qop3;
   total3 = qop3 * product3;
  }
  break;

 default:
  {
   cout << "You are not buying Product 3" << endl;
  }
 }
 cout << "Do you want to buy Product 4 ? (Y/N)";
 cin >> answer4;
 switch (answer4)
 {
 case 'y': case 'Y':
  {
   cout << "How much quanity of Product 4 you want ?" << endl;
   cin >> qop4;
   total4 = qop4 * product4;
  }
  break;

 default:
  {
   cout << "You are not buying Product 4" << endl;
  }
 }
 cout << "Do you want to buy Product 5 ? (Y/N)";
 cin >> answer5;
 switch (answer5)
 {
 case 'y': case 'Y':
  {
   cout << "How much quanity of Product 5 you want ?" << endl;
   cin >> qop5;
   total5 = qop5 * product5;
  }
  break;

 default:
  {
   cout << "You are not buying Product 5" << endl;
  }
 }
 total = total1 + total2 + total3 + total4 + total5;
 cout << "Total Retail Price Is: $" << total << endl;
}

Saturday, November 16, 2013

Program to Reverse a Five Digit Number

Program to Reverse a Five Digit Number

Output:

Output

Code:


#include <iostream>
using namespace std;
void main ()
{
    int num;
 cout << "Enter a five number digit: ";
    cin>>num;
    int a=(num/10000);
    int b=(num%10000)/1000;
    int c=((num/100)%100)%10;
    int d=(((num/10)%1000)%100)%10;
 int e=(num%10);
    cout << num << " is reversed to " << e << d << c << b << a << endl;
}

Friday, November 15, 2013

C++ Class 2: A Simple Program (Continued)

A Simple Program (Continued)

Program
Click on image to enlarge it.

Output of the above program:

Output

Another Example:

Program
Click on image to enlarge it.

Output of the above program:

Output

Another Simple Program:

Adding Two Integers:

  • Variables
    • Location in memory where value can be stored
    • Fundamental data types
      • int - integer numbers (whole numbers + and -)
      • char - characters
      • float/double - floating point numbers
    • Declare variables with name and data type before use
int integer1;
int integer2;
int sum;
    • Can declare several variables of same type in one declaration
      • comma-separated list
                         
int integer1, integer2, sum;

    • Variable names
      • Valid identifier
        • Series of characters (letters, digits, underscores)
        • Cannot begin with digit
        • Case sensitive
  • Input stream object
    • >> (stream extraction operator)
      • used with cin
      • Waits for user to input value, then press Enter (Return) key
      • Stores value in variable to right of operator
        • Coverts value to variable data type
  • = (assignment operator)
    • Assigns value to variable
    • Binary operator (works on two operands)
    • Example:
      • sum = variable1 + variable2;
      • Operands : sum, value of expression, variable1+variable2
Code:

// http://welearncpp.blogspot.com
// code3.cpp
// Addition Program
#include <iostream>
using namespace std;

// function main begins execution of program
int main()

{
 int integer1; // first number to be input by user
 int integer2; // second number to be input by user
 int sum; // variable in which sum will be stored

 cout << "Enter first integer: "; // prompt
 cin >> integer1; // read an integer

 cout << "Enter second integer: "; // prompt
 cin >> integer2; // read an integer

 sum = integer1 + integer2; // assign result to sum

 cout << "Sum is " << sum << endl; // print sum
 
 return 0; // indicates successfull execution of program
} // ends the main function

Output of the above program:

Output

Well after this class you might be able to create simple programs such as simple calculator. We will soon come back with a new class for new things..

For complete list of manipulators visit this site:
http://www.cplusplus.com/reference/library/manipulators/

Comparison of Two Numbers


Code:


#include <iostream>
using namespace std;
void main()
{
 int a,b,c,d;
 cout << "Enter First Number: ";
 cin >> a;
 cout << "Enter Second Number: ";
 cin >> b;
 if (a>b)
 {
  cout << "***** Explanation about these numbers *****" << endl;
  cout << "Larger Number Is: " << a << endl;
  cout << a << " is larger than " << b << " by " << a-b << " units " << endl;
 }
 else
 {
  cout << "***** Explanation about these numbers *****" << endl;
  cout << "Larger Number Is: " << b << endl;
  cout << b << " is larger than " << a << " by " << b-a << " units " << endl;
 }
 if (a==b)
 {
  cout << "Both Numbers Are Equal" << endl;
 }
 else
 {
  cout << "Both Numbers Are Not Equal" << endl;
 }
 c = a%2;
 if (c==0)
 {
  cout << "First Number is Even" << endl;
 }
 else
 {
  cout << "First Number is Odd" << endl;
 }
 d = b%2;
 if (d==0)
 {
  cout << "Second Number is Even" << endl;
 }
 else
 {
  cout << "Second Number is Odd" << endl;
 }
 if(a>b)
{
 c = a%b;
 if(c==0)
  cout<<"Larger Number " << a << " is Multiple of Smaller Number " << b << endl;
 else
  cout<<"Larger Number " << a << " is not a Multiple of Smaller Number " << b << endl;
}

else
{
 c = b%a;
 if(c==0)
  cout<<"Larger Number " << b << " is Multiple of Smaller Number " << a << endl;
 else
  cout<<"Larger Number " << b << " is not a Multiple of Smaller Number " << a << endl;
}
 }

Diamond With 3 Do While Loops


Code:

// http://welearncpp.blogspot.com
#include<iostream>
using namespace std;

void main()
{
 int row,space,star=1;
 cout << "Enter an odd number :";
 cin >> row;
 if((row%2)==0)
 row--;
 space=row;
 int a=1;
 do
 {
  int b=1;
  do
  {
   cout << " ";
   b++;
  } while(b<=space);

  int c=1;
  do
  {
   cout << "*";
   c++;
  } while (c<=star);
  if(a<=row/2)
  {
   space--;
   star+=2;
  }
  else
  {
   space++;
   star-=2;
  }
  cout<<endl;
  a++;
 } while(a<=row);
}

Thursday, November 14, 2013

C++ Class 2: A Simple Program

A Simple Program:

Printing a Line of Text

  • Comments
    • Document Programs
    • Improve program readability
    • Ignored by compiler
    • Single-line comment
      • Begin with //
    • Multiple-line comment
      • Everything between /* and */ is ignored
  • Preprocessor directives
    • Processed by preprocessor before compiling
    • Begin with #
      • #include
      • #define

C++ Preprocessor

  • C++ Compilers automatically invoke a preprocessor that takes care of #include statements and some other special directives.
  • You don't need to do anything special to run the preprocessor - it happens automatically.

The Preprocessor

  • Lines that start with the character '#' are special instructions to a preprocessor.
  • The preprocessor can replace the line with something else:
    • include: replaced with contents of a file
  • Other directives tell the preprocessor to look for patterns in the program and do some fancy processing.

Preprocessing

  1. C++ Program
  2. C++ Preprocessor
  3. Temporary file (C++ program)
  4. C++ Compiler
  5. Executable Program
Code
Click on image to enlarge it

Output of above program:

Output
Click on image to enlarge it
The program was written in Microsoft Visual C++ 6.0
You can use any compiler which you find easy to use but I suggest you to use Microsoft Visual C++  6.0 or Borland in the beginning if you want to be a good programmer.

  • Output stream object
    • cout
    • "Connected" to screen
    • <<
      • Stream insertion operator
      • Value to right (right operand) inserted into output stream
  • Namespace
    • using namespace std; specifies that we are using objects from "namespace" std
  • Escape character and escape sequences
    • \ is called the "Escape Character"
    • Indicates "special" character output
Some Escape Characters:

Some Escape Characters
Click on image to enlarge it
(To be continued)

C++ Class 1: Some Basic Concepts

What is a Computer?

  • A computer is an electronic device that can follow instructions to
    • Accept input (data)
    • Process that input
    • Produce output (information)
Data is raw, unorganized facts that need to be processed. Data can be something simple and seemingly random and useless until it is organized.
For Example:
Each student's test score.

When data is processed, organized, structured or presented in a given context so as to make it useful, it is called Information.
For Example:
The class' average score or the school's average score.


  • A computer is an electronic device, operating under the control of instructions (software) stored in its own memory unit, that can accept data (input), manipulate data (process), and produce information (output) from the processing.
  • Generally, the term is used to describe a collection of devices that function together as a system.

What does a Computer do?

  • Computers perform four general operations, which make up the information processing cycle
    • Input
    • Processing
    • Output
    • Storage

Devices that comprise a Computer System

  • Monitor (output)
  • Speaker (output)
  • Printer (output)
  • System Unit (processor, memory)
  • Scanner (input)
  • Keyboard (input)
  • Mouse (input)
  • Storage Devices (CD-RW, Floppy, Hard disk etc)

Introduction to C++ Programming

The Computer
The Computer: "I only know Binary 0s and 1s"








The Programmer
Programmer writes a 'set of instructions' for Computer. This set is called a 'Computer Program'







Programming Languages

  • Programmers write these instructions in different programming languages.
    • Directly understandable by Computer
      • Machine Languages
    • Requiring intermediate translation steps
      • Assembly Languages
      • High Level Languages
  • Machine Languages
    • Strings of 0s and 1s
    • Directly understandable by computer
    • Defined by hardware of the machine
    • Hard for human beings to learn, error prone
    • Slow Programming
  • Assembly Languages
    • English like abbreviations
    • Not directly understandable by computer, hence required translator programs called 'Assemblers'
    • Relatively easier for human beings to learn
    • Slightly faster programming
  • High Level Languages
    • Developed for fast and speedy programming
    • Not directly understandable by computer, hence required translator programs called 'Compilers'
    • Not dependent on hardware of the machine (mostly)
    • Easy to learn, less chances of error
    • Fast programming
Interpreters are another important category of translator programs.

Why C++

  • C is a language designed by and for programmers
  • C++ is an expanded and enhanced version of C Programming Language
  • It is the language of choice for professional programmers worldwide
  • Once mastered, C++ will give you complete control over the computer
  • C++ is, above all, the most powerful programming language even invented

Compiler

1: C++ Program (Created with text editor or development environment):
int main()
    {
 
int i=1;
    . . .

2: C++ Compiler:
e.g. g++

3: Machine Language Program
01001001
10010100

Basics of a Typical C++ Environment

  • C++ Systems
    • Program-development environment
    • Language
    • C++ Standard Library
  • Phases of C++ Programs
    • Edit (Program is created in the editor and stored on disk.)
    • Preprocess (Preprocessor program processes the code.)
    • Compile (Compiler creates object code and stores it on disk.)
    • Link (Linker links the object code with the libraries, creates a.out and stores it on disk.)
    • Load (Loader puts program in memory.)
    • Execute (CPU takes each instruction and executes it, possibly storing new data values as the program executes.)

The C++ Standard Library

  • C++ programs consist of pieces/modules called classes and functions
    • A programmer can create his own functions
      • Advantage: the programmer knows exactly how it works
      • Disadvantage: time consuming
    • Programmers will often use the C++ library functions
      • Use these as building blocks
    • Avoid re-inventing the wheel
      • If a pre-made function exists, generally best to use it rather than write your own
      • Library functions are carefully written, efficient, and portable



 

Copyright @ 2014 CPP Fuzz.