An array is a derived data type. An array is an collection of similar data type having common characteristics such as name. Each data in array is placed at particular position by their index. Index in array start from 0 by default. We can create array of integer having only integer data. We can create array of character having only character data.In the same way float, Double and soon.All member of array is stored in incremental manner.
Type of Array
- One Dimension Array
- Two Dimension Array
- Multi Dimension Array
Note: We mostly used one dimension and two dimension.
Defining an Array
We define array in the same way as ordinary variable define. But in array we must specify the size of array. Size show how many number of element inside array.
Syntax for defining one dimension array
DataType array_name [length];
Example:
int marks [10];
char section [10];
Syntax for defining two dimension array
DataType array_name [length1][length2];
Example:
int marks [10][10];
char section [10][10];
Syntax for Initilizing one dimension array
DataType array_name [expression];
Example:
int marks [10]={50,45,35,50,55,20,30,34,39,40};
char section [10]={'A','B','C','D','E','F','G','H','I','J'};
Example 1: For One Dimension Array
#include <stdio.h>
#include<conio.h>
void main()
{
int marks[10]={50,45,35,50,55,20,30,34,39,40};
char section[10]={'A','B','C','D','E','F','G','H','I','J'};
for(int i=0;i<10;i++)
{
printf("%d-%c\n",marks[i],section[i]);
}
getch();
}
Out Put:
Example 2: For Two Dimension Array
#include <stdio.h>
#include<conio.h>
void main()
{
int matrix[3][3]={2,4,5,3,7,8,9,6,1};
printf("Matrix:\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
getch();
}
Out Put:
Assignments:
- Write a program to find maximum of an array.
- Write a program to find minimum of an array.
- Write a program to arrange input elements in ascending order.
- Write a program to find duplicate element from an array.
- Write a program to print two 3X3 matrix.
- Write a program to add two 3X3 matrix.
- Write a program to subtract two 3X3 matrix.
- Write a program to multiply two 3X3 matrix.
- Write a program to transpose of a matrix.
- Write a program to find diagonal elements of a matrix.
- Write a program to print * in following pattern.
-
Write a program to print numbers in following pattern.
No comments:
Post a Comment