Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Monday, 2 July 2018

C Array Types

In C programming array can be divided into two types :

1. Single dimensional array / Simple array / 1-D array / Linear array

2. Multidimensional array


# Single dimensional array :

When dimension is 1. or when there is a pair of square brackets with the name of array it is known as single dimension array . 

Declaration of single dimension array :

data type , variable name ;

# multidimensional array :

When there are number of pairs of square bracket . or , When there are more than one dimension it is known as multidimensional array . In C programming generally 2- D array are used for the implementation of matrices which is in forms rows and column . In double dimension array first dimension represent the no of rows and second dimension represent the no of columns . 

# Declaration of 2-D array : 

Syntax :    data type array name [ no of rows ] [ no of columns ] ;
                                            Or
                 data type  array name [ m ] [ n ] ;
                    Where  m = no of rows 
                                 n = no of columns
Example :
                 
int a [3] [3] ;
1 element = a [0] [0]
2 element = a [0] [1]
3 element = a [0] [2]
4 element = a [1] [0]
5 element = a [1[ [1]
6 element = a [1] [2]
7 element = a [2] [0]
8 element = a [2] [1]
9 element = a [2] [2]

# Input and output with double dimensional array :

# include <stdio.h>
void main ()
{
int a [3] [3] ;
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
{
printf ("\n Enter a number:") ;
scanf ("%d", & a[i] [j]) ;
}

#  Output with 2 - D array :

for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
printf ("%d", a [i] [j]) ;
printf ("\n") ;
}
}








Sunday, 1 July 2018

Array in C

 An array is collection of homogenous element ( of same types ) consist a common property and share  a common name . An array is also known as collection of homogenous element from ( 0 to n-1 ) where n represent the maximum number of element of array .    
An ordinary variable can contain only one value of one types it is not possible to assign more than one value at a single variables.

EXAMPLE :
                     Consider the following variable declaration and initialization .
                     int a = 10 ;
                     Here , a denote a single memory location with a value of 10.
                     If we update the value of variable .
                     a = 20 ;
                     Now the same memory location will contain the value 20 .
                    

Declaration of an array :

An array can be declared as an ordinary variable including the size specification in pair of [ ] bracket.

Syntax :   data type  variable name[ size ] ;
                        or
                data type  variable name [n] ;
              where n = number of elements

EXAMPLE 
                       int a[3] ;
                       In this example the name of an array is " a " which can contain maximum value of 3 at the 3 different locations .
 These 3 different memory location are known as 3 element in c programming , these 3 element are also termed as the 3 subscript . that's why array is also known as " sub scripted variable" . The three different types of variable of a can be written as follows :
                                                    1 element : a [0]
                                                    2 element : a [1]
                                                    3 element : a [2]

Initialization of an Array :


An array can be initialized in three different ways :
(1.)  Element by Element 
    Exam :   int a [3] ;
                    a[0] = 22 ;
                    a[1] = 5 ;
                    a[2] = 66 ;

(2,) Declaration and initialization at same time
    Exam :  int a [3] = { 11 , 22 , 66 } ;
it means 
            a [0] = 11 ;
            a [1] = 22 ;
            a [2] = 66 ;

(3.) Initialization from the user 
    Exam :  
              {
              int i ;
              int a [3] ;
             for ( i = 0 ; i < 3 ; i++ )
             {
             printf ("\n Enter a number:") ;
             scanf ("%d", & a [i]) ;
             }

Output :
              for ( i = 0 ; i < 3 ; i++ )
              printf ("\n %d", a [i]) ;

Program to Access  an array in C :

/* uses of array */
# include <stdio.h>
void main () 
{
int a[5] , i ;
clrscr () ;
for ( i = 0 ; i < 5 ; i++ )
{
printf ("\n Enter a number:") ;
scanf ("%d", & a [i]) ;
}
for ( i = 0 ; i < 5 ; i++ )
printf ("\n %d", a[i]) ;
getch ()
}


When above result is compiled :

Here as from loop statement is compiled then a condition of 5 is check out and a element of a[0] to a[5] is initialized from the user



       
             





 

             
                                                               

Friday, 22 June 2018

Program to find maximum of 5 element of an array in C using for loop statement

/* program of maximum */
# include <stdio.h>
void main ()
{
int a[5],i,max=0 ;
clrscr () ;
for ( i=0 ; i<5 ; i++ )
{
printf ( "\n Enter a number:") ;
scanf ("%d", & a[i]) ;
}
for ( i=0 ; i<5 ; i++)
{
if ( max < a[i] )
max = a[i] ;
}
printf ("\n max = %d", max) ;
getch () ;
}

Thursday, 21 June 2018

How to write program to find average of an array of 5 elements

/* Avg of an array */
# include <stdio.h>
void main ()
{
int a[5],i,avg ;
int sum = 0 ;
clrscr () ;
for ( i=o ; i<5 ; i++ )
{
printf ("\n Enter a number:") ;
scanf ("%d"< & a[i]) ;
}
for ( i=0 ; i<5 ; i++ )
sum = sum + a[i] ;
avg = sum / 5 ;
printf ("\n avg= %d", avg) ;
getch () ;
}


DRY RUN ;

sum =0 i=0 0<5
a[0] = 11
i=0 0<5 (TRUE)   i++ = 1
sum = sum + a[i]

similarly
for  i = 1,2,3,4  having true

for i = 5 ( 5<5 ) false

Featured post

Kalgudi Interview Round!!

Those who cleared the second round of kalgudi got an email from exceller to complete their Kalgudi Interview Round. So here we will discuss ...