C-Programming:-
In this tutorial yon will learn:
Array :-
Array is the collection of homogeneous data items that share a common name , Array contain similar kind of values on a same name it allocate contiguous or sequential memory.
* Declaration of an array:-
data type array name [ array size ] ;
for example,
float bookprice [ 5 ] ;
*Initialization of an array:-
You can initialize an array in both condition
1 Compilation time :-
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5] = { 1 , 2 , 3 , 4 , 5 };
Memory background like this :-
___________________arr______________
0
|
1
|
2
|
3
|
4
|
arr[0] arr[1]
arr[2] arr[3] arr[4]
2 On run time :-
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5];And after use scanf function to store all values :------
There are two main types of array are:-
1. Single dimension :- Single dimension array also known as 1D array which contain either row or column of a given size of memory.
syntax :-
Data type array name [ size ];
for example :-
int arr [5] ;
Memory background like this :-
_______________arr_________________
0
|
1
|
2
|
3
|
4
|
arr[0] arr[1] arr[2] arr[3] arr[4]
for example:-
* Write a program to accepting value in array and print :-
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5];
int i,j;
printf("enter 5 any values");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
printf("values od an array \ n");
for(j=0;j<5;j++)
{
printf("%d\n",a[i]);
}
getch();
}
OUTPUT
enter 5 any values 12
3
4
5
values of array 1
2
3
4
5
In an array there are two types condition are:-
* Under flow condition :- In this condition , the no. of values is less than the size of an array.
* Over flow condition :- In this condition , the no of values is more than the size of an array.
Note point :-
* On compilation time , by default value become 0 if cells are empty.
* On run time , by default value become garbage values if cells are empty.
2 double dimension array:- 2D array is a double dimension array having row and column
Syntax :-
Data type array name [row] [column] ;
int arr[3][3] ;
__________rows_____________
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
column
Let see one example to more clear this topic:-
* write a program to create 3 x 3 matrix accept the values in matrix and print the values in matrix format:-
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[3][3] ;
printf("enter the value of 3 x 3 matrixs");
for(i=0 ; i<3 ; i++)\
{
for(j=0 ; j<3 ; j++)
{
scanf("%d",& arr[i][j] ) ;
}
}
printf("Array values are");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
printf("%d", arr[i][j] ) ;
}
printf(" \ n");
}
getch();
}
OUTPUT:-
enter the values of 3 x 3 matrix 1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
#include<stdio.h>
#include<conio.h>
int sum (int [5] ) ;
void main()
{
int arr[5] ;
int i , s ;
printf("Enter any 5 values ");
for(i=0 ; i<5 ; i++)
{
scanf("%d",&arr[i] );
}
s= sum ( a ) ; OR s=sum( a [ ] ) ;
printf("%d",s);
getch();
}
int sum( int b [5] )
{
int i,s=0;
for(i=0 ; i<5 ; i++)
{
s=s+b [i] ;
}
return(s);
}
Array and function:-
Array function in C is a data structure that hold multiple elements of the same data type. The size of an array is fixed and the elements are collected in a sequential manner .let see one example to more clear this topic :-
#include<stdio.h>
#include<conio.h>
int sum (int [5] ) ;
void main()
{
int arr[5] ;
int i , s ;
printf("Enter any 5 values ");
for(i=0 ; i<5 ; i++)
{
scanf("%d",&arr[i] );
}
s= sum ( a ) ; OR s=sum( a [ ] ) ;
printf("%d",s);
getch();
}
int sum( int b [5] )
{
int i,s=0;
for(i=0 ; i<5 ; i++)
{
s=s+b [i] ;
}
return(s);
}
Thanks for reading this post and if you wanna know more to related this topic
read m next post ,......thankyou for your support...........
0 comments:
Post a Comment
If you have any doubt, please let me know