Professional Documents
Culture Documents
Practical No. 5
Aim: Write a c program to demonstrate arrays.
a. Write a c program to multiply two matrices of equal size and display
the result.
b. Write a c program to store integer numbers in a 3D array and display
the numbers onto the terminal.
c. Write a c program to check whether a given number is present in an
array, display the index value if a number is found otherwise display
“number does not exist in the array”. (use linear search).
• RAM : 1GB
Software Requirement
THEORY:
An array is a collection of items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. This makes it easier to calculate the position of each
element by simply adding an offset to a base value, i.e., the memory location of the first element
of the array (generally denoted by the name of the array). The base value is index 0 and the
difference between the two indexes is the offset.
Types of array:
Searching:
Searching Algorithms are designed to check for an element or retrieve an element from any data
structure where it is stored. Based on the type of search operation, these algorithms are generally
classified into two categories:
a. Sequential Search: In this, the list or array is traversed sequentially and every element is
checked. For example: Linear Search.
b. Interval Search: These algorithms are specifically designed for searching in sorted data
structures. These type of searching algorithms are much more efficient than Linear Search
as they repeatedly target the center of the search structure and divide the search space in
half. For Example: Binary Search.
a. Write a c program to multiply two matrices of equal size and display the result.
ALGORITHM:
START
Step 1: Declare matrix A[m][n]
and matrix B[p][q]
and matrix C[m][q]
Step 2: Read m, n, p, q.
Step 3: Now check if the matrix can be multiplied or not, if n is not equal to q matrix can't be
multiplied and an error message is generated.
Step 4: Read A[][] and B[][]
Step 5: Declare variable i=0, k=0, j=0 and sum=0
Step 6: Repeat Step until i < m
Repeat Step until j < q
Repeat Step until k < p
Set sum= sum + A[i][k] * B[k][j]
Set multiply[i][j] = sum;
Set sum = 0 and k=k+1
Set j=j+1
Set i=i+1
Step 7: C is the required matrix.
STOP
FLOWCHART:
CODE: #include<stdio.h>
int main()
{
int a[10][10],b[10][10],i,j,k,r,c;
int sum=0,mul[10][10];
printf("Enter the rows and columns\n");
scanf("%d%d",&r,&c);
}
}
printf("Enter the 2nd matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
for(k=0;k<c;k++)
{
sum=sum+a[i][k]*b[k][j];
}
mul[i][j]=sum;
sum=0;
}
}
printf("Result=\n");
for(i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
b. Write a c program to store integer numbers in a 3D array and display the numbers
onto the terminal.
ALGORITHM:
START
Step 1: Declare a 3D matrix.
Step 2: Read elements in the matrix until matrix is
full Step 3: Print the elements of the matrix
STOP
FLOWCHART:
CODE: #include<stdio.h>
int main()
{
int a[10][10][10],i,j,k,n1,n2,n3;
printf("Enter the index value\n");
scanf("%d%d%d",&n1,&n2,&n3);
printf("Enter the elements\n");
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
for(k=0;k<n3;k++)
{
scanf("%d",&a[i][j][k]);
}
}
}
printf("Array:\n");
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
for(k=0;k<n1;k++)
{
printf("a[%d][%d][%d]=%d\n",i,j,k,a[i][j][k]);
}
}
}
}
ALGORITHM:
START
Step-1: Declare the Array A[n] & key variable
Step-2: Enter elements in array
Step-3: Enter elements to be searched i.e. key.
Step-4: Set i to 1
Step-5: If i > n, jump to step 9.
Step-6: If X[i] == key, jump to step 8.
Step-7: increment i by 1 i.e. i = i+1, Go back to step 2
Step-8: Display the element key which is found at particular index i.
Step-9: Display element not found in the set of input elements.
STOP
FLOWCHART:
CODE: #include<stdio.h>
int main()
{
int a[50],i,n,s,flag=0;
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be searched\n");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(s=a[i])
{
printf("\n Number %d is found in index value %d",s,i);
flag++;
}
}
if (flag==0)
{
printf("\n Number does not exist in the array\n");
}
return 0;
}
REFERENCE:
● The C programming language: Brian Kerninghan and Dennis Ritchie, PHI-EEE (or
Pearson)
● http://nptel.ac.in/courses/106104128/16
● http://nptel.ac.in/courses/106104128/17
● http://www.cplusplus.com/doc/
● http://www.exforsys.com/tutorials/c-language/c-arrays.html