Week 7 & 8 Worksheet

Filter:
Sort:

Question 1

What is the total number of times the body executes in the following nested loop?

for (int i = 15; i <= 40; i++) {
    for (int j = 10; j <= 20; j += 2) {
        // Body
    }
}
Solution
Total Operations: 156


                        
Total Operations: 156

                        

Question 2

What is the total number of times the body executes in the following nested loop?

for (int i = 1000; i > 500; i -= 25) {
    for (int j = 0; j < 200; j++) {
        // Body
    }
}
Solution
Total Operations: 4000
                        
Total Operations: 4000
                        

Question 3

Write a CC++ program that asks the user for a size N and prints a Hollow Square of asterisks (*) of size N x N.

Example:

Enter size: 5
*****
*   *
*   *
*   *
*****
Solution
#include <stdio.h>

int main() {
    int n;
    printf("Enter size: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter size: ";
    cin >> n;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n) {
                cout << "*";
            } else {
                cout << " ";
            }
        }
        cout << "\n";
    }

    return 0;
}

                        

Question 4

Write a CC++ program that asks the user for the size N of a square matrix, reads the matrix elements, and then prints the elements of the Main Diagonal with their spatial positioning.

Example:

Enter size of square matrix: 3
Enter elements:
1 2 3
4 5 6
7 8 9

Main Diagonal:
1
 5
  9 
Solution
#include <stdio.h>

int main() {
    int n;
    printf("Enter size of square matrix: ");
    scanf("%d", &n);

    int arr[n][n];

    printf("Enter elements:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &arr[i][j]);
        }
    }

    printf("Main Diagonal:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if(i == j)
                printf("%d ",arr[i][j]);
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter size of square matrix: ";
    cin >> n;

    int arr[n][n];

    cout << "Enter elements:\n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> arr[i][j];
        }
    }

    cout << "Main Diagonal:\n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j)
                cout << arr[i][j] << " ";
            else
                cout << " ";
        }
        cout << "\n";
    }

    return 0;
}

                        

Question 5

Write a CC++ program that asks the user for the size N of a square matrix, reads the matrix elements, and then prints the elements of the Cross Diagonal (Anti-Diagonal) with their spatial positioning.

Example:

Enter size of square matrix: 3
Enter elements:
1 2 3
4 5 6
7 8 9

Cross Diagonal:
  3
 5
7 
Solution
#include <stdio.h>

int main() {
    int n;
    printf("Enter size of square matrix: ");
    scanf("%d", &n);

    int arr[n][n];

    printf("Enter elements:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &arr[i][j]);
        }
    }

    printf("Cross Diagonal:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if(i == n-1-j)
                printf("%d ",arr[i][j]);
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter size of square matrix: ";
    cin >> n;

    int arr[n][n];

    cout << "Enter elements:\n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> arr[i][j];
        }
    }

    cout << "Cross Diagonal:\n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == n-1-j)
                cout << arr[i][j] << " ";
            else
                cout << " ";
        }
        cout << "\n";
    }

    return 0;
}

                        

Question 6

Write a CC++ program to read elements in two matrices and multiply them. The program should check if the dimensions allow for multiplication (Columns of A = Rows of B).

Matrix Multiplication

Example:

Enter rows and columns for Matrix A: 2 2
Enter rows and columns for Matrix B: 2 2
Enter elements of Matrix A:
1 2
3 4
Enter elements of Matrix B:
1 1
1 1
Result Matrix:
3 3 
7 7 
Solution
#include <stdio.h>

int main() {
    int r1, c1, r2, c2;

    printf("Enter rows and columns for Matrix A: ");
    scanf("%d %d", &r1, &c1);

    printf("Enter rows and columns for Matrix B: ");
    scanf("%d %d", &r2, &c2);

    if (c1 != r2) {
        printf("Error! Column of A must be equal to Row of B.\n");
        return 0;
    }

    int a[r1][c1], b[r2][c2], result[r1][c2];

    printf("Enter elements of Matrix A:\n");
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c1; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    printf("Enter elements of Matrix B:\n");
    for (int i = 0; i < r2; i++) {
        for (int j = 0; j < c2; j++) {
            scanf("%d", &b[i][j]);
        }
    }

    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            result[i][j] = 0;
        }
    }

    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            for (int k = 0; k < c1; k++) {
                result[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    printf("Result Matrix:\n");
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    int r1, c1, r2, c2;

    cout << "Enter rows and columns for Matrix A: ";
    cin >> r1 >> c1;

    cout << "Enter rows and columns for Matrix B: ";
    cin >> r2 >> c2;

    if (c1 != r2) {
        cout << "Error! Column of A must be equal to Row of B.\n";
        return 0;
    }

    int a[r1][c1], b[r2][c2], result[r1][c2];

    cout << "Enter elements of Matrix A:\n";
    for (int i = 0; i < r1; i++)
        for (int j = 0; j < c1; j++)
            cin >> a[i][j];

    cout << "Enter elements of Matrix B:\n";
    for (int i = 0; i < r2; i++)
        for (int j = 0; j < c2; j++)
            cin >> b[i][j];

    for (int i = 0; i < r1; i++)
        for (int j = 0; j < c2; j++)
            result[i][j] = 0;

    for (int i = 0; i < r1; i++)
        for (int j = 0; j < c2; j++)
            for (int k = 0; k < c1; k++)
                result[i][j] += a[i][k] * b[k][j];

    cout << "Result Matrix:\n";
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++)
            cout << result[i][j] << " ";
        cout << "\n";
    }

    return 0;
}

                        

