You are on page 1of 10

20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

Ex. No: 5(A) WRITE A PROGRAM TO FIND SUM OF ELEMENTS IN


ARRAYS, TO INSERT AN ELEMENT IN AN ARRAY, TO
DATE:
19/02/2021 DELETE AN ELEMENT IN AN ARRAY AND TO REVERSE
THE ELEMENTS IN THE ARRAY.

AIM:
To write a program to find sum of elements in arrays, to insert an element in an array, to delete an
element in an array and to reverse the elements in the single dimension array.

ALGORITHM:
• Start the program.
• Declare the variable as array.
• Initialise the value of the sum = 0.
• Get the array elements.
• Find the sum of the array using the looping structure. Sum = sum+a[i]
• Print the sum of the elements.
• Get the position of term which is to inserted.
• Make an additional cell and place the term as a[i+1]=a[i] until the iteration of i=n. And at the
term a[n] get the value of the element to be inserted and placed in a[n].
• Print the array.
• Get the position of term to be deleted.
• By placing the term a[i-1]=a[i] until the iteration of the i=n.
• Print the array.
• Declare a new array.
• By looping statement reverse the array as follows.(initialise i=0; j=n)
● a[i] = b[j]
● ++i● --j
• Print the reversed array.

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

PROGRAM:

#include<stdi
o.h> int
main()

int a[50], b[50], x[50], c=0,n,i,j,k;

printf("Enter the number of elements in the array: "); scanf("%d",&n);

printf("Enter the %d elements.\n",n); for(i=0;i<n;++i)

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

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

x[i]=a[i];

printf("The original array is: "); for(i=0;i<n;++i)

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

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

c=c+a[i];

printf("\n\na) The sum of numbers in arrays is %d\n",c);

printf("\nEnter the position a[n] in which the new number to be inserted: n= "); scanf("%d",&j);

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

printf("\nEnter the number to be inserted: "); scanf("%d",&k);

for(i=n;i>=j;--i)

a[i+1]=a[i];

}
a
[j
]
=
k
;

printf("\nb) The new array after inserting a number:\n"); for(i=0;i<n+1;++i)

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

}
printf("
\n");

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

a[i]=x[i];

printf("\nEnter the position a[n] in which the number to be deleted: n= "); scanf("%d",&j);

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

a[i]=a[i+1];

printf("\n\nc) The new array after deleting a number:\n"); for(i=0;i<n-1;++i)

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

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

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

b[n-i-1]=x[i];

printf("\n\nd) The reversed array is:\n"); for(i=0;i<n;++i)

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

printf("\n");

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

OUTPUT:

RESULT:
Thus the given operations are performed in a single dimensional array.

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

Ex. No:5(B)
DATE: PROGRAM TO MULTIPLY TWO MATRICES.
19/02/2021

AIM:
To write a program to multiply two matrices using c program.

ALGORITHM:
• Start the Program.

• Enter the row and column of the first (a) matrix.


• Enter the row and column of the second (b) matrix.

• Enter the elements of the first (a) matrix.

• Enter the elements of the second (b) matrix.

• Print the elements of the first (a) matrix in matrix form.

• Print the elements of the second (b) matrix in matrix form.

• Set a loop up to row.

• Set an inner loop up to the column.

• Set another inner loop up to the column.


• Multiply the first (a) and second (b) matrix and store the element in the third matrix (c). Print
the final matrix.
• Stop the Program.

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

FLOWCHART:

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

PROGRAM:
#include <stdio.h>

i
n
t

m
a
i
n
(
)

{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first
matrix\n"); scanf("%d%d", &m, &n);
printf("Enter the elements of first
matrix\n"); for (c=0;c<m;c++)
for (d=0;d<n;d++) scanf("%d",
&first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each
other.\n"); else
{
printf("Enter the elements of second
matrix\n"); for ( c=0;c<p;c++ )
for (d=0;d<q;d++) scanf("%d",
&second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}

r
e
t
u
r
n

0
;

Ex. No. | < Ex. Title>


20CS111 – PROBLEM SOLVING USING C PROGRAMMING LAB 20EUEC107

OUTPUT:

RESULT:
Thus the program to multiply two matrices are coded the two matrix A and B are multiplied
and the result matrices is printed.

Ex. No. | < Ex. Title>

You might also like