C++ gurus and learners, we program ourselves :P all work is CC licensed

Sunday, November 24

Moving Text - SCreen Saver in C++

Moving Text - SCreen Saver in C++

Although it is exam time but i just can't help myself from compiler;; below is just code, nothing much to explain; just copy past and run it is compatible with visual studio as no graphic thing is used; enjoy

  1. #include<iostream>
  2. #include<windows.h>
  3. HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
  4. void clr(int y){SetConsoleTextAttribute(out,y);}
  5. void go(int a,int b){COORD cord; cord.X = a; cord.Y = b; SetConsoleCursorPosition(out,cord);}
  6. using namespace std;
  7. void G(int x,int y)
  8. {go(x,y);cout<<char(220)<<char(220)<<char(220)<<char(220)<<char(220)<<endl; y++;
  9. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl; y++;
  10. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(32)<<endl; y++;
  11. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(223)<<char(219)<<endl; y++;
  12. go(x,y);cout<<char(223)<<char(223)<<char(223)<<char(223)<<char(223)<<endl;}
  13. void A(int x,int y)
  14. {go(x,y);cout<<char(220)<<char(220)<<char(220)<<char(220)<<endl; y++;
  15. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<endl; y++;
  16. go(x,y);cout<<char(219)<<char(220)<<char(220)<<char(219)<<endl; y++;
  17. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<endl; y++;
  18. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<endl;}
  19. void M(int x,int y)
  20. {go(x,y);cout<<char(219)<<char(220)<<char(32)<<char(220)<<char(219)<<endl; y++;
  21. go(x,y);cout<<char(219)<<char(32)<<char(287)<<char(32)<<char(219)<<endl; y++;
  22. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl; y++;
  23. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl; y++;
  24. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl;}
  25. void E(int x,int y)
  26. {go(x,y);cout<<char(220)<<char(220)<<char(220)<<char(220)<<char(220)<<endl; y++;
  27. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(32)<<endl; y++;
  28. go(x,y);cout<<char(219)<<char(205)<<char(205)<<char(32)<<char(32)<<endl; y++;
  29. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(32)<<endl; y++;
  30. go(x,y);cout<<char(223)<<char(223)<<char(223)<<char(223)<<char(223)<<endl;}
  31. void R(int x,int y)
  32. {go(x,y);cout<<char(220)<<char(220)<<char(220)<<char(220)<<char(220)<<endl; y++;
  33. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl; y++;
  34. go(x,y);cout<<char(219)<<char(220)<<char(220)<<char(220)<<char(219)<<endl; y++;
  35. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<char(32)<<endl; y++;
  36. go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<char(220)<<endl;}
  37. int main()
  38. {
  39. int X=0,x1,x2,x3,x4,x5,x6,y=5; x1=0; x2=6; x3=12; x4=18; x5=24; x6=30;
  40. for (int i=0;i<1;)
  41. {clr(9);go(x1,y);G(x1,y);if(x1>72){x1=1;}x1++;clr(12);go(x2,y);A(x2,y);if(x2>72){x2=1;}x2++;
  42. clr(6);go(x3,y);M(x3,y);if(x3>72){x3=1;}x3++;clr(9);go(x4,y);M(x4,y);if(x4>72){x4=1;}x4++;
  43. clr(10);go(x5,y);E(x5,y);if(x5>72){x5=1;}x5++;clr(12);go(x6,y);R(x6,y);if(x6>72){x6=1;}x6++;
  44. if(x1==2){y+=3;} if(y>50){y=3;}system("cls");}
  45. return 0;
  46. }
