Arrays in C

Array in C

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

Copy Text
#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

Copy Text
#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:


No comments:

Post a Comment