5/10/2016

Arrays in C

Single Dimension Arrays

Syntax :  type  var_name[ size ] ;


For Example :- 

  int array[ 5 ] ;

Here 5 indicates size of the array.Thus total number of elements in the array is 5

which are represented as follows:

array[0]

array[1]

array[2]

array[3]

array[4]

1.Now understand the following program:-

#include<stdio.h>

#include<conio.h>

int main()

{

/*declaration of an array*/

int array[4];

    array[0]=6; /*first element*/

    array[1]=5; /*2nd element*/

    array[2]=8; /*3rd element*/

    array[3]=1; /*4th element*/

    clrscr();

   printf("The 1st element of array = %d\n",array[0]); 

   printf("The 2nd element of array = %d\n",array[1]);

   printf("The 3rd element of array = %d\n",array[2]);

   printf("The 4th element of array = %d",array[3]);

    getch();

}

Output:

The 1st element of array =6

The 2nd element of array =5 

The 3rd element of array =8 

The 4th element of array =1

We can also write this program using for loop.

2.A C program to accept N numbers and print them in an ascending order.

 

 #include<stdio.h>

#include<conio.h>

void main() 

{

  int i, j, a, N, number[30]; 

/*30 is the size of the array*/

 

  printf("Enter the value of N: \n");

  scanf("%d", &N);


  printf("Enter the numbers: \n");

   for (i = 0; i < N; ++i) 

{

   scanf("%d", &number[i]);

 }

 

  for (i = 0; i < N; ++i) 

{

  for (j = i + 1; j < N; ++j)

 {

     if (number[i] > number[j]) 

       {

        a = number[i];

        number[i] = number[j];

        number[j] = a;

        }

 

    }

 

  }

 

  printf("The numbers in ascending order: \n");

 

  for (i = 0; i < N; ++i)

{

     printf("%d\n", number[i]);

 }

 getch();

}

Output:

Enter the value of N:

3

Enter the numbers:

9 6 7

The numbers in ascending order:

6

7

9


3.A C program to reverse  the digits of number using array.

#include<stdio.h> 
#include<conio.h>
void main()
{
int num;
int n,i;
int temp[100];
printf("Enter no. of digits in your number:");
scanf("%d",&n);

printf("Enter number:");
scanf("%d",&num);
for(i=0;i<n;i++)
{
temp[i]=num%10;
num=num/10;
}
/*storing elements in array*/
printf("Required reverse number:");
for(i=0;i<n;i++)
{
printf("%d",temp[i]);
}
/*print the elements of array*/
getch();
}

Output:
Enter no. of digits in your number:4
Enter number:4563
Required reverse number:3654

Multidimensional Arrays

Syntax : type   name [ rows ] [ columns ]  ;

For Example :- 2D array of dimension 2 X 3.

  int d[ 2 ] [ 3 ] ;

3.Now understand the following program:

#include<stdio.h>

#include<conio.h>

int main()

{

    int array[2][4]={{1,3,3,5,},{3,4,6,7}};

    

    clrscr();

   printf("array[0][1] = %d\n",array[0][1]); 

   printf("array [1][1] = %d\n",array[1][1]);

   printf("array[0][0] = %d\n",array[0][0]);

   printf("array[1][0] = %d",array[1][0]);

    getch();

}

Output:

array[0][1] = 3

array[1][1] = 4

array[0][0] = 1

array[1][0] = 3

4.Following is the C program of the summation of two 3×3 matrix.

#include<stdio.h>

#include<conio.h>

int main()

{

    int a[10][10],b[10][10],c[10][10];

    printf("Enter first 3×3 matrix:\n");

    for(int i=1;i<=3;i++)

        {

        for(int j=1;j<=3;j++)

        {

        scanf("%d",&a[i][j]);

            }

    }

    

    

       printf("Enter second 3×3 matrix:\n");

    for(int i=1;i<=3;i++)

        {

        for(int j=1;j<=3;j++)

        {

        scanf("%d",&b[i][j]);

            }

    }

   printf("Required matrix after summation:\n");

    for(int i=1;i<=3;i++)

    {

        for(int j=1;j<=3;j++)

        {

 printf("%d  ",a[i][j]+b[i][j]);

   }

        printf("\n");

        }

    getch();

}

Output:
Enter first 3×3 matrix:
1 2 3
4 3 5
3 2 1
Enter second 3×3 matrix:
2 2 2 
3 3 3
4 1 1
Required matrix after summation:
3  4  5
7  6  8
7  3  2


More programs of array is available in for loop.

Please post comment.Also you can post programming related problems.




No comments:

Post a Comment