C Programing-Questions On Arrays : Exercises, Practice, Solution

Arrays are crucial in C programming for storing and manipulating data efficiently. Mastering array initialization, accessing elements, and basic operations like insertion and deletion is essential. Learning search and sorting algorithms for arrays enhances problem-solving skills. Exploring multi-dimensional arrays expands data representation possibilities. Advanced techniques like slicing and copying offer powerful manipulation tools. Understanding practical applications, such as implementing data structures like stacks and queues, provides real-world context. Stay tuned for detailed explanations and examples!

Write a C program to find the sum of all elements in an array.

#include <stdio.h>

int main() {
    int arr[] = {5, 10, 15, 20, 25};
    int sum = 0;
    
    for (int i = 0; i < 5; i++) {
        sum += arr[i];
    }
    
    printf("Sum of array elements: %d\n", sum);
    
    return 0;
}

Write a C program to find the largest element in an array.

#include <stdio.h>

int main() {
    int arr[] = {30, 15, 25, 40, 20};
    int max = arr[0];
    
    for (int i = 1; i < 5; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    
    printf("Largest element in array: %d\n", max);
    
    return 0;
}

Write a C program to find the smallest element in an array.

#include <stdio.h>

int main() {
    int arr[] = {30, 15, 25, 40, 20};
    int min = arr[0];
    
    for (int i = 1; i < 5; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
    }
    
    printf("Smallest element in array: %d\n", min);
    
    return 0;
}

Write a C program to sort an array in ascending order using Bubble Sort.

#include <stdio.h>

int main() {
    int arr[] = {30, 15, 25, 40, 20};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
    
    printf("Original array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    
    // Sort the array using bubble sort
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    
    printf("Sorted array (ascending): ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    
    return 0;
}

Write a C program to find the second largest element in an array.

#include <stdio.h>

int main() {
    int arr[] = {30, 15, 25, 40, 20};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
    
    int max = arr[0];
    int secondMax = arr[0];
    
    for (int i = 1; i < size; i++) {
        if (arr[i] > max) {
            secondMax = max;
            max = arr[i];
        } else if (arr[i] > secondMax && arr[i] != max) {
            secondMax = arr[i];
        }
    }
    
    printf("Second largest element in array: %d\n", secondMax);
    
    return 0;
}

Write a C program to find the duplicate elements in an array.

#include &lt;stdio.h>

int main() {
    int arr[] = {5, 10, 15, 20, 25, 10, 30, 25};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
    
    printf("Duplicate elements in array: ");
    for (int i = 0; i &lt; size - 1; i++) {
        for (int j = i + 1; j &lt; size; j++) {
            if (arr[i] == arr[j]) {
                printf("%d ", arr[i]);
                break;
            }
        }
    }
    printf("\n");
    
    return 0;
}

Write a C program to find the frequency of each element in an array.

#include <stdio.h>

int main() {
    int arr[] = {5, 10, 15, 20, 25, 10, 30, 25};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
    
    printf("Frequency of each element in array:\n");
    for (int i = 0; i < size; i++) {
        int count = 1;
        if (arr[i] == -1) {
            continue;
        }
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                count++;
                arr[j] = -1; // Mark the element as visited
            }
        }
        printf("%d occurs %d times\n", arr[i], count);
   

Write a C program to find the missing number in an array containing numbers from 1 to N.

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 4, 6, 3, 7, 8};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
    
    int n = size + 1;
    int total = (n * (n + 1)) / 2; // Sum of first n natural numbers
    int sum = 0;
    
    for (int i = 0; i < size; i++) {
        sum += arr[i];
    }
    
    int missingNumber = total - sum;
    
    printf("Missing number in array: %d\n", missingNumber);
    
    return 0;
}

Write a C program to remove duplicate elements from an array.

#include <stdio.h>

int main() {
    int arr[] = {5, 10, 15, 20, 25, 10, 30, 25};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
    
    printf("Array with duplicates removed: ");
    
    for (int i = 0; i < size; i++) {
        int isDuplicate = 0;
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                isDuplicate = 1;
                break;
            }
        }
        if (!isDuplicate) {
            printf("%d ", arr[i]);
        }
    }
    
    printf("\n");
    
    return 0;
}

