Tutorial references

c
c++
c#

Tutorial references

chapters

Array

Array

Array is type of variable which contains one or more than one values in c language.

In c language array contains same type values.

There are three types of array:

(1.) 1-D Array

(2.) 2-D Array

(3.) Multidimensional Array

1-D Array

It is type of array

Index always start from zero(0).

First value always assign into zero(0) index

A single index of array contains only one value.

Syntax 1

data-type arrayName[ ] = {value1, value2, value3,.................};

If array data-type is int then array will contain only int type values

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[]={10,20,30,20,10,50,20};
 clrscr();
 printf("\n This is a 1-d array program.");
 printf("\n value of arr[1] = %d ",arr[1]);
 getch();
}

Run program

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[]={10,20,30,20,10,50,20};
  int i;
 clrscr();
 printf("\n This is 1-d array program.");
 for(i=0;i<7;i++){
 printf("\n value of arr[%d] = %d",i,arr[i]);
 }
 getch();
}

Run program

Syntax 2

data-type arrayName[size] = {value1, value2, value3,.................};

size tells maximum how many values you can hold in array.

If "number of values <= size" then array holds values. Otherwise it can give syntax error.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[7]={10,20,30,20,10,50,20};
  int i;
 clrscr();
 printf("\n This is 1-d array program.");
 for(i=0;i<7;i++){
 printf("\n value of arr[%d] = %d",i,arr[i]);
 }
 getch();
}

Run program

Syntax 3

data-type arrayName[size];

Example

#include <stdio.h>
#include <conio.h>

void main(){
 int arr[7];
  int i;
 clrscr();
 printf("\n This is 1-d array program.");

 arr[0] = 100;
 arr[1] = 200;
 arr[2] = 300;
 arr[3] = 200;
 arr[4] = 400;
 arr[5] = 100;
 arr[6] = 500;

 for(i=0;i<7;i++){
 printf("\n value of arr[%d] = %d",i,arr[i]);
 }
 getch();
}

Run program

In below, you can use 1-D array dynamically.

In below, you can read values from user into 1-D array.

Example

array_1d_read

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[7];
  int i;
 clrscr();
 printf("\n This is 1-d array program.");

 for(i=0;i<7;i++){
 printf("\n Enter value for arr[%d] = ",i);
 scanf("%d",&arr[i]);
 }

 for(i=0;i<7;i++){
 printf("\n value of arr[%d] = %d",i,arr[i]);
 }
 getch();
}

Run program

2-D Array

It is a type of array.

It is called 2-dimensional array.

Syntax 1:

data-type arrayName[ size ][ size ] = {
                                       { value1, value2, value3,...},
                                       { value1, value2, value3,...},
                                       ..............
                                       ..............
                                    };

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[2][3] = {
                   {10, 20, 30},
                   {70, 60, 50}
               };
 int i,j;
 clrscr();
 printf("\n This is 2-d array program.");
 for(i=0;i<2;i++){
  for(j=0;j<3;j++){
   printf("\n value of arr[%d][%d] = %d",i,j,arr[i][j]);
  }
 }
 getch();
}

Run program

Syntax-2:

data-type arrayName[ size ][ size ];

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[2][3];
  int i, j;
 clrscr();
 printf("\n This is 2-d array program.");

 arr[0][0] = 100;
 arr[0][1] = 200;
 arr[0][2] = 300;
 arr[1][0] = 200;
 arr[1][1] = 400;
 arr[1][2] = 100;

 for(i=0;i<2;i++){
  for(j=0;j<3;j++){
   printf("\n value of arr[%d][%d] = %d",i,j,arr[i][j]);
  }
 }

 getch();
}

Run program

Below program is read data from user. It means below program is a 2-dimensional dynamic array.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[2][3];
  int i, j;
 clrscr();
 printf("\n This is 2-d array program.");

 for(i=0;i<2;i++){
  for(j=0;j<3;j++){
   printf("\n Enter value for arr[%d][%d] : ",i,j);
   scanf("%d",&arr[i][j]);
  }
 }

 for(i=0;i<2;i++){
  for(j=0;j<3;j++){
   printf("\n value of arr[%d][%d] = %d",i,j,arr[i][j]);
  }
 }

 getch();
}

Run program

Multidimensional Array

Multidimensional is also a type of array.

According to below syntax you can go more deeply but "3-dimension" array can hold complex data. Therefore you have not need to go more deeply but you have need then you can go deeply.

Syntax-1:

data-type arrayName[ size ][ size ][ size ] = {
                                                {
                                                  { value1, value2,...},
                                                  { value1, value2,...}
                                                },
                                                {
                                                  {value1, value2,...},
                                                  {value1, value2,...}
                                                }
                                            };

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[2][2][3] = {
                   {
                     {10, 20, 30},
                     {70, 60, 50}
                   },
                   {
                     {99, 98, 97},
                     {22, 33, 44}
                   }
              };
 int i,j,k;
 clrscr();
 printf("\n This is multidimensional array program.");

 for(i=0;i<2;i++){
  for(j=0;j<2;j++){
   for(k=0;k<3;k++){
   printf("\n value of arr[%d][%d][%d] = %d",i,j,k,arr[i][j][k]);
   }
  }
 }

 getch();
}

Run program

Syntax-2:

data-type arrayName[ size ][ size ][ size ];

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[2][2][3];
 int i,j,k;
 clrscr();
 printf("\n This is multidimensional array program.");

 arr[0][0][0] = 101;
 arr[0][0][1] = 201;
 arr[0][0][2] = 301;
 arr[0][1][0] = 401;
 arr[0][1][1] = 502;
 arr[0][1][2] = 601;
 arr[1][0][0] = 701;
 arr[1][0][1] = 801;
 arr[1][0][2] = 901;
 arr[1][1][0] = 111;
 arr[1][1][1] = 121;
 arr[1][1][2] = 551;

 for(i=0;i<2;i++){
  for(j=0;j<2;j++){
   for(k=0;k<3;k++){
   printf("\n value of arr[%d][%d][%d] = %d",i,j,k,arr[i][j][k]);
   }
  }
 }

 getch();
}

Run program

In below program you can read data from user. It means below program make a dynamic multidimensional array.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int arr[2][2][3];
 int i,j,k;
 clrscr();
 printf("\n This is multidimensional array program.");

 for(i=0;i<2;i++){
  for(j=0;j<2;j++){
   for(k=0;k<3;k++){
   printf("\n Enter value for arr[%d][%d][%d]: ",i,j,k);
   scanf("%d",&arr[i][j][k]);
   }
  }
 }

 for(i=0;i<2;i++){
  for(j=0;j<2;j++){
   for(k=0;k<3;k++){
   printf("\n value of arr[%d][%d][%d] = %d",i,j,k,arr[i][j][k]);
   }
  }
 }

 getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018