lab 8
lab 8
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
scanf("%d", &n);
scanf("%d", &a[i]);
scanf("%d", &x);
if(a[i] == x) {
found = 1;
break;
}
if(!found) {
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.
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
int main() {
scanf("%d", &n);
scanf("%d", &a[i]);
scanf("%d", &x);
low = 0;
high = n - 1;
if(a[mid] == x) {
found = 1;
break;
high = mid - 1;
} else {
low = mid + 1;
if(!found) {
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.
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
float avg;
scanf("%d", &n);
scanf("%d", &a[i]);
sum += a[i];
avg = (float)sum / n;
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:
int main() {
scanf("%d", &n);
scanf("%d", &a[i]);
temp = a[0];
a[n - 1] = temp;
return 0;
Discussion:
Rotation helps in understanding array manipulation and indexing.
Learning Outcomes:
Learned array shifting and use of temporary variables.
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:
int main() {
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
printf("Sum of matrices:\n");
printf("\n");
return 0;
}
Discussion:
Matrix addition is straightforward and teaches multi-dimensional arrays.
Learning Outcomes:
Learned how to perform operations on 2D arrays.
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:
int main() {
if(c1 != r2) {
return 0;
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
c[i][j] = 0;
}
}
printf("Resultant matrix:\n");
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.