An
array is a collection of similar types of data element which holds fixed number
of values.
Initialization of 2D Array
Example:
Example:
For example an int array holds the elements of int types while a float array holds the elements of float types
Advantages:
Less code is required to access the data
We can access any element randomly using the array
We can sort the elements of array
Disadvantage:
Array size is fixed so we cannot change the value
Types:
There are 2 types of C arrays.They are
For example an int array holds the elements of int types while a float array holds the elements of float types
Advantages:
Less code is required to access the data
We can access any element randomly using the array
We can sort the elements of array
Disadvantage:
Array size is fixed so we cannot change the value
Types:
There are 2 types of C arrays.They are
1)
One dimensional
array
2) Multi dimensional array
2) Multi dimensional array
One dimensional array
Syntax:
Data_type array_name [array_Size];
Example
int marks[10];
Where int is the data_type, marks is the array_name and 10 is the array_size.
Note:
Array index is always started from 0.Its range between 0 to n-1.
Where n is the number of elements.0 is the lower index and n-1 is the upper index.
The size and type of arrays cannot be changed after its declaration.
Array index is always started from 0.Its range between 0 to n-1.
Where n is the number of elements.0 is the lower index and n-1 is the upper index.
The size and type of arrays cannot be changed after its declaration.
Declaration with Initialization
There are two ways to declare with initialize
the array. They are
1ST
Way:
int marks[5];//declaration of array
marks[0]=10;//initialization of array
marks[1]=20;
marks[2]=30;
marks[3]=40;
marks[4]=50;
2nd Way
We can
initialize the array at the time of declaration like
int marks[5]={10,20,30,40,50};
or
int marks[]={20,30,40,50,60};
Example:
#include<stdio.h>
#include<conio.h>
void main(){
int i=0;
clrscr();
int marks[5]={10,20,30,40,50};//declaration and initialization of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}
getch();
}
Output:
10
20
30
40
50
How to take 1D Array values from user in C
#include
<stdio.h>
#include<conio.h>
void main()
{
int marks[20], i, n;
clrscr();
printf("Enter n:\n ");
scanf("%d", &n);
for(i=0;
i<n; ++i) //Input statement
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
}
for(i=0; i<n;
++i) // Output statement
{
printf("Array is:%d\n",
marks[i]);
}
getch();
}
Example 2: Calculate average marks using
array in C
#include
<stdio.h>
#include
<conio.h>
void main()
{
int marks[10], i, n, sum = 0, average;
clrscr()
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d:
",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
getch();
}
Two Dimensional Array
Two dimensional array is know as matrix because it is represented by rows and columns.Two ,three or other dimensional arrays are also known as multidimensional arrays.
Declaration of 2D Array:
data_type array_name[row][column];
Example:
Two Dimensional Array
Two dimensional array is know as matrix because it is represented by rows and columns.Two ,three or other dimensional arrays are also known as multidimensional arrays.
Declaration of 2D Array:
data_type array_name[row][column];
Example:
int arr[2][3];
Where 2 is the row number and 3 is the column number.
Where 2 is the row number and 3 is the column number.
Initialization of 2D Array
int arr[2][3]={{1,2,3},{2,3,4}};
Example:
#include<stdio.h>
#include<stdio.h>
void main(){
int i,j;
clrscr();
int arr[2][3]={{1,2,3},{2,3,4}};
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
}
Output:
1 2 3
3 4 5
How to take 2D Array values from user in C
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[2][3];
int i, j;
clrscr();
printf("Enter array element \n");
for(i = 0; i < 2;i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d",
&arr[i][j]);
}
}
for(i = 0; i < 2; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t",
arr[i][j]);
}
printf("\n");
}
getch();
}
No comments:
Post a Comment