Lab 5: Array in C

Lab Report Information
Lab No.: 5
Title: Array in C
Course: Computer Programming (CT 101), I Year I Part
Credit: Important Notes
C Programming Lab Report: Theory for Lab 5
Arrays in C
An array is a data structure that stores a fixed-size, sequential collection of elements of the same data type. This C Programming Lab Report covers both single and multi-dimensional arrays.
- Element: An individual item in an array.
- Index: A number used to access an element. C arrays are 0-indexed.
- Single-dimensional Array: A linear list of elements. Declared as
type name[size];
. - Multi-dimensional Array: An array of arrays, often used for matrices. Declared as
type name[rows][cols];
.
C Programming Lab Report: Programs for Lab 5
1. Calculate Sum and Average
Question:
Write a program to calculate the sum and average of ‘n’ integer numbers using an array.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i, n, sum = 0; float avg; int a[50]; clrscr(); printf("Enter range of number:\n"); scanf("%d", &n); printf("\nEnter numbers:\n"); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < n; i++) { sum = sum + a[i]; } avg = (float)sum / n; printf("\nSum = %d", sum); printf("\nAvg = %.2f", avg); getch(); }
Output:
Enter numbers: 4 8 3 34 4
Sum = 53
Avg = 10.60
Algorithm:
- Start
- Declare variables `i`, `n`, `sum=0`, array `a`, and float `avg`.
- Read the number of elements `n`.
- Loop from `i=0` to `n-1` to read numbers into array `a`.
- Loop from `i=0` to `n-1` to calculate the sum of elements.
- Calculate `avg = (float)sum / n`.
- Print `sum` and `avg`.
- Stop.
2. Find Smallest Number
Question:
Write a program to display the smallest number among 'n' numbers.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i, s, n; int a[50]; clrscr(); printf("Enter range of numbers:\n"); scanf("%d", &n); printf("Enter numbers:\n"); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } s = a[0]; // Assume first element is smallest for (i = 1; i < n; i++) { if (a[i] < s) { s = a[i]; } } printf("\nThe smallest number is: %d", s); getch(); }
Output:
Enter numbers: 4 8 3 9 2
The smallest number is: 2
Algorithm:
- Start
- Declare variables `i`, `s`, `n`, and array `a`.
- Read the number of elements `n`.
- Read `n` numbers into the array `a`.
- Initialize `s = a[0]`.
- Loop from `i=1` to `n-1`.
- Inside the loop, if `a[i] < s`, update `s = a[i]`.
- After the loop, print `s`.
- Stop.
3. Sort Numbers in Descending Order
Question:
Write a program to display 'n' numbers in descending order.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i, j, n, temp; int a[50]; clrscr(); printf("\nEnter range of Number: "); scanf("%d", &n); printf("\nEnter Numbers: "); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } // Bubble Sort for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (a[i] < a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } printf("\nSorted Numbers: "); for (i = 0; i < n; i++) { printf("%d ", a[i]); } getch(); }
Output:
Enter Numbers: 1 5 8 4 9
Sorted Numbers: 9 8 5 4 1
Algorithm:
- Start
- Declare variables `i`, `j`, `n`, `temp`, and array `a`.
- Read `n`.
- Read `n` numbers into array `a`.
- Use nested loops for sorting: outer loop `i` from 0 to `n-1`, inner loop `j` from `i+1` to `n-1`.
- Inside inner loop, if `a[i] < a[j]`, swap them using `temp`.
- After sorting, loop from `i=0` to `n-1` to print the elements of `a`.
- Stop.
4. Transpose of a 3x3 Matrix
Question:
Write a program to find the transpose of a 3x3 matrix.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i, j, m[3][3], t[3][3]; clrscr(); printf("\nEnter 3x3 Matrix Elements:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &m[i][j]); } } for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { t[i][j] = m[j][i]; } } printf("\nThe transpose of the matrix is:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%d\t", t[i][j]); } printf("\n"); } getch(); }
Output:
The transpose of the matrix is: 1 7 25 2 10 3 5 15 8
Algorithm:
- Start
- Declare 3x3 matrices `m` and `t`.
- Read 9 elements into matrix `m` using nested loops.
- Use nested loops to compute transpose: `t[i][j] = m[j][i]`.
- Use nested loops to print the transposed matrix `t`.
- Stop.
5. Sum of Diagonal Elements of a 3x3 Matrix
Question:
Write a program to find the sum of the diagonal elements of a 3x3 matrix.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i, j, m[3][3], sum = 0; clrscr(); printf("\nEnter Elements of matrix:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &m[i][j]); } } for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (i == j) { sum = sum + m[i][j]; } } } printf("Sum of diagonal Elements is: %d", sum); getch(); }
Output:
Sum of diagonal Elements is: 19
Algorithm:
- Start
- Declare 3x3 matrix `m` and `sum=0`.
- Read 9 elements into `m`.
- Use nested loops to iterate through the matrix.
- Inside the loops, if `i == j`, add `m[i][j]` to `sum`.
- After the loops, print `sum`.
- Stop.
6. Find Greatest Number in a 3x3 Matrix
Question:
Write a program to find the greatest number in a 3x3 matrix.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i, j, m[3][3], g; clrscr(); printf("\nEnter Element of matrix:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &m[i][j]); } } g = m[0][0]; // Assume first element is greatest for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (m[i][j] > g) { g = m[i][j]; } } } printf("The greatest element is: %d", g); getch(); }
Output:
The greatest element is: 25
Algorithm:
- Start
- Declare 3x3 matrix `m` and integer `g`.
- Read 9 elements into `m`.
- Initialize `g = m[0][0]`.
- Use nested loops to iterate through the matrix.
- Inside the loops, if `m[i][j] > g`, update `g = m[i][j]`.
- After the loops, print `g`.
- Stop.
Discussion and Conclusion
This C Programming Lab Report on Array in C provided hands-on experience with both single-dimensional and multi-dimensional arrays. The exercises demonstrated fundamental operations such as calculating sum and average, finding minimum values, and sorting. The transition to 2D arrays covered common matrix operations like finding the transpose and summing diagonal elements. In conclusion, this lab successfully covered the core concepts of array declaration, data input, and processing, illustrating how arrays serve as a powerful tool for organizing collections of data in a structured manner.