Question 7

Write a CC++ program that reads a 3×4 matrix from the user, then uses two separate functionsprintMatrix to display it and sumMatrix to compute and return the total of all elements — and prints the result in main.

Example:

Enter elements of the 3x4 matrix:
1 2 3 4
5 6 7 8
9 10 11 12

Matrix:
1 2 3 4 
5 6 7 8 
9 10 11 12 

Sum of all elements: 78
Solution
#include <stdio.h>

#define ROWS 3
#define COLS 4

void printMatrix(int rows, int cols, int arr[][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

int sumMatrix(int rows, int cols, int arr[][cols]) {
    int total = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            total += arr[i][j];
        }
    }
    return total;
}

int main() {
    int matrix[ROWS][COLS];

    printf("Enter elements of the %dx%d matrix:\n", ROWS, COLS);
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    printf("\nMatrix:\n");
    printMatrix(ROWS, COLS, matrix);

    printf("\nSum of all elements: %d\n", sumMatrix(ROWS, COLS, matrix));

    return 0;
}

                        
#include <iostream>
using namespace std;

#define ROWS 3
#define COLS 4

void printMatrix(int arr[][COLS], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << arr[i][j] << " ";
        }
        cout << "\n";
    }
}

int sumMatrix(int arr[][COLS], int rows, int cols) {
    int total = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            total += arr[i][j];
        }
    }
    return total;
}

int main() {
    int matrix[ROWS][COLS];

    cout << "Enter elements of the " << ROWS << "x" << COLS << " matrix:\n";
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            cin >> matrix[i][j];
        }
    }

    cout << "\nMatrix:\n";
    printMatrix(matrix, ROWS, COLS);

    cout << "\nSum of all elements: " << sumMatrix(matrix, ROWS, COLS) << "\n";

    return 0;
}

                        

Question 8

Write a CC++ program that reads a 3×4 matrix from the user and uses a function countEvens to count and return the number of even elements in the matrix. Print the result in main.

Example:

Enter elements of the 3x4 matrix:
1 2 3 4
5 6 7 8
9 10 11 12

Number of even elements: 6
Solution
#include <stdio.h>

#define ROWS 3
#define COLS 4

int countEvens(int rows, int cols, int arr[][cols]) {
    int count = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (arr[i][j] % 2 == 0) {
                count++;
            }
        }
    }
    return count;
}

int main() {
    int matrix[ROWS][COLS];

    printf("Enter elements of the %dx%d matrix:\n", ROWS, COLS);
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    printf("\nNumber of even elements: %d\n", countEvens(ROWS, COLS, matrix));

    return 0;
}

                        
#include <iostream>
using namespace std;

#define ROWS 3
#define COLS 4

int countEvens(int arr[][COLS], int rows, int cols) {
    int count = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (arr[i][j] % 2 == 0) {
                count++;
            }
        }
    }
    return count;
}

int main() {
    int matrix[ROWS][COLS];

    cout << "Enter elements of the " << ROWS << "x" << COLS << " matrix:\n";
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            cin >> matrix[i][j];
        }
    }

    cout << "\nNumber of even elements: " << countEvens(matrix, ROWS, COLS) << "\n";

    return 0;
}

                        

Question 9

Write a complete CC++ program that reads a matrix of size $R \times C$ from the user. The program should:

  1. For each row, calculate and print the largest absolute difference between any two adjacent elements.
  2. Calculate and print the largest absolute difference between any two adjacent elements across the entire matrix.

Example:

Enter rows and columns: 3 4
Enter elements:
1 5 2 8
10 12 11 9
3 3 8 1

Row 0 largest adjacent difference: 6
Row 1 largest adjacent difference: 2
Row 2 largest adjacent difference: 7
Max adjacent difference across whole matrix: 7
Solution
#include <stdio.h>
#include <stdlib.h>

