View chapters from top of the tab for more information.
in the above table “ 0 “ is lower limit / lower bound
and “ 9 “ is the upper limit / upper bound
<data type> <variable> [size] , <variable>[size],…..
int a[10] , b[20] ;
float f[10];
char str[20];
<data type> <variable name> [size] = { val1, val2 , val3,..}
“here size is optional”
int a[5] = { 10, 20 , 30 , 40 , 50 };
int a[] = { 5 , 7 , 22 , 77 , 44 , 99 };
char s[] = { ‘S’ , ‘A’ , ‘U’ , ‘R’ , ‘A’ , ‘V’ };
char s[] = “SAURAV” ;
<variable name> [index] = <value> ;
int a[10];
a[0] = 10;
a[1] = 30;
a[2] = 50;
……….
……….
a[10] = 100;
<variable> [index]
int a[10] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 }
i.e,
a[5] = ? , that is 60.
a [1] = ? , that is 20.
a[ 9] = ? , that is 100.
a[0] = ? , that is 10.
a[3] = ? , that is 40.
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
int a[ 10 ] , i ;
printf( “ enter the 10 numbers “ ) ;
for ( i = 0 ; i < 10 ; i ++ )
{
scanf( “ %d “ , &a[i] );
}
printf( “ All elements are :- “ );
for ( i = 0 ; i < 10 ; i ++ )
{
printf( “ %d \n “ , a[ i ] ) ;
}
gethc() ;
}
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[ 10 ] , i ;
printf( “ enter the 10 numbers “ ) ;
for ( i = 0 ; i < 10 ; i ++ )
{
scanf ( “ %d “ , &a[ i ] ) ;
}
printf( “ All elements are \n “ ) ;
for ( i = 9 ; i >= 0 ; i-- )
{
printf( “ %d \n “ , a[ i ] );
}
getch();
}
Array in C |
- It is a special variable which is used to store multiple values of same type.
- It share common name for all values.
- It is also known as subscripted variable.
- The element of array is accessed with the help of index value . An index value is positive (+ve) integer value.
- The lower limit of array is zero “ 0 “ and upper limit is ( size -1 ) .
- The index value of array must be started with zero “ 0 “ .
- It is user defined data type.
➤Advantage of array :-
- It holds multiple value at a time.
- It provides simple operation as insertion , deletion, searching , sorting , etc.
- It shares common name for all values.
➤Disadvantage of array :-
- It supports static memory allocation.
- It is a technique in which memory is allocated during compile time . So in this case memory is wasted due to fixed size of array . It is major drawbacks of array but it can be avoided with the help of pointer.
- It stores same type of value but it can be removed with the help of structure.
⟹example:- int a[10]
10
|
20
|
30
|
40
|
50
|
60
|
70
|
80
|
90
|
100
|
0 1 2 3 4 5 6 7 8 9
|
in the above table “ 0 “ is lower limit / lower bound
and “ 9 “ is the upper limit / upper bound
➤Types of array :-
- One dimensional array / single dimensional array
- Two dimensional array
- Multi dimensional array
➤One dimensional array :-
- This type of array is use to hold multiple values in a single row.
- The array consists of only one subscript which represents size of array.
⏩syntax:-
<data type> <variable> [size] , <variable>[size],…..
⟹example:-
int a[10] , b[20] ;
float f[10];
char str[20];
⏩Initialization :-
⏩Syntax: during declaration time
<data type> <variable name> [size] = { val1, val2 , val3,..}
“here size is optional”
⟹Example:-
int a[5] = { 10, 20 , 30 , 40 , 50 };
int a[] = { 5 , 7 , 22 , 77 , 44 , 99 };
char s[] = { ‘S’ , ‘A’ , ‘U’ , ‘R’ , ‘A’ , ‘V’ };
char s[] = “SAURAV” ;
⏩Syntax : after declaration
<variable name> [index] = <value> ;
⟹Example:-
int a[10];
a[0] = 10;
a[1] = 30;
a[2] = 50;
……….
……….
a[10] = 100;
➧Accessing:-
⏩Syntax:-
<variable> [index]
⟹Example:-
int a[10] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 }
i.e,
10
|
20
|
30
|
40
|
50
|
60
|
70
|
80
|
90
|
100
|
0 1 2 3 4 5 6 7 8 9
|
a[5] = ? , that is 60.
a [1] = ? , that is 20.
a[ 9] = ? , that is 100.
a[0] = ? , that is 10.
a[3] = ? , that is 40.
P.Example:- Program to input 10 numbers after that printf it.
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
int a[ 10 ] , i ;
printf( “ enter the 10 numbers “ ) ;
for ( i = 0 ; i < 10 ; i ++ )
{
scanf( “ %d “ , &a[i] );
}
printf( “ All elements are :- “ );
for ( i = 0 ; i < 10 ; i ++ )
{
printf( “ %d \n “ , a[ i ] ) ;
}
gethc() ;
}
P.example:- Program to input 10 numbers after that print in reverse order.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[ 10 ] , i ;
printf( “ enter the 10 numbers “ ) ;
for ( i = 0 ; i < 10 ; i ++ )
{
scanf ( “ %d “ , &a[ i ] ) ;
}
printf( “ All elements are \n “ ) ;
for ( i = 9 ; i >= 0 ; i-- )
{
printf( “ %d \n “ , a[ i ] );
}
getch();
}
Great
ReplyDelete