for feedback:



    
    

    Saturday, November 23

    Hollow Square & Hollow diamond & Hollow Triangle

    Hollow Square & Hollow diamond & Hollow Triangle

    It's basically a request from a student as we go further in loops and functions we need to know more tricks and practise more for these things which eventually leads to good programming;
    Basic concept to understand;
    1. Notice if any line of the shape is going to match original filled shape; i.e. in square the first line and last line are filled!
    2. Make sure you know the ration of spaces for hollow part to the number of characters in the corresponding filled space i.e. if your square is of the size of 5x5 so it is going to be 5 characters in a single line; but in hollow shape you are going to print only 2 characters ; the first one and last one no matter how much long line is this; but here in 5 character line the total spaces would be countable as spaces=one side length - 2;
    Prepare for the basic shapes looping that will be joined together to make final shape i.e. to make diamond you place 4 triangles together if you ever made it:

    Hence by mixing these points you can make logic of this king of shapes.

    Basic Hollow looping;
    In this kind of looping you have to make sure that ration of lines is safely described to make appropriate loop
    Square concept:
    1. int main()
    2. {
    3.         int size;            //suppose it is 5
    4.         cin>>size;
    5.         for (int i = 0 ; i < size ; i++ ) //runs 5 times
    6.          {
    7.                  cout  <<  "*" ;          //first char
    8.                  for (int j=2;j<size;j++) //runs 5-2 times
    9.                  {
    10.                      cout<<" ";
    11.                  }
    12.                  cout  <<  "*" ;          //last char
    13.           }
    14.    return 0;
    15. }
    Now here in line 8 you can see that formula which subtracts number of characters from hollow line to make it even to the rest of shape, it could be 5 - 3  or maybe  5-4  required for the program hence the distribution of spaces and loops as well; depending on the shape.
    And for first and last line here is simple method:
    1.  if ( line 1 || last line ) //mean i==1 or i==size-1 according to loop iteration
    2.  {
    3.          for ( int a=0;a<size;a++ )
    4.          {
    5.              cout<< "*" ;
    6.          }
    7.       cout << endl ; continue ;
    8.  }
    Put it in main loop Before the space loop so that if first line is being print it would print it and skip the space printing for first and last line.
    i guess it is enough for the basic concept and you will be able to make assignment ;; I'll give complete codes after exams;; thanks for reading and for feedback or any questions:
    Join the group  on facebook for constant news about new posts..
    
    

    Friday, November 22

    gotoxy() function in c++ by Umair Sajid

    21:49 By

    gotoxy() function :

    The gotoxy function is used to to move the cursor to a specified location on the screen. Its Just Something Other then just Sequential Execution .It moves the cursor with respect to x axis and y axis. It is very useful when the output is to be displayed at a specified location on the screen.

    Working: 

    The Variable x indicates x-axis. Its Value can be from 0 to 79. The Variable y indicates the y-axis. Its Value can be from 0 to 24.

    Program:

    #include               // Header Files 
    #include               // for getch() function 
    #include               
    #include             // Necessary when treating consoles  
    using namespace std;
    void gotoxy (int x, int y)
        {
             COORD coordinates;     // coordinates is declared as COORD
             coordinates.X = x;     // defining x-axis
             coordinates.Y = y;     //defining  y-axis
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coordinates);       
        }
    int main()              
    {
        int x;
        int y; 
     
        cout<<"Enter x: ";
        cin>>x;
        cout<<"Enter y: ";
        cin>>y;
            
        gotoxy(x,y);     // function call 
        cout<<"C++ Clan Umair Sajid"<<endl;
        getch();
        return EXIT_SUCCESS;
    }
    

    Tuesday, November 19

    Easy Colouring

    As promissed here is bit easy technique for those colour commands;
    Hope you guys understand it :
    Theme here is to use funcitons to determin command stuff i.e. how much easy would it be if you only have to type 

    color(r); //red colour

    here is the function that will help you to use colours easily
    ______________________________________________________________________

    1. HANDLE Output=GetStdHandle(STD_OUTPUT_HANDLE);
    2. void colour(int x)
    3. {SetTextAttribute(Output,x);}
    ______________________________________________________________________
    here if you remember colour codes integers just use them as follows:

    colour(10);      //green


    and if you want to make it runing upon character values you can place switch in it

    1. HANDLE Output=GetStdHandle(STD_OUTPUT_HANDLE);
    2. void colour(char ch)
    3. {
    4. int x;
    5. switch (ch)
    6. {
    7. case 'r':  x=12; break;                       //red
    8. case 'b': x=9;   break;                        //blue
    9. case 'g': x=10; break;                        //green
    10. case 'a': x=11; break;                        //aqua
    11. case 'v': x=13; break;                         //voilet
    12. case 'y': x=14; break;                         //yellow
    13. case 'w: x=15; break;                         //white
    14. }
    15.  SetTextAttribute(Output,x);
    16. }

    now you can call it as this;
    colour('r');                    //red colour
    end of post;



    
    

    Monday, November 18

    Play Sound in Your CPP Program

    Till now, all of us have coded C++ programs on the black & white console, some know how to color the text on string.
    Lets go a step ahead and introduce sound being played as your program runs.

    Interesting, Right?
             Yes, you'll have a new effect to add in your code :-)

    How to Do?
             The amazing sound feature consists of just two lines,
    1. The PlaySound function call.
    2. The header file that contains this function.

    Code:
               
     #include<iostream.h>
     #include<conio.h>
     #include <string.h>
     #include <windows.h>
     using namespace std;
            
     int main()
     {
                     PlaySound("file_name.wav",NULL,SND_FILENAME|SND_LOOP);
     // PlaySound method is defined in windows.h
     // file_name is the name of your sound file in .wav format
     getche();
     return 0;
     }

    Note:
     Your .wav file should be in the same location as that of ur .cpp file.

    More:


    For details and possible values of parameters in PlaySound function visit the link below
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx
    Include sound in your programs and have FUN
    Stay Blessed :-)


    Regards

    
    

    Bit Level Manipulation and Programming in C++

    C++ is a classical language and it allows us to manipulate bits in much better ways. for starter we will learn 2 kind of bit operators namely bit shift operators

    Left shift operator: <<
    Right shift operator: >>

    The left shift operator shifts all bits to left and left most bit(MSB) is dropped/deleted
    similarly the right shift operator shifts all bits to right and LSB(right most bit) is dropped

    e.g. binary of 2 will be 00000010 which is when shifted to left becomes 00000100 which in decimal is 4
    so it also means that shifting to left multiplies by 2 and shifting to right divides by 2.

    void main()
    {
    int castles = 2;
    castles = castles<<1; //this will set castles = 4 i.e. 2x2
    castles <<=2; //this will left shift by 2 bits and hence castles is now multiplied by 2 two times so it now becomes 16
    //you can experiment for your self with these tricks
    //PS these are heavily used for masking purposes and files compression as well
    

    Reverse any Integer without strings

    It seems now are a stronger community to write generalized programs for FAST NU as well :)
    I am posting a program below which reverses any number with just 1 while loop.
    See if it works :P
    PS please if you copy this or get inspired from this post for your blog then mention us in the post that will be a great gesture than just being douche 3:)
    //Code by Umar Farooq
    /**
     * follow us at http://CppQA.blogspot.com
     * 
    **/
    #include <iostream>
    using namespace std;
    
    int main() {
     int number = 12345678;
     cout<<"Reverse of number "<<number<<" is ";
     while(number)
     {
      cout<<number%10;
      number/=10;
     }
     
     return 0;
    }
    

    Sunday, November 17

    Loading bar in C++

    Since we are high on special effects these days, so below is the code to make a loading bar in C++.

    #include iostream.h
    #include conio.h
    #include windows.h
    #include dos.h
    
    void main()
    {
     system("color 0a");
     cout<<"\n\n\n\t\t\t\tPlease wait while loading\n\n";
     char a=177, b=219;
     cout<<"\t\t\t\t";
     for (int i=0;i<=15;i++)
     cout<<a;
     cout<<"\r";
     cout<<"\t\t\t\t";
     for (int i=0;i<=15;i++)
     {
      cout<<b;
      for (int j=0;j<=1e8;j++); //You can also use sleep function instead of for loop
     }
    
     clrscr();
     cout<<"\n\n\n\t\t\tHELLO WORLD\n\n\t\t\tShashka made by Ivan :P";
    
     getch();
    }

    Output is :

    Addition of two 2-Dimensional Arrays

    Below is the code that I made to add the elements of a 2-D array.
    The 2-dimensional arrays have been previously discussed, what they are and how to initialize them.
    http://cppqa.blogspot.com/2013/11/two-dimension-pointers-c.html

    For those (like me) who are still confused, think of it like this,
    An 1-dimensional array can hold element like this:
    [ 1 3 5 7 9]    => array1[5]

    Whereas a 2-D array will hold elements much like a matrix, like this:
    | 2  4 |
    | 6  8 |            => array2[2][2]
    So we get a matix of 2rows and 2 columns.

    Now that we have a basic concept of 2-D array, lets go a little further.
    The initialization is petty much same as that in the above link. We shall only do the addition of 2-D arrays.

    int main()
    {
    int x[2][2] , y[2][2] , z[2][2];
    //for simplicity 2,2 matrix is used, but you can also make a user specified
    
    int i , j , k;
    
    cout<<"enter number for 1st matrix"<<endl; //applying for loop to enter the data
    
    for( i = 0 ; i < 2 ; i++)
    for( j = 0 ; j < 2 ; j++)
    {
    cin>>x[i][j];
    }
     
    cout<<"enter numbers for 2nd matrix"<<endl;
    for( i = 0 ; i < 2 ; i++)
    for( j = 0 ; j < 2 ; j++)
    {
    cin>>y[i][j];
    }
    
    for( i = 0 ; i < 2 ; i++)
    for( j = 0 ; j < 2 ; j++)
    {
    z[i][j] = 0;
    for( k = 0 ;k < 2 ; k++)
    z[i][j]=x[i][j]+y[i][j]; // x + y stored in z
    }
    
    cout<<"additon of matrix is"<<endl;   // z is displayed
    for( i = 0 ; i < 2 ; i++)
    {
    for( j = 0 ; j < 2 ; j++)
    cout<<z[i][j]<<"\t";
    cout<<"\n";
    }
    getch();
    return 0;
    }
    
    
    So here it is a simple program to add two 2-D arrays

    Saturday, November 16

    Recursive functions

    19:30 By

    Recursive functions

    Recursive functions are way much difficult then simple functions
    1. in a function you place set of commands in a bracket and use it as many time as u want
    2. in recursive functions you make such a function which with the help of other functions and the repetition of it's own body gives you output ; complex but short code.
    As compared to recursive function simple function when called runs specific commands inside it's body (could be loop but still simple as for command) it's decision criteria is small or let's say limited.
    In recursive function you make some kind of super function which has wide choice of features including that it's body is small.
    Definition: When a function calls itself or other functions while execution it is said to be recursive function.
    Here is a simple example : 
    To print a countdown form a digit that user inputs up to '0'
    1. #include <iostream>
    2. using namespace std;
    3. void sample_recursive_function(int N)
    4. {
    5.            if (N==0) { cout<<"N"<<endl; }
    6.          else  { cout<<"N"<<endl; --N; sample_recursive_function(N);}
    7. }
    8. int main()
    9. {
    10.           int N;
    11.           cout<<"Enter the number: ";
    12.           cin>>N;
    13.           sample_recursive_function(N);
    14.           return 0;
    15. }
    Enter the number: 5                                                                                                                                                       
    5                                                                                                                                                                                              
    4                                                      -                                                                                                                                      
    3                                                                                                                                                                                              
    2                                                                                                                                                                                              
    1                                                                                                                                                ^   --                                     
    0                                                                                                                                                                       -                     
    Press any key to continue . . . .                                                                                                                                  

    In this function there is not much as coding or might say complex coding but it is a complex logic. This is what is difference between these functions.
    This function can also call other functions described in this program hence you can generate loop in which more then 1 functions will work with each other to generate desired output;

                 A -------------> B
                                          |
                  |                             |
                  |                             |
                  |                            V
                D <-------------  C
    In this diagram functions are related to each other because when you call one function in another function the control goes to this function and once this function is finished then it will allow for control to go back to main called function..
    Here is an example :
    1.              int sum(int a , int b )                                      //f1
    2.             {            
    3.                         int c=  a+b;
    4.                         if  ( check_type(c)  == 1 )
    5.                        { return c;
    6.                         else
    7.                                 c+=1;
    8.             }

    9.             int check_type(int x)                                   //f2
    10.             {
    11.                       int check=x; 
    12.                      if ( x%2==0 ) 
    13.                       { return 1;} 
    14.                      if ( c%2!=0 )
    15.                        { return 0; }
    16.             }
    Here f1 is calling f2 to change it's output. A simple recursive function.
    Hope you understood the concept of recursive functions; gud luck; Allah HAfiz

    Colour Coding in C++

    15:37 By

    The first time i saw colour in c++ was amazing experience because just black  and white stuff is more like old school comparing to modern software and  technology. It was hard to find well defined code and definition for colours in c++ language ; took me a while but i got it finally so here is this code in simple language, hope it helps you...

    _________________________________________________________________________
    To change whole screen colour i.e. foreground (text) and background, it is very simple and         easy command.

    The header file for this command is :                   <windows.h>
    The command   is :                                          system("Colour _ _");

    In place of _ _ you have to enter colour combination you want ; first place takes background colour and 2nd place takes foreground colour (text colourthis is list of all colours available for this command:

    1_____Blue                                       A______Light Green
    2_____Green                                    B______Light Aqua
    3_____Aqua                                      C______Light Red
    4_____Red                                        D______Light Purple
    5_____Purple                                   E______Light Yellow
    6_____Yellow                                   F______Bright White 
    7_____White
    8_____Gray
    9_____Light Blue
    0_____Black

    It's not perfact colouring but it is simple to use

    1. #include <iostream>
    2. #include <windows.h>
    3. using namespace std;
    4. int main()
    5. {
    6.            system("Color 0A");  // first 0 = black background second A = light green
    7.            cout<<"This is my fev. colour combination for this command"<<endl;
    8.       return 0;
    9. }
    This is my fev. colour combination for this command              
    
    
    Press any key to continue . . .                                                 
    =========================================================================

    The detailed coding

    _________________________________________________________________________
    To change minor colour i.e. colour of just 1 character so that we can use as much colour combinations ; it comes to this code because command showed above will not allow you to print 2 or more colours in single output combination. This command is not capable of changing whole background so i prefer to select background colour from system command and then change text colour from this code i'm about to share with you guys.

    This must be wirten at start of main:   HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
    The header file for this command is :                   <windows.h>
    The command   is :                            SetConsoleTextAttribute(color, _ );


    In place of _ you have to enter colour code you want ; there is a brief list of some basic colours for this command:

    1_____Blue                                       10______Light Green
    2_____Green                                    11______Light Aqua
    3_____Aqua                                      12______Light Red
    4_____Red                                        13______Light Purple
    5_____Purple                                   14______Light Yellow
    6_____Yellow                                   15______Bright White 
    7_____White
    8_____Gray
    9_____Light Blue
    0_____Black


    1. #include <iostream>
    2. #include <windows.h>
    3. using namespace std;
    4. int main()
    5. {
    6.         HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE); //just once
    7.         SetConsoleTextAttribute(color, 10);
    8.            cout<<"This";
    9.         SetConsoleTextAttribute(color, 11);
    10.         cout<<" is";
    11.         SetConsoleTextAttribute(color, 12);
    12.         cout<<" a";
    13.         SetConsoleTextAttribute(color, 13);
    14.         cout<<" colour";
    15.         SetConsoleTextAttribute(color, 14);
    16.         cout<<" combination"<<endl;
    17.       return 0;
    18. }
    This is a colour combination                                                   
    
    
    Press any key to continue . . .                                                 

    But the best thing is that it doesnot stops here at 15 ; you can go up to 300 for different colour combinations for headline your text on your program; enjoy
    Like this post then i will tell you how to use 2nd one command very easily like this:
    clr( _ );
    that's it..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    Ascii Art with C++

    While posts on shaskaas are going on let me share another tweak with you. As a trademark thingy you can insert your name with ascii art in beginning of your program and this will always impress your teachers. I used star wars theme always for my assignments and you can bet the marks i got :P
    
    

    We use figglet for generating asscii art Figglet Fonts

    Then i would just cout this text

    /**
    _    __  .___  ___.      ___      .______      
    |  |  |  | |   \/   |     /   \     |   _  \     
    |  |  |  | |  \  /  |    /  ^  \    |  |_)  |    
    |  |  |  | |  |\/|  |   /  /_\  \   |      /     
    |  `--'  | |  |  |  |  /  _____  \  |  |\  \----.
     \______/  |__|  |__| /__/     \__\ | _| `._____|
      
    **/                                               
    
    Happy Coding :P

    Friday, November 15

    Program that converts ASCII number to character and vice versa By Umair Sajid

    12:14 By

    This Program can also be done using if-else structure but switch statement is most appropriate. 


    #include<iostream>  /* Header Files */
    #include<stdlib.h>    
    #include<conio.h>    
    using namespace std; 
    int main() 
      int number,option; 
      char letter; 
     cout<<"1.Convert ASCII Value to Character "<<endl;
     
     cout<<"2.Convert Character to ASCII Value "<<endl;
     
     cout<<"Enter Your Choice :"; cin>>option;
     
     switch(option) 
     { 
     case 1:
       cout<<"Enter The Number :"; 
       cin>>number; 

       cout<<"The Corresponding character is :"<<char(number)<<endl;  break;
     
     case 2: 
       cout<<"Enter The Letter :";
       cin>>letter; 

       cout<<"ASCII Value of the letter is:"<<int(letter)<<endl; 
     break;
     
     default:
     cout<<"*** INVALID OPTION ***"; 
     break; 
     } 
     getch();
     }
    
    




    Wednesday, November 13

    Two Dimension Pointers C++

    Pointers are very useful construct of C++ programming language and what is more useful is 2d pointers.

    The pointers are special variables pointing to a memory location or a variable where as an array is a collection of those variables. A 2d pointer is a pointer which is pointing to array of pointers.

    e.g.

    //Assign an array of 5 nameless pointers to a 2d pointer
    int **pointer = new int*[5];

    //then assign values to each array
    for(int i=0;i<5; i++)
    {
        pointer[] = new int[10];
    }

    //now we have a 2d pointer which can be also accessed as array
    pointer[2][9] ; //this is third row and 10th element

    Tuesday, November 12

    Arrays in C++

    01:36 By

    Hey Guys.
    This blog is related to a datatype in c++ that you'll find enormously helpful in your future assignments and semester projects.

    So, to answer the very first question : "What is an Array ?"
    Formally, An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
    Confused ? I'd be surprised if you weren't.

    Think of it like this, you have 5 numbers. Now as a beginner in C++, i'll make ten different variables and the store them in separately in these variables. Like this :

    int a=1;
    int b=5;
    int c=11;
    int d=19;
    int e=30;
    
    Now in order to access these stored values, we'll have to call each element by its distinct name.

    By using array, we can use a single datatype to store multiple related elements within a closed structure. This makes it easier for us to access the values and acquire less space in the memory (10 elements in an array use less space than 10 different variables).
    A typical way to initialize an array in c++ is :

    datatype name [no. of elements] ;


    //we shall now declare some arrays
    
    //Suppose we want to make an array of 10 integer numbers. 
    int array1[10];
    
    // we can assign elements to the array like this:
    int array1[10] = {2,4,6,8,10,12,14,16,18,20};
    float array2[5] = {3.14, 2.27, 0.6990, 4.5, 123.4};
    string array3[3] = {"Porsche", "Lamborghini" , "Maserati"};
    
    //Note that in an array of 10 elements, values are indexed as 0-9
    
    
    // now we can access the values in the array using their index number
    print(array1[0]); // 0 is the index number of the element in the array
    // we'll get 2 in the output
    
    //similarly 
    
    print(array2[3]); //This will print us 4.5
    print(array3[1]); //This will print us Lamborghini
    
    
    //In order to print every element of the array, we use for loop
    for (int i=0; i<10; i++)
    {
    print(array1[i])
    print(endl);
    }
    //This will print every element in the array1
    //value of i will determine the index number of the element to be printed
    
    //P.s. use cout statement instead of print command :)

    The concept of arrays is very helpful for data structures.
    You get a very good idea of memory management in c++. This concept will help you guys a lot in your 1st semester projects. Be it small databases, games, or anything. You'll always find arrays quite helpful.

    Until next time.

    Monday, November 11

    Diamond Printing Problem from Uxama Ahmad

    20:31 By

    The problem was diamond did not print odd rows with required spaces and stars.
    #include <iostream>
    using namespace std;
    int main()
    {
     int num,i=1,j=1,k=1,space,star;
     cout<<"Enter odd number of rows: ";
     cin>>num;
     if(num%2==0)
      num=num-1;
    
     space=num/2;
     star=1;
     cout<<endl;
     do
     { 
      if(space!=0)
       do //prints spaces
       {
    
        cout<<" ";
        k++;
       }while(k<=space);
       k=1;
       do //prints stars
       {
        cout<<"*";
        j++;
       }while(j<=star);
       j=1;
       cout<<endl;
    
       i++; //current row number for diamond
       if(i==(num/2)+1)
       {
        space=0;
        star=num;
       }
    
       if(i<=num/2)
       {
        star=star+2;
        space--;
    
       }
    
       if(i>1+num/2)
       {
        star=star-2;
        space++;
       }
    
       //star=2*i-1;
     }while(i<=num);
     return 0; 
    }
    
    sample output

    Sunday, November 10

    Function Pointers : the power of C

    14:59 By

    Function pointers are a very powerful yet misunderstood aspect off programming languages.
    
    
    A function pointer is simply a points to the address of a function in memory instead of pointing to a data variable like a regular pointer.When dereferenced, a function pointer can be used to invoke the function it points to and pass it arguments just like a normal function call. Such an invocation is also known as an "indirect" call, because the function is being invoked indirectly through a variable instead of directly through a fixed name or address.
    So, now why is such a simple construct so powerful? Its because the i
    t allows us as developers to do things which wouldn't be possible without function pointers, a simple example is that a function pointer allows us to replace very long switch statements.
    let us take the following code
    float Add(float a, float b) { return a+b; }
    float Sub(float a, float b) { return a-b; }
    float Mul(float a, float b) { return a*b; } 
    float Div(float a, float b) { return a/b; }
    //now we write a function thattakes 3 parameters first two are floating point
    // numbers and third is a function pointer
    float MathOp(float a, float b, float (*FuncPtr)(float, float))
    {
           return FuncPtr(a,b);
    }
    
    int main()
    {
         //Now we can simply call MathOp with appropriate arguments
         float x= MathOp(10,5,&Mul);
         cout<<x<<endl;
          return 0;
    }
    In the above code the important thing is the declaration of a function pointer.  float (*FuncPtr)(floatfloat)
    Let us break it down.
    first is the return type of the function pointed to by the function pointer. which is this case is float.
    Second is part is (* FunctionPointerIdentifier) . The parentheses and the * are the syntax declaring it as a function pointer and must not be confused as anything else. The asterisk is followed by any identifier for the function pointer.
    The third part is the parameter list of the function pointed to by the function pointer.

    Thus a function pointer can point to any function that has the same return type and parameter list as those declared in that function pointers declaration So,all functions, you want to use with the same function pointer, must have the same parameters and return-type!

    This was just a very basic introduction to Function Pointers. There are a lot of other things that make function pointers such a powerful tool.

    Functions Continued (Functions Input)

    02:51 By

    Input in functions : type of input

    We can use functions by 2 methods of input:
    • input as passing arguments by values [Part 1;]
    • input as passing arguments by reference [Part 2;]
    Part 1; using function : passing arguments by value
    Problem 1; scope of variable and functionality
    • In this type, we give function the value of a variable not that variable...!!!
    • Function creates a local scope variable (local to the functions body only)
    • It does not change the value of original variable.
    • Say we ran an square function which takes argument by value and argument is x
    • When function is called it reserves  memory of ram and makes a copy of value of x and runs operations on this copy
    • In the end when it returns value then function is removed from memory so is that copy of variable
    • Original variable remains intact
    • This is how scope of variable is defined
    Problem 2; returning complex values
    Say we have to make a function which takes 5 variables and returns separate value
    How to return this: compare those variables for largest one? Obviously we can't return this:
    return if (num1>num2&&num1>num3){num1}; and if we have to put all five variables confirmation statements?
    Here is a tip: make a local scope variable which will collect all those values which has to be returned in the end i.e.
    _________________________________________________________________________________
    int result; //this variable only exists while function is working when it is done this variable                                        
                         will be removed so it is a local variable to this function only / must not match existing one
    if (num1>num2&&num1>num3){result=num1;}
    if (num2>num3){result=num2;}
    else {result=num3;}
    return result;
    _______________________________________________________________________
    Very easy!
    Now every time you can just create your required variable and save returning values in it and return it easily....!!!!
    ==================================================================================================================================================
    Till next time.. Please let me know your problem with syntax or logic or error so i can make better understanding in the article...!!!
    Do comment in section below :)

    Saturday, November 9

    Factorial Using Recursive Function

    20:56 By

    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    
    
    int Factorial(int number)
    {
     if (number <2)
     {
      return 1;
     }
     else
     {
      return (number * Factorial(number - 1));
     }
    }
    
    void main()
    {
     int num;
     cout << "Enter Number: ";
     cin  >> num;
    
     int fact = Factorial(num);
     cout << "\nFactorial: " << fact;
    
     _getch(); //use 'getch()' if you are using version older than Visual Studio 2013.
    }

    Friday, November 8

    Macros In C++ By Umair Sajid

    Macros In C++ By Umair Sajid

    Macros : are pre-processor directives Just As

                                    #include<stdio.h>
                 #include<graphic

                 #include<conio.h>
                 #include<malloc.h>
                 #include<stdlib.h>
                 #include<dos.h>
                 #include<iostream> 


    but In Above Directive Directs The Compiler to include followings library functions . However We Are Concern with the Macros.

    Syntax:               #define ADD x+y

      Actually They Can Be think As Same As Functions But Just Defined Before Main and with A little difference of Syntax and working. In a Nutshell  You can Call Macros as Functions Using As A Variable

    Use: Macros In C++ Is Usually Used Where We Required Fast Execution. However The Same Work Can be Done By Using Functions But The Function can pass arguments and require a return_type of the Integers or Result Return.

    Lets Come To A Simple Program That prompt the user to enter a number and prints its cube and square using macros.

    #include<iostream>
    #define SQUARE num*num      // MACROS
    #define CUBE num*num*num    // MACROS
    using namespace std;
    int main()
    {
        int num;
        cout<<"\n\t\tEnter A Number : "; // prompt to enter a number
        cin>>num;               // store it in num

        cout<<"\n\t\tThe Square of "<<num<<" is : "<<SQUARE<<"\n";   // Just as function call we have used
        cout<<"\n\t\tThe Cube of "<<num<<" is :"<<CUBE;                     // macros
        cout<<"\n\n";
        cout<<"\n\t\t***THANKYOU PC Umair Sajid ****\n\n";
    }

    Increment Decrement Operators

    C++ has a great feature of increment and decrement operators.
    increment means to increase value and decrement means to decrease value.

    e.g.
    int apples = 10 ; 
    
    apples = apples + 1 ;     //increments by 1
    
    apples = apples - 1 ;     //decrements by 1 
    
    //now apples ==10
    
    
    
    //in C++ we can do this more easily with increment and decrement operators.
    
    
    
    apples++; //increments
    
    apples--; //decrements
    
    
    
    Type of increment/decrement operators
    
    
    
    PostFix: it changes value of variable after rest of the statement is executed.
    
    Prefix: it changes value before any other calculation.
    
    
    
    e.g.
    
    
    
    int oranges = apples++;
    
    /*
    
    oranges will now be 10 and after execution apples value will increase by 1 so  apples=1
    
    */
    
    //similarly this will result in 10 oranges and 10 apples
    
    oranges = --apples; //first apples will be set to 10(11-1) and then oranges = apples = 10;
    
    
    
    /* We can see in the above example that --apples/++apples or vice versa changes the value of apples. The compiler reads the following line : */
    oranges = ++apples ;
    // change apples = apples+1. Then set the value of Oranges = apples (new value)
    
    
    /*So the statement has altered the original value of apples.
    // If we do not want to change the value of apples and still set the value of oranges, then we do the following : */
    
    
    oranges = apples+1;
    //So the value of apples remains intact and +1 value is set for oranges :)