Monday, August 26, 2019

1-D Array in C


Arrays:
Ø  An array is defined as the collection of similar type of data items stored at contiguous memory locations.
Ø  An array is a group (or collection) of same data types.
Ø  An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.
Ø  An array is also known as a subscripted variable. 
Ø  Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.
Ø  Each item in the array is called an element.
Ø  Every element of an array is identified by a number and is known as index / subscript of an array element.
Ø  Array element index always start with zero (0).

C language support two types of array
1)     One Dimensional Array
2)     Multidimensional Array

One Dimensional
The array which have only one dimension i.e. elements in row or column fashion is non as one dimensional array.

Declaration of array:
The array variable can be declared as
dataType arrayName[arraySize];
Example
int marks[5];

in above example int is a data type,
marks is a name of array, number in square bracket indicates the total number of elements to be stored in an array. i.e. size of an array.


Initialization of array elements:

              Array elements can be initialized in two different ways.
1)     At the time of initialization

int marks[5] = {44,55,66,44,33};

2)     After declaration of array variable, using index of array

marks[0] = 44;
marks[1] = 55;
marks[2] = 66;
marks[3] = 44;
marks[4] = 33;
To access array elements one can use any looping construct as shown in example below.

Example 1: initialize array elements and display using for loop.
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        int marks[5] = {44,55,66,33,55};
        clrscr();
        for(i=0;i<5;i++)
        {
           printf("\n %d",marks[i]);
        }
        getch();
}

Example 2: Read array elements and display them.
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        int marks[5];
        clrscr();
        printf(“Enter 5 elements of an array”);
        for(i=0;i<5;i++)
        {
                 scanf(“%d”,&marks[i]);
        }
        for(i=0;i<5;i++)
        {
           printf("\n marks[%d] = %d",i, marks[i]);
        }
        getch();
}

 Example 3: Read array elements and display total of them.
#include<stdio.h>
#include<conio.h>
main()
{
        int i, tot=0;
        int marks[5];
        clrscr();
        printf(“Enter 5 elements of an array”);
        for(i=0;i<5;i++)
        {
                 scanf(“%d”,&marks[i]);
        }

        for(i=0;i<5;i++)
        {
           tot = tot + marks[i]);
        }

        printf("\n Sum of all elements of an array is %d”,tot);
        getch();
}

Looping Statements in C


Looping statements:
Types of Loops
Depending upon the position of a control statement in a program, a loop is classified into two types:
1. Entry controlled loop
2. Exit controlled loop

In an entry controlled loop, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.

The control conditions must be well defined and specified otherwise the loop will execute an infinite number of times. The loop that does not stop executing and processes the statements number of times is called as an infinite loop. An infinite loop is also called as an "Endless loop."
Following are some characteristics of an infinite loop:
1. No termination condition is specified.
2. The specified conditions never meet.
The specified condition determines whether to execute the loop body or not.

The while statement
It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.
After exiting the loop, the control goes to the statements which are immediately after the loop. The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body.
In while loop, if the condition is not true, then the body of a loop will not be executed, not even once. 

Syntax:
while (condition)
{
   statements;
}

Example:
#include<stdio.h>
#include<conio.h>
main()
{
        int num=1;       //initializing the variable
        clrscr();
        while(num<=10)   //while loop with condition
        {
                 printf("%d\n",num);
                 num++;           //incrementing operation
        }
        getch();
}

The do … while statement
It is an exit-controlled loop. The do…while loop checks the condition at the end of the loop. This means that the statements inside the loop body will be executed at least once even if the condition is never true. The do…while loop is mainly used in the case where we need to execute the loop at least once. The do…while loop is mostly used in menu-driven programs where the termination condition depends upon the end user.

Syntax:
do
{
   statements;
} while (condition);

Example:
#include<stdio.h>
#include<conio.h>
main()
{
        int num=1;       //initializing the variable
        clrscr();
        do
        {
                 printf("%d\n",num);
                 num++;           //incrementing operation
        } while(num<=10);        //while loop with condition
        getch();
}

In the do…while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop.

The for statement
A for loop is a more efficient loop structure in 'C' programming.  Like while loop it is an entry-controlled loop. The main advantage of using this loop is that, one can specify initialization, condition and increment / decrement operation on single statement.
The general syntax of for loop is

Syntax:
for (initialization; condition; incrementation or decrementation )
{
  statements;
}
Ø  The initialization of the for loop is performed only once.
Ø  The condition is a Boolean expression that tests and compares the counter to a fixed value after each iteration, stopping the for loop when false is returned.
Ø  The incrementation/decrementation increases (or decreases) the counter by a set value.
Example 1:
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        clrscr();
        for(i=1;i<=10;i++)       //for loop to print 1-10 numbers
        {
                 printf("\n I = %d",i);           //to print the number
        }
        getch();
}

Example 2:
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        clrscr();
        for(i=10;i>=0;i--)       //for loop to print 10 to 0 numbers
        {
                 printf("\n I = %d",i);           //to print the number
        }
        getch();
}

Example 3:
#include<stdio.h>
#include<conio.h>
main()
{
        int i,no;
        clrscr();
        printf(“Enter a number for Multiplication Table ”);
        scanf(“%d”,&no);
        for(i=1;i<=10;i++)       //for loop to print 1-10 numbers
        {
                 printf("\n %d",i*no);            //to print the number
        }
        getch();
}

Example 4:
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        clrscr();
        for(i=1;i<=10;i++)      
        {
                 if(i%2==0)
                 {
                         printf("\n Even Number = %d",i);
                 }
                 else
                 {
                         printf("\n Odd Number = %d",i);
                 }
        }
        getch();
}