int main() {
    int r, c;
    printf("Enter rows and columns: ");
    scanf("%d %d", &r, &c);

    int arr[50][50];
    printf("Enter elements:\n");
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            scanf("%d", &arr[i][j]);
        }
    }

    int overallMaxDiff = 0;

    for (int i = 0; i < r; i++) {
        int rowMaxDiff = 0;
        for (int j = 0; j < c - 1; j++) {
            int diff = abs(arr[i][j] - arr[i][j + 1]);
            if (diff > rowMaxDiff) {
                rowMaxDiff = diff;
            }
        }
        printf("Row %d largest adjacent difference: %d\n", i, rowMaxDiff);
        if (rowMaxDiff > overallMaxDiff) {
            overallMaxDiff = rowMaxDiff;
        }
    }

    printf("Max adjacent difference across whole matrix: %d\n", overallMaxDiff);

    return 0;
}

                        
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int r, c;
    cout << "Enter rows and columns: ";
    cin >> r >> c;

    int arr[50][50];
    cout << "Enter elements:" << endl;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            cin >> arr[i][j];
        }
    }

    int overallMaxDiff = 0;

    for (int i = 0; i < r; i++) {
        int rowMaxDiff = 0;
        for (int j = 0; j < c - 1; j++) {
            int diff = abs(arr[i][j] - arr[i][j + 1]);
            if (diff > rowMaxDiff) {
                rowMaxDiff = diff;
            }
        }
        cout << "Row " << i << " largest adjacent difference: " << rowMaxDiff << endl;
        if (rowMaxDiff > overallMaxDiff) {
            overallMaxDiff = rowMaxDiff;
        }
    }

    cout << "Max adjacent difference across whole matrix: " << overallMaxDiff << endl;

    return 0;
}

                        

Question 10

Write a complete CC++ program including a function PrintPascal() that computes and prints Pascal’s Triangle up to $N$ rows using a 2D array representation.

In main(), read the number of rows from the user, and call the function to print the triangle.

Example:

Enter number of rows: 5
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
Solution
#include <stdio.h>

void PrintPascal(int n) {
    int arr[50][50];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0 || j == i) {
                arr[i][j] = 1;
            } else {
                arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
            }
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int n;
    printf("Enter number of rows: ");
    scanf("%d", &n);

    PrintPascal(n);

    return 0;
}

                        
#include <iostream>
using namespace std;

void PrintPascal(int n) {
    int arr[50][50];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0 || j == i) {
                arr[i][j] = 1;
            } else {
                arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
            }
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int n;
    cout << "Enter number of rows: ";
    cin >> n;

    PrintPascal(n);

    return 0;
}

                        

Question 11

Write a complete CC++ program including a function SwapRows() that receives a 2D array, its dimensions, and two row indices, and swaps the elements of the two rows.

In main(), read the row and column size along with the matrix elements. Then, read the two row indices to swap, call SwapRows(), and print the matrix after the swap.

Example:

Enter rows and columns: 3 3
Enter elements:
1 2 3
4 5 6
7 8 9
Enter rows to swap: 0 2

Matrix after swap:
7 8 9
4 5 6
1 2 3
Solution
#include <stdio.h>

void SwapRows(int r, int c, int arr[][c], int r1, int r2) {
    for (int i = 0; i < c; i++) {
        int temp = arr[r1][i];
        arr[r1][i] = arr[r2][i];
        arr[r2][i] = temp;
    }
}

int main() {
    int r, c, r1, r2;
    printf("Enter rows and columns: ");
    scanf("%d %d", &r, &c);

    int arr[50][50];
    printf("Enter elements:\n");
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            scanf("%d", &arr[i][j]);
        }
    }

    printf("Enter rows to swap: ");
    scanf("%d %d", &r1, &r2);

    SwapRows(r, c, arr, r1, r2);

    printf("\nMatrix after swap:\n");
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

                        
#include <iostream>
using namespace std;

#define MAX_COLS 50

void SwapRows(int arr[][MAX_COLS], int r, int c, int r1, int r2) {
    for (int i = 0; i < c; i++) {
        int temp = arr[r1][i];
        arr[r1][i] = arr[r2][i];
        arr[r2][i] = temp;
    }
}

int main() {
    int r, c, r1, r2;
    cout << "Enter rows and columns: ";
    cin >> r >> c;

    int arr[50][MAX_COLS];
    cout << "Enter elements:" << endl;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            cin >> arr[i][j];
        }
    }

    cout << "Enter rows to swap: ";
    cin >> r1 >> r2;

    SwapRows(arr, r, c, r1, r2);

    cout << "\nMatrix after swap:" << endl;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}