0% found this document useful (0 votes)
3 views11 pages

lab 8

The document outlines six programming tasks in C, including linear search, binary search, average calculation, left rotation of arrays, addition and multiplication of matrices. Each task includes an aim, explanation, algorithm, code, discussion, and learning outcomes. The focus is on understanding array manipulation, search algorithms, and matrix operations.

Uploaded by

devrishinain063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

lab 8

The document outlines six programming tasks in C, including linear search, binary search, average calculation, left rotation of arrays, addition and multiplication of matrices. Each task includes an aim, explanation, algorithm, code, discussion, and learning outcomes. The focus is on understanding array manipulation, search algorithms, and matrix operations.

Uploaded by

devrishinain063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

LAB 8

1. Linear Search in an Array

Aim:
To write a C program that searches an element in an array using linear search.

Explanation:
In linear search, each element of the array is checked one by one until the target element is found or
the array ends. It is a simple search method.

Algorithm:
1. Input array size and elements
2. Input element to be searched
3. Traverse array from start to end
4. Compare each element with target
5. If found, display position
6. If not, show element not found

Code :#include <stdio.h>


int main() {

int a[100], n, i, x, found = 0;

printf("Enter size of array: ");

scanf("%d", &n);

printf("Enter %d elements: ", n);

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

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

printf("Enter element to search: ");

scanf("%d", &x);

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

if(a[i] == x) {

printf("Element found at position %d\n", i+1);

found = 1;

break;
}

if(!found) {

printf("Element not found\n");

return 0;

Discussion:
Linear search is easy but not efficient for large arrays. It checks each element, so the time taken
increases with size.

Learning Outcomes:
Learned how to use loops and conditionals to perform basic search in arrays.

2. Binary Search in an Array

Aim:
To write a C program that searches for an element using binary search.

Explanation:
Binary search only works on sorted arrays. It repeatedly divides the array in half to find the target
element.

Algorithm:
1. Input sorted array and target
2. Set low = 0, high = n - 1
3. While low ≤ high:
o Find mid
o If a[mid] == target, found
o Else if target < a[mid], search left
o Else search right
4. If loop ends, element not found

Code : #include <stdio.h>

int main() {

int a[100], n, x, low, high, mid, found = 0, i;

printf("Enter size of array: ");

scanf("%d", &n);

printf("Enter %d sorted elements: ", n);

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

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

printf("Enter element to search: ");

scanf("%d", &x);

low = 0;

high = n - 1;

while(low <= high) {

mid = (low + high) / 2;

if(a[mid] == x) {

printf("Element found at position %d\n", mid + 1);

found = 1;

break;

} else if(x < a[mid]) {

high = mid - 1;

} else {
low = mid + 1;

if(!found) {

printf("Element not found\n");

return 0;

Discussion:
Binary search is faster than linear search for large sorted arrays. It reduces comparisons by half each
time.

Learning Outcomes:
Understood how to use divide-and-conquer for efficient searching.

3. Average of n Numbers Using Arrays

Aim:
To write a C program that calculates the average of n numbers using arrays.

Explanation:
The program takes n inputs, stores them in an array, adds all elements, then divides the sum by n to
get the average.

Algorithm:
1. Input number of elements
2. Store values in array
3. Add all values
4. Calculate average = sum / n
5. Display average

Code : #include <stdio.h>


int main() {

int a[100], n, i, sum = 0;

float avg;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter %d elements: ", n);

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

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

sum += a[i];

avg = (float)sum / n;

printf("Average = %.2f\n", avg);

return 0;

Discussion:
Using arrays helps in handling large number of values easily in a loop.

Learning Outcomes:
Learned how to store data in arrays and use loops for calculations.
4. Left Rotate Array Elements by One

Aim:
To write a C program that rotates array elements to the left by one position.

Explanation:
In left rotation, the first element moves to the last position, and others shift left by one.

Algorithm:

1. Input array size and elements


2. Store first element in temp
3. Shift other elements to left
4. Assign temp to last position
5. Display updated array

Code #include <stdio.h>

int main() {

int a[100], n, i, temp;

printf("Enter size of array: ");

scanf("%d", &n);

printf("Enter %d elements: ", n);

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

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

temp = a[0];

for(i = 0; i < n - 1; i++) {

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

a[n - 1] = temp;

printf("Array after left rotation: ");

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


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

return 0;

Discussion:
Rotation helps in understanding array manipulation and indexing.

Learning Outcomes:
Learned array shifting and use of temporary variables.

5. Addition of Two 3×3 Matrices

Aim:
To write a C program to add two 3×3 matrices.

Explanation:
Each element of matrix A is added to the same position in matrix B to form the result matrix.

Algorithm:

1. Input two 3×3 matrices


2. Add each corresponding element
3. Store result in new matrix
4. Display result matrix

Code #include <stdio.h>

int main() {

int a[3][3], b[3][3], c[3][3], i, j;

printf("Enter elements of first matrix:\n");


for(i = 0; i < 3; i++) {

for(j = 0; j < 3; j++) {

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

printf("Enter elements of second matrix:\n");

for(i = 0; i < 3; i++) {

for(j = 0; j < 3; j++) {

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

for(i = 0; i < 3; i++) {

for(j = 0; j < 3; j++) {

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

printf("Sum of matrices:\n");

for(i = 0; i < 3; i++) {

for(j = 0; j < 3; j++) {

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

printf("\n");

return 0;

}
Discussion:
Matrix addition is straightforward and teaches multi-dimensional arrays.

Learning Outcomes:
Learned how to perform operations on 2D arrays.

6. Multiplication of Two Matrices

Aim:
To write a C program that multiplies two matrices.

Explanation:
In matrix multiplication, the element at row i and column j in result matrix is sum of products
of elements from row i of first matrix and column j of second matrix.

Algorithm:

1. Input size and elements of matrices


2. Multiply using triple loop
3. Store result in new matrix
4. Display result matrix

Code #include <stdio.h>

int main() {

int a[10][10], b[10][10], c[10][10], r1, c1, r2, c2, i, j, k;

printf("Enter rows and columns of first matrix: ");

scanf("%d%d", &r1, &c1);


printf("Enter rows and columns of second matrix: ");

scanf("%d%d", &r2, &c2);

if(c1 != r2) {

printf("Multiplication not possible\n");

return 0;

printf("Enter elements of first matrix:\n");

for(i = 0; i < r1; i++) {

for(j = 0; j < c1; j++) {

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

printf("Enter elements of second matrix:\n");

for(i = 0; i < r2; i++) {

for(j = 0; j < c2; j++) {

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

for(i = 0; i < r1; i++) {

for(j = 0; j < c2; j++) {

c[i][j] = 0;

for(k = 0; k < c1; k++) {

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

}
}

printf("Resultant matrix:\n");

for(i = 0; i < r1; i++) {

for(j = 0; j < c2; j++) {

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

printf("\n");

return 0;

Discussion:
Matrix multiplication shows the use of nested loops and careful indexing.

Learning Outcomes:
Learned matrix multiplication logic and improved understanding of loops and arrays.

You might also like