Write a C program to add two matrices entered by the user.

#include <stdio.h>

int main() {
    int mat1[3][3], mat2[3][3], sum[3][3];
    
    printf("Enter elements of first matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &mat1[i][j]);
        }
    }
    
    printf("Enter elements of second matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &mat2[i][j]);
        }
    }
    
    printf("Sum of the matrices:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            sum[i][j] = mat1[i][j] + mat2[i][j];
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

Write a C program to multiply two matrices entered by the user.

#include <stdio.h>

int main() {
    int mat1[3][3], mat2[3][3], result[3][3];
    
    printf("Enter elements of first matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &mat1[i][j]);
        }
    }
    
    printf("Enter elements of second matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &mat2[i][j]);
        }
    }
    
    // Multiplication logic
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            result[i][j] = 0;
            for (int k = 0; k < 3; k++) {
                result[i][j] += mat1[i][k] * mat2[k][j];
            }
        }
    }
    
    printf("Product of the matrices:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

Write a C program to find the transpose of a matrix entered by the user.

#include <stdio.h>

int main() {
    int mat[3][3], transpose[3][3];
    
    printf("Enter elements of the matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &mat[i][j]);
        }
    }
    
    // Transpose logic
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            transpose[j][i] = mat[i][j];
        }
    }
    
    printf("Transpose of the matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

Write a C program to check if a matrix is symmetric or not.

#include <stdio.h>
#include <stdbool.h>

bool isSymmetric(int mat[3][3]) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (mat[i][j] != mat[j][i]) {
                return false;
            }
        }
    }
    return true;
}

int main() {
    int mat[3][3];
    
    printf("Enter elements of the matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &mat[i][j]);
        }
    }
    
    if (isSymmetric(mat)) {
        printf("The matrix is symmetric.\n");
    } else {
        printf("The matrix is not symmetric.\n");
    }
    
    return 0;
}

Write a C program to find the sum of each row and each column of a matrix.

#include <stdio.h>

int main() {
    int mat[3][3], rowSum[3] = {0}, colSum[3] = {0};
    
    printf("Enter elements of the matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &mat[i][j]);
            rowSum[i] += mat[i][j];
            colSum[j] += mat[i][j];
        }
    }
    
    printf("Sum of each row:\n");
    for (int i = 0; i < 3; i++) {
        printf("Row %d: %d\n", i + 1, rowSum[i]);
    }
    
    printf("Sum of each column:\n");
    for (int j = 0; j < 3; j++) {
        printf("Column %d: %d\n", j + 1, colSum[j]);
    }
    
    return 0;
}

Write a C program to find the sum of all elements in the main diagonal of a square matrix entered by the user.

#include <stdio.h>

int main() {
    int mat[3][3];
    int sum = 0;
    
    printf("Enter elements of the square matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &mat[i][j]);
            if (i == j) {
                sum += mat[i][j];
            }
        }
    }
    
    printf("Sum of main diagonal elements: %d\n", sum);
    
    return 0;
}

Write a program in C to merge two arrays of the same size sorted in descending order.

#include <stdio.h>

int main() {
    int arr1[100], arr2[100], arr3[200];
    int size1, size2, size3;
    int i, j, k;

    printf("\nMerge two arrays of the same size sorted in descending order.\n");
    printf("--------------------------------------------------------------\n");

    printf("Input the number of elements to be stored in the first array: ");
    scanf("%d", &size1);

    printf("Input %d elements in the first array:\n", size1);
    for (i = 0; i < size1; i++) {
        printf("Element - %d: ", i);
        scanf("%d", &arr1[i]);
    }

    printf("Input the number of elements to be stored in the second array: ");
    scanf("%d", &size2);

    printf("Input %d elements in the second array:\n", size2);
    for (i = 0; i < size2; i++) {
        printf("Element - %d: ", i);
        scanf("%d", &arr2[i]);
    }

    // Merge the arrays
    size3 = size1 + size2;
    for (i = 0; i < size1; i++) {
        arr3[i] = arr1[i];
    }
    for (j = 0; j < size2; j++) {
        arr3[i] = arr2[j];
        i++;
    }

    // Sort the merged array in descending order
    for (i = 0; i < size3; i++) {
        for (k = 0; k < size3 - 1; k++) {
            if (arr3[k] <= arr3[k + 1]) {
                int temp = arr3[k + 1];
                arr3[k + 1] = arr3[k];
                arr3[k] = temp;
            }
        }
    }

    // Print the merged array in descending order
    printf("\nThe merged array in descending order is:\n");
    for (i = 0; i < size3; i++) {
        printf("%d   ", arr3[i]);
    }
    printf("\n\n");

    return 0;
}

