You are on page 1of 13

S. B.

JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.

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).

Name of Student: Sakshi Pahade


Roll No.: 228
Semester/Year: Ist Year
Academic Session: 2021-2022
Date of Performance:
21/01/2022
Date of Submission : 24/01/2022
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).

OBJECTIVE/EXPECTED LEARNING OUTCOME:

• To be able to understand the concept of arrays.

• To be able to distinguish between different dimensions of array and their memory


arrangements.

• To be able to identify and choose n-dimension arrays depending upon problem


requirement.

HARDWARE AND SOFTWARE REQUIREMENT:


Hardware Requirement

• Processor : Dual Core

• RAM : 1GB

• Hard Disk Drive : > 80 GB

Software Requirement

• Operating System – Windows 2007

• Package used – g++

• TURBO C++/DEV C++

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:

a. Single Dimensional Array / One Dimensional Array


b. Multi-Dimensional Array

Single Dimensional Array / One Dimensional Array:


In the C programming language, single dimensional arrays are used to store lists of values of the
same datatype. In other words, single dimensional arrays are used to store a row of values. In a
single dimensional array, data is stored in linear form. Single dimensional arrays are also called
as one-dimensional arrays, Linear Arrays or simply 1-D Arrays.
Multi-Dimensional Array:
An array of arrays is called a multidimensional array. In simple words, an array created with
more than one dimension (size) is called a multidimensional array. Multi-dimensional array can
be of two-dimensional array or three-dimensional array or four-dimensional array or more.

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 1st matrix\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);

}
}
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;
}

INPUT & OUTPUT (With Different Test Cases):


Sr. No. INPUT OUTPUT

1. Enter the no of rows and columns Result


2 2 Enter the 1st matrix 1 0 3 4 2
Enter the 2nd matrix 2 0 3 4 0
18
16

2. Enter the no of rows and columns Result


1 1 Enter the 1st matrix 4 20
Enter the 2nd matrix 5

3. Enter the no of rows and columns Result


2 2 Enter the 1st matrix 4 7 8 8 91
Enter the 2nd matrix 7 7 9 0 28
128
56

4. Enter the no of rows and columns4 Result


1 Enter the 1st matrix 5 6 7 7 40
Enter the 2nd matrix 8 0 9 8 48
56
56

5. Enter the no of rows and columns Result


3 1 Enter the 1st matrix 4 5 7 24
Enter the 2nd matrix 6 7 8 30
42

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]);
}
}
}
}

INPUT & OUTPUT (With Different Test Cases):


Sr. No. INPUT OUTPUT

1. Enter the index value 1 2 3 Array


Enter the elements 2 4 5 7 7 8 a[0][0][0]=2
a[0][1][0]=7

2. Enter the index value 3 2 1 Array


Enter the elements 2 3 4 5 7 9 a[0][0][0]=2 a[0][1][1]=32761
a[0][0][1]=0 a[0][1][2]=-4606
a[0][0][2]=113 a[1][0][0]=4 a[0]
[1][0]=3 a[1][0][1]=0

3. Enter the index value 1 1 1 Array


Enter the elements 3 a[0][0][0]=3

4. Enter the index value 1 2 3 Array

Enter the elements 3 4 5 5 7 8 a[0][0][0]=3


a[0][1][0]=5
5. Enter the index value 0 0 0 Array
Enter the elements A[0][0][0]=0

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”.

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;
}

INPUT & OUTPUT (With Different Test Cases):


Sr. No. INPUT OUTPUT

1. Enter the no of elements 4 Number 6 is found in the index


Enter the array elements 5 6 value1
9 7 Enter the no to be
searched 6

2. Enter the no of elements 2 Number 2 is found in the index


Enter the array elements 1 2 value1
Enter the no to be searched 2

3. Enter the no of elements 5 Number 5 does not exist in the array


Enter the array elements 1 2 3 4
5 Enter the no to be searched 0

4. Enter the no of elements 0 Number 5 does not exist in the array


Enter the array elements 0
Enter the no to be searched 5

5. Enter the no of elements 4 Number 5 does not exist in the array


Enter the array elements 0 1 2
3 Enter the no to be searched
5
CONCLUSION: Hence we have written a program to demonstrate

Array. DISCUSSION QUESTIONS:

Q.1) What do you mean by an Array?


Q.2) Advantages and disadvantages of Array?
Q.3) Can we change the size of an array at run time?
Q.4) Can you declare an array without assigning the size of an array?
Q.5) Can we declare array size as a negative number?

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

You might also like