Wednesday, 22 July 2015

Arrays Simple Understanding

                                             

                                    ARRAYS USE IN C++


Arrays Definition:

                                               In programming, one of the frequently arising problem is to handle similar types of data. Consider this situation: You have to store marks of more than one student depending upon the input from user. These types of problem can be handled C++ programming(almost all programming language have arrays) using arrays.
An array is a sequence of variable that can store value of one particular data type.

Defining Arrays

type array_name[size];
Consider this code.
int test[85];
The above code creates an array which can hold 85 elements of int type.

Array Elements

The maximum number of elements an array can hold depends upon the size of an array. Consider this code below:
int age[5];
This array can hold 5 integer elements.
Elements of an array in C++ Programming
Notice that the first element of an array is age[0] not age[1]. This array has 5 elements and notice fifth is age[4] not age[5]. C++ programmer should always keep this is mind as it may differ from other programming languages.

Array Initialization

Array can be initialized at the time of declaration. For example:
float test[5] = {12, 3, 4, -3, 9};
It is not necessary to write the size of an array during array declaration.
float test[] = {12, 3, 4, -3, 9};
I prefer writing size of array during array declaration because it quickly gives the information on size of an array while help me in debugging code.

Example 1: C++ Array

C++ Program to store 5 numbers entered by user in an array and display first and last number only.
#include <iostream>
using namespace std;

int main() {
    int n[5];
    cout<<"Enter 5 numbers: ";
/*  Storing 5 number entered by user in an array using for loop. */
    for (int i = 0; i < 5; ++i) {
        cin>>n[i];
    }
    
    cout<<"First number: "<<n[0]<<endl;  // first element of an array is n[0]
    cout<<"Last number: "<<n[4];        // last element of an array is n[SIZE_OF_ARRAY - 1]
    return 0;
}
Output
Enter 5 numbers: 4
-3
5
2
0
First number: 4
Last number: 0

Wednesday, 1 July 2015