Write a program in C to count the frequency of each element of an array.

#include <stdio.h>

int main() {
    int arr[100];
    int size;

    // Input the size of the array
    printf("Enter the size of the array: ");
    scanf("%d", &size);

    // Input elements of the array
    printf("Enter %d elements in the array:\n", size);
    for (int i = 0; i < size; i++) {
        printf("Element - %d: ", i + 1);
        scanf("%d", &arr[i]);
    }

    // Initialize an array to store frequency of elements
    int freq[size];
    for (int i = 0; i < size; i++) {
        freq[i] = -1;
    }

    // Count frequency of each element
    for (int i = 0; i < size; i++) {
        int count = 1;
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                count++;
                freq[j] = 0; // Mark as visited
            }
        }
        if (freq[i] != 0) {
            freq[i] = count;
        }
    }

    // Print frequency of each element
    printf("\nFrequency of each element in the array:\n");
    for (int i = 0; i < size; i++) {
        if (freq[i] != 0) {
            printf("%d occurs %d times\n", arr[i], freq[i]);
        }
    }

    return 0;
}

Write a program in C to separate odd and even integers into separate arrays.

#include <stdio.h>

int main() {
    int arr[100], evenArr[100], oddArr[100];
    int size, evenCount = 0, oddCount = 0;

    // Input the size of the array
    printf("Enter the size of the array: ");
    scanf("%d", &size);

    // Input elements of the array
    printf("Enter %d elements in the array:\n", size);
    for (int i = 0; i < size; i++) {
        printf("Element - %d: ", i + 1);
        scanf("%d", &arr[i]);

        // Check if the element is even or odd and store in respective arrays
        if (arr[i] % 2 == 0) {
            evenArr[evenCount++] = arr[i];
        } else {
            oddArr[oddCount++] = arr[i];
        }
    }

    // Print even integers array
    printf("\nEven integers array:\n");
    for (int i = 0; i < evenCount; i++) {
        printf("%d ", evenArr[i]);
    }
    printf("\n");

    // Print odd integers array
    printf("\nOdd integers array:\n");
    for (int i = 0; i < oddCount; i++) {
        printf("%d ", oddArr[i]);
    }
    printf("\n");

    return 0;
}

Write a program in C to delete an element at a desired position from an array.

#include <stdio.h>

