Lab 5: Array in C – C Programming Lab Report

Lab 5: Array in C

A C Programming Lab Report for 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 range of number: 5
Enter numbers: 4 8 3 34 4
Sum = 53
Avg = 10.60

Algorithm:

  1. Start
  2. Declare variables `i`, `n`, `sum=0`, array `a`, and float `avg`.
  3. Read the number of elements `n`.
  4. Loop from `i=0` to `n-1` to read numbers into array `a`.
  5. Loop from `i=0` to `n-1` to calculate the sum of elements.
  6. Calculate `avg = (float)sum / n`.
  7. Print `sum` and `avg`.
  8. 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 range of numbers: 5
Enter numbers: 4 8 3 9 2
The smallest number is: 2

Algorithm:

  1. Start
  2. Declare variables `i`, `s`, `n`, and array `a`.
  3. Read the number of elements `n`.
  4. Read `n` numbers into the array `a`.
  5. Initialize `s = a[0]`.
  6. Loop from `i=1` to `n-1`.
  7. Inside the loop, if `a[i] < s`, update `s = a[i]`.
  8. After the loop, print `s`.
  9. 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 range of Number: 5
Enter Numbers: 1 5 8 4 9
Sorted Numbers: 9 8 5 4 1

Algorithm:

  1. Start
  2. Declare variables `i`, `j`, `n`, `temp`, and array `a`.
  3. Read `n`.
  4. Read `n` numbers into array `a`.
  5. Use nested loops for sorting: outer loop `i` from 0 to `n-1`, inner loop `j` from `i+1` to `n-1`.
  6. Inside inner loop, if `a[i] < a[j]`, swap them using `temp`.
  7. After sorting, loop from `i=0` to `n-1` to print the elements of `a`.
  8. 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:

Enter 3x3 Matrix Elements: 1 2 5 7 10 15 25 3 8
The transpose of the matrix is: 1 7 25 2 10 3 5 15 8

Algorithm:

  1. Start
  2. Declare 3x3 matrices `m` and `t`.
  3. Read 9 elements into matrix `m` using nested loops.
  4. Use nested loops to compute transpose: `t[i][j] = m[j][i]`.
  5. Use nested loops to print the transposed matrix `t`.
  6. 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:

Enter Elements of matrix: 1 2 5 7 10 15 25 3 8
Sum of diagonal Elements is: 19

Algorithm:

  1. Start
  2. Declare 3x3 matrix `m` and `sum=0`.
  3. Read 9 elements into `m`.
  4. Use nested loops to iterate through the matrix.
  5. Inside the loops, if `i == j`, add `m[i][j]` to `sum`.
  6. After the loops, print `sum`.
  7. 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:

Enter Element of matrix: 1 2 5 7 10 15 25 3 8
The greatest element is: 25

Algorithm:

  1. Start
  2. Declare 3x3 matrix `m` and integer `g`.
  3. Read 9 elements into `m`.
  4. Initialize `g = m[0][0]`.
  5. Use nested loops to iterate through the matrix.
  6. Inside the loops, if `m[i][j] > g`, update `g = m[i][j]`.
  7. After the loops, print `g`.
  8. 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.

Scroll to Top