Pages

Saturday, 7 March 2020

Array in C

Array : The array is a data structure, which can store a fixed-size sequential collection of elements of the same data type.
Syntax:
datatype   array_name [ array_size ] ; 
Examples:
int n[6];






n[ ] is used to denote an array 'n'. It means that 'n' is an array.
We can also declare an array by another method.
int n[ ] = {2, 3, 15, 8, 4, 23};
element
2
3
15
8
4
23
index
0
1
2
3
4
5

E
xamples:
int mark[5] = {19, 1, 8, 7, 9};

We can also initialize an array like this.

int mark[] = {19, 1, 8, 7, 9};

Example 1: Array Input/Output

#include <stdio.h>
int main()
{
  int a[5];
  printf("Enter 5 integers: ");
  for(int i = 0; i < 5; ++i)
{
     scanf("%d", &a[i]);
   }
   printf("Displaying integers: ");
for(int i = 0; i < 5; ++i)
 {
      printf("%d\n", a[i]);
    }
  return 0;
}

Example 2:
#include <stdio.h>
void show(char ch)
{
   printf("%c ", ch);
}
int main()
{
   char arr[ ] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
   for (int x=0; x<10; x++)
   {
       show(arr[x]);
   }
   return 0;
}

2D Arrays

Syntax:   datatype array_name[size1][size2];
Example:  
int x[2][4];Here, 'x' is a 2D array of integers which consists of 2 rows and 4 columns.
It is like

Column[0]
Column[1]
Column[2]
Column[3]
Row[0]
a[0][0]
a[0][1]
a[0][2]
a[0][3]
Row[1]
a[1][0]
a[1][1]
a[1][2]
a[1][3]

Example: 
int a[2][3] = { 1, 2, 3, 4, 5, 6 };

or

int a[2][3] = {
                       {1, 2, 3},
                       {4, 5, 6 }
                      };
Let's consider different cases of assigning values to an array at the time of declaration.
int a[2][2] = { 10, 20, 30, 40 };       /* valid */
int a[  ][2] = { 10, 20, 30, 40 };        /* valid */
int a[2][  ] = { 10, 20, 30, 40 };        /* invalid */
int a[  ][  ] = { 10, 20, 30, 40 };        /* invalid */
Multi-dimensional array
Syntax:
datatype array_name[size1][size2]...[sizeN]
Example:
int a[5][10][4];
double c[3][2][4]={
                             {{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}},
                             {{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}},
                             {{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}}
                              };
Example:
#include<stdio.h>
void show_array(int a[3][3]);
int main()
{
    int a[3][3], i, j;
    printf("Enter 9 numbers for the array: \n");
    for (i = 0; i < 3; ++i)
    {
        for (j = 0; j < 3; ++j)
        {    
            scanf("%d", &a[i][j]);
        }
    }
       show_array(a);
return 0;
}
void show_array(int a[3][3])
{
    int i, j;
    printf("The complete array is: \n");
    for (i = 0; i < 3; ++i)
    {
        printf("\n");           // getting cursor to new line
        for (j = 0; j < 3; ++j)
        {       
           printf("%d\t", a[i][j]);  

        }
}
}

Example 3: Three-dimensional array

#include <stdio.h>
int main()
{
  int a[2][3][2];
  printf("Enter 12 values: \n");
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
      for (int k = 0; k < 2; ++k)
      {
        scanf("%d", &a[i][j][k]);
      }
    }
  }
printf("\n Displaying values:\n"); 
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
      for (int k = 0; k < 2; ++k)
      {
        printf("a[%d][%d][%d] = %d\n", i, j, k, a[i][j][k]);
      }
    }
  }
  return 0;
}

No comments:

Post a Comment

Program for Largest Number among three numbers

Program for Largest Number among three numbers /* c program to find largest number among 3 numbers*/ #include <stdio.h>   in...