int main() {
    int arr[100];
    int size, pos;

    // Input the size of the array
    printf("Enter the size of the array: ");
    scanf("%d", &size);

    // Input elements of the array
    printf("Enter %d elements in the array:\n", size);
    for (int i = 0; i < size; i++) {
        printf("Element - %d: ", i + 1);
        scanf("%d", &arr[i]);
    }

    // Input the position of the element to delete
    printf("Enter the position of the element to delete (1 to %d): ", size);
    scanf("%d", &pos);

    // Check if position is valid
    if (pos < 1 || pos > size) {
        printf("Invalid position!\n");
    } else {
        // Delete element by shifting elements to the left
        for (int i = pos - 1; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }

        // Decrement size of the array
        size--;

        // Print the updated array
        printf("Array after deletion:\n");
        for (int i = 0; i < size; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
    }

    return 0;
}

Write a program in C to find the sum of rows and columns of a matrix.

#include <stdio.h>

int main() {
    int matrix[100][100];
    int rows, cols;

    // Input the number of rows and columns of the matrix
    printf("Enter the number of rows of the matrix: ");
    scanf("%d", &rows);
    printf("Enter the number of columns of the matrix: ");
    scanf("%d", &cols);

    // Input elements of the matrix
    printf("Enter elements of the matrix:\n");
    for (int i = 0; i < rows; i++) {
        printf("Enter elements for row %d:\n", i + 1);
        for (int j = 0; j < cols; j++) {
            printf("Element [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    // Calculate sum of rows
    printf("\nSum of rows:\n");
    for (int i = 0; i < rows; i++) {
        int rowSum = 0;
        for (int j = 0; j < cols; j++) {
            rowSum += matrix[i][j];
        }
        printf("Row %d: %d\n", i + 1, rowSum);
    }

    // Calculate sum of columns
    printf("\nSum of columns:\n");
    for (int j = 0; j < cols; j++) {
        int colSum = 0;
        for (int i = 0; i < rows; i++) {
            colSum += matrix[i][j];
        }
        printf("Column %d: %d\n", j + 1, colSum);
    }

    return 0;
}

Write a program in C to accept a matrix and determine whether it is a sparse matrix.

(A sparse matrix is a matrix that contains a large number of zero elements compared to the total number of elements in the matrix. In other words, most of the elements in a sparse matrix have the value of zero.)

#include <stdio.h>

int main() {
    int matrix[100][100];
    int rows, cols, nonZeroCount = 0;

    // Input the number of rows and columns of the matrix
    printf("Enter the number of rows of the matrix: ");
    scanf("%d", &rows);
    printf("Enter the number of columns of the matrix: ");
    scanf("%d", &cols);

    // Input elements of the matrix
    printf("Enter elements of the matrix:\n");
    for (int i = 0; i < rows; i++) {
        printf("Enter elements for row %d:\n", i + 1);
        for (int j = 0; j < cols; j++) {
            printf("Element [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
            if (matrix[i][j] != 0) {
                nonZeroCount++;
            }
        }
    }

    // Determine if the matrix is sparse
    if (nonZeroCount < (rows * cols) / 2) {
        printf("\nThe matrix is a sparse matrix.\n");
    } else {
        printf("\nThe matrix is not a sparse matrix.\n");
    }

    return 0;
}

Write a program in C to accept two matrices and check whether they are equal.

#include <stdio.h>

int main() {
    int matrix1[100][100], matrix2[100][100];
    int rows1, cols1, rows2, cols2;

    // Input the number of rows and columns of the first matrix
    printf("Enter the number of rows of the first matrix: ");
    scanf("%d", &rows1);
    printf("Enter the number of columns of the first matrix: ");
    scanf("%d", &cols1);

    // Input elements of the first matrix
    printf("Enter elements of the first matrix:\n");
    for (int i = 0; i < rows1; i++) {
        printf("Enter elements for row %d:\n", i + 1);
        for (int j = 0; j < cols1; j++) {
            printf("Element [%d][%d]: ", i, j);
            scanf("%d", &matrix1[i][j]);
        }
    }

    // Input the number of rows and columns of the second matrix
    printf("\nEnter the number of rows of the second matrix: ");
    scanf("%d", &rows2);
    printf("Enter the number of columns of the second matrix: ");
    scanf("%d", &cols2);

    // Check if the dimensions of the two matrices are equal
    if (rows1 != rows2 || cols1 != cols2) {
        printf("\nThe matrices are not equal (different dimensions).\n");
        return 0;
    }

    // Input elements of the second matrix
    printf("Enter elements of the second matrix:\n");
    for (int i = 0; i < rows2; i++) {
        printf("Enter elements for row %d:\n", i + 1);
        for (int j = 0; j < cols2; j++) {
            printf("Element [%d][%d]: ", i, j);
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Check if the elements of the two matrices are equal
    int isEqual = 1; // Assume matrices are equal
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < cols1; j++) {
            if (matrix1[i][j] != matrix2[i][j]) {
                isEqual = 0; // Matrices are not equal
                break;
            }
        }
        if (!isEqual) {
            break;
        }
    }

    // Print the result
    if (isEqual) {
        printf("\nThe matrices are equal.\n");
    } else {
        printf("\nThe matrices are not equal.\n");
    }

    return 0;
}

On Key

Related Posts