Functions understanding in C++

                                   Functions in C++

    A function can be defined as a set of statement used to achieve a particular task. though we know what it means lets understand what can we use a function for.

    Why do you need functions ?

    Suppose you are running in a marathon at a point of time during the run you become thirsty and you stop running, take a sip of water and then start running again .
    similarly in c++when a program is being executed it is executed sequentially i.e line by line , now when compiler encounters  a function call, it pauses the  execution (similar to stops running )at that point runs the new function (like drinking water )and when it is complete ,it returns to is original state (starts running again).

    Declaration

    when we write a variable we write int x , where x is the name of the variable and int is its data type , declaration of a function is something similar first we declare what will the data type of the return (to be covered shortly) – in this case int  , and second is whatever name we want to give to the function.

    Syntax of declaration:

    datatype variablename(arguments);
    int myfunction(int n);
    
    This prototype specifies that in this program, there is a function named "myfunction" which takes a single integer argument "n" and returns an integer. Elsewhere in the program a function definition must be provided if one wishes to use this function. It's important to be aware that a declaration of a function does not need to include any arguments. The following is a argument-less function declaration, which just declares the function name and its return type, but doesn't tell what parameter types the definition expects.
    double myfunction();

    Defining a Function:

    Syntax of defining:

    int myfunction(int n)
    {
    Function Body

    }

    Calling a function:

    Syntax of calling:

    myfunction(int n);
    {
    Function method

    }

    Example of a function:

    // FunctionFactorial.cc
    // Program to calculate factorial of a number with function call
    
    #include <iostream>
    using namespace std;
    
    // Function declaration (prototype)
    int Factorial(int M); 
    
    int main()
    {
       int number=0, result;
    
       // User input must be an integer number between 1 and 10
       while(number<1 || number>10)
       {
         cout << "Integer number = ";
         cin >> number;
       }
    
       // Function call and assignment of return value to result
       result = Factorial(number);  
    
       //Output result
       cout << "Factorial = " << result << endl;
       return 0;
    }
    
    

    Program to read two numbers from the keyboard and print the sum using a function

    #include <iostream>
    using namespace std;
     
    int sum(int a, int b); //declare the function
     
    int main() {
       int a = 0, b = 0;
       cout << "Enter a number ";
       cin >> a;
     
       cout << "Enter another number ";
       cin >> b;
     
       int c = sum(a, b); //call the sum function
     
       cout << "sum = " << c << endl;
     
       system("pause");
    }   
     
    int sum(int a, int b){
        return a + b;
    }
    
    
    I think it would be enough for the basic understanding of the functions.



    C++ easy understanding

                             Easy C++ Program Basics 


    • A simple C++ program:

    Let's use the code below to find what makes up a very simple C++ program - one that simply prints "Hello World!" and stops. Adjust your browser so you can see the text window below the code. Then point your mouse at different statements in the program.


    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     cout << "Hello World!" << endl;
     return 0;
    }
    
    
    
    
    iostream is a preprocessive directive need probably for all programs it is also called header files it helps to include the contents of the file. int main() is used to start the main program body.The body of the Program shoulad start and end with the curly braces as shown.
    
    
    {
     cout << "Hello World!" << endl;
     return 0;
    }
    
    
    • The above is the body of program.
    • it has cout<< it is used to show output to screen
    • which will display Hello World! and note that it should be in inverted commas as shown.
    • return 0; is used to savely end the program.

    Program to add to numbers:

    #include <iostream>
     
    using namespace std;
     
    int main()
    {
       int a, b, c;
     
       cout << "Enter two numbers to add\n";
       cin >> a >> b;
     
       c = a + b;
       cout <<"Sum of entered numbers = " << c << endl;
     
       return 0;
    }
    
    
    • Look here we add two numbers.
    • In body in the first we declare three integers.
    • int is data type which is integers all three numbers would be integers.
    • c integer would act as a result of addition of a and b.
    • than we use cout which would display sum of entered numbers.
    • In cin>> we give input of any two numbers, as cin>> allows to take input from the user.
    • I think you understand well.

    LOGICAL OPERATORS and RELATIONAL OPERATORS IN C++:

     Increment and decrement operator:

    Loops in C++ Language

    C++ programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.
    Loop TypeDescription
    Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
    Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
    Like a while statement, except that it tests the condition at the end of the loop body
    You can use one or more loop inside any another while, for or do..while loop.

    Loop Control Statements:

    Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
    C++ supports the following control statements. Click the following links to check their detail.
    Control StatementDescription
    Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
    Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
    Transfers control to the labeled statement. Though it is not advised to use goto statement in your program.

    The Infinite Loop:

    A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.
    #include <iostream>
    using namespace std;
     
    int main ()
    {
    
       for( ; ; )
       {
          printf("This loop will run forever.\n");
       }
    
       return 0;
    }
    When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C++ programmers more commonly use the for(;;) construct to signify an infinite loop.
    Now we will Discuss each tutorial one by one

    While Loop:

    #include <iostream>
    using namespace std;
     
    int main ()
    {
       // Local variable declaration:
       int a = 10;
    
       // while loop execution
       while( a < 20 )
       {
           cout << "value of a: " << a << endl;
           a++;
       }
     
       return 0;
    }
    When the above code is compiled and executed, it produces the following result:
    value of a: 10
    value of a: 11
    value of a: 12
    value of a: 13
    value of a: 14
    value of a: 15                  

    For Loop:  

    The syntax of a for loop in C++ is:
    for ( init; condition; increment )
    {
       statement(s);
    }
    Here is the flow of control in a for loop:
    • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
    • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
    • After the body of the for loop executes, the flow of control jumps back up to theincrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
    • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
    j#include <iostream>
    using namespace std;
     
    int main ()
    {
       // Local variable declaration:
       int a = 10;
    
       // do loop execution
       do
       {
           cout << "value of a: " << a << endl;
           a = a + 1;
       }while( a < 20 );
     
       return 0;
    }
    When the above code is compiled and executed, it produces the following result:
    value of a: 10
    value of a: 11
    value of a: 12
    value of a: 13
    value of a: 14
    value of a: 15
    value of a: 16
    value of a: 17
    value of a: 18
    value of a: 19
    Thats enough for the beginners of C++