C PROGRAMING: A Beginner’s Course | CHAPTER 5 Arrays

Immanuel Training Institute proudly presents free software courses

To get certificate Contact: iits.certificate@gmail.com


CHAPTER 1   Introduction and structure of C program

CHAPTER 2   Data types, Constants, Variables and I/O Statements

CHAPTER 3   Operators and Expressions

CHAPTER 4   Control Statements

CHAPTER 5   Arrays

CHAPTER 6   String Manipulations

CHAPTER 7   Functions

CHAPTER 8   Structure, Unions and Enumerated Data types  

CHAPTER 9   Pointers

CHAPTER  5

 ARRAYS:-

      Array is collection of similar data items that are stored under a common name . a value in array is identified by index or subscript enclosed in square brackets with same .Ex:- a[0],a[1],………a[n-1]

Array declaration :- int a[10]

 

TYPES:

·       One dimensional

         The collection of data items can be stored under a one variable name using only one script, such a variable is called the one dimensional array

Ex:-

void main()

{

int a[5],sum =0,I;

clrscr();

printf(“enter the 5 integer number\n”);

for(i=1;i<=5;i++)

{

scanf(“%d”,&a[i]);

sum =sum+a[i];

}

printf(“the sum of the given number is:%d”\n”,sum);

getch();

}

·       Two dimensional

       Two dimensional  array are stored in a row-column matrix, were the left index indicates the row and the right indicate the column.

Ex:-

         int stud[4][2]

         int i;           

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

{

printf(“enter the %d stud roll no and mark”);

scanf(“%d%d”,&stud[i][0],&stud[i][1]);

}

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

printf(“%d%d”,&stud[i][0],&stud[i][1]);

}

·       Multi dimensional

  Multi dimensional arrays stored in number of columns and number of rows.

Ex:-

int num[6]={12,34,56,56,13,23}

float press[]={1.2,34,4.5,45.6,}

EXAMPLE PROGRAMS:

1.     Example program for one dimensional array (addition).

#include<stdio.h>

#include<conio.h>

void main()

{

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

clrscr();

printf("Enter the size of array:");

scanf("%d",&n);

printf("Enter arr1 values one by one:");

    for(i=1;i<=n;i++)

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

printf("Enter arr2 values one by one:");

    for(i=1;i<=n;i++)

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

printf("The addition of arr1 & arr2:\n");

    for(i=1;i<=n;i++)

    {

                c[i]=a[i]+b[i];

                printf("%d+%d=%d\n",a[i],b[i],c[i]);

    }

getch();

}

2.     Example program for two dimensional array(matrix - getting and displaying a matrix).

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10][10],m,n,i,j;

clrscr();

printf("Enter the order the matrix:");

scanf("%d%d",&m,&n);

printf("Enter the values:");

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

    {

                for(j=0;j<n;j++)

                {

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

                }

    }

printf("The given matrix is:\n");

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

    {

                for(j=0;j<n;j++)

                    printf("%d\t",a[i][j]);

    printf("\n");

    }

getch();

}

3.     Example program for two dimensional array(matrix addition).

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10][10],b[10][10],c[10][10],i,j,m,n;

clrscr();

printf("Enter the order of the matrix:");

scanf("%d%d",&m,&n);

printf("Enter the first matrix:");

    for(i=1;i<=m;i++)

    {

                for(j=1;j<=n;j++)

                {

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

                }

    }

printf("Enter the second matrix:");

    for(i=1;i<=m;i++)

    {

                for(j=1;j<=n;j++)

                {

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

                }

    }

printf("\nThe addition of MAT1 + MAT2 is:\n\n");

    for(i=1;i<=m;i++)

    {

                for(j=1;j<=n;j++)

                {

                            c[i][j]=a[i][j]+b[i][j];

                }

    }

    for(i=1;i<=m;i++)

    {

                for(j=1;j<=n;j++)

                {

                            printf("%d\t",c[i][j]);

                }

    printf("\n\n");

    }

getch();

}

4.     Example program for two dimensional array(matrix transportation).

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10][10],m,n,i,j;

clrscr();

printf("Enter the order of the matrix:");

scanf("%d%d",&m,&n);

printf("Enter the matrix:");

    for(i=1;i<=m;i++)

    {

                for(j=1;j<=n;j++)

                {

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

                }

    }

printf("The transportation of matrix is :\n");

    for(j=1;j<=n;j++)

    {

                for(i=1;i<=m;i++)

                {

                            printf("%d\t",a[i][j]);

                }

    printf("\n\n");

    }

getch();

}

5.     Example program for two dimensional array(matrix multiplication).

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10][10],b[10][10],c[10][10],o,p,m,n,i,j,k;

clrscr();

printf("Enter the order of the first matrix:");

scanf("%d%d",&m,&n);

printf("Enter the first matrix:");

    for(i=1;i<=m;i++)

    {

                for(j=1;j<=n;j++)

                {

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

 

                }

    }

printf("Enter the order of the second matrix:");

scanf("%d%d",&o,&p);

printf("Enter the second matrix:");

    for(i=1;i<=o;i++)

    {

                for(j=1;j<=p;j++)

                {

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

                }

    }

    if(n!=o)

    {

                printf("Matrix cannot be multiplied\n");

                goto l1;

    }

    else

    {

                for(i=1;i<=m;i++)

                {

                            for(j=1;j<=p;j++)

                            {

                                        c[i][j]=0;

                            }

                }

                for(i=1;i<=m;i++)

                {

                            for(j=1;j<=p;j++)

                            {

                                        for(k=1;k<=n;k++)

                                        {

                                                    c[i][j]=c[i][j]+(a[i][k]*b[k][j]);

                                        }

                            }

                }

    }

printf("The resultant matrix is:\n");

    for(i=1;i<=m;i++)

    {

                for(j=1;j<=p;j++)

                {

                            printf("%d\t",c[i][j]);

                }

    printf("\n");

    }

l1:getch();

}

6.     Write a program to convert decimal number into binary number.

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],n,i,j,c;

clrscr();

printf("Enter the no. do u want to convert:");

scanf("%d",&n);

i=1;

    while(n!=0)

    {

                j=n%2;

                a[i]=j;

                n/=2;

                i++;

    }

c=i-1;

printf("The binary value for given decimal is ");

    for(i=c;i>0;i--)

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

getch();

}

7.     Write a program to arrange the given numbers in ascending order.

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],i,j,n,t;

clrscr();

printf("Enter the size of array:");

scanf("%d",&n);

printf("Enter the values one by one:");

    for(i=1;i<=n;i++)

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

    for(i=1;i<=n;i++)

    {

                for(j=i;j<=n;j++)

                {

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

                            {

                            t=a[i];

                            a[i]=a[j];

                            a[j]=t;

                            }

                }

    }

printf("The Ascending order is:\n");

    for(i=1;i<=n;i++)

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

getch();

}




TN 10TH SAMACHEER MATHS EXERCISE 2.5 SOLUTIONS

Contact Form

Name

Email *

Message *