💻 Unit 2 – Part B (13-Mark Q&A)

C Programming

⬅ Back to Unit 2

Ad Space

Part B: 13-Mark Questions

1. Discuss two dimensional and multi-dimensional array with example.

1. Two-Dimensional Array

A two-dimensional (2D) array is a collection of elements arranged in rows and columns, similar to a matrix or a table. It can be considered as an array of arrays.

Syntax:
data_type array_name[rows][columns];
Example Declaration:
int matrix[3][3]; // Creates a matrix with 3 rows and 3 columns
Example Program - Display 2D Array:
#include <stdio.h>
int main() {
    int i, j;
    int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
    
    printf("The 2D array elements are:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Output:
The 2D array elements are:
1 2 3
4 5 6
Explanation:

2. Multi-Dimensional Array

A multi-dimensional array is an extension of the 2D array. It has more than two dimensions, such as 3D, 4D, etc. They are used for storing data in 3D models or tables of values.

Syntax:
data_type array_name[size1][size2][size3]...[sizeN];
Example Declaration (3D Array):
int cube[2][2][2]; // 2 blocks, 2 rows, 2 columns
Example Program - 3D Array:
#include <stdio.h>
int main() {
    int a[2][2][2] = {
        { {1, 2}, {3, 4} },
        { {5, 6}, {7, 8} }
    };
    
    printf("Elements of 3D array:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                printf("%d ", a[i][j][k]);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}
Output:
Elements of 3D array:
1 2
3 4

5 6
7 8
Explanation:

2. Write a program to concatenate two strings without using built-in functions.

#include <stdio.h>

int main() {
    char str1[50], str2[50];
    int i = 0, j = 0;

    // Input two strings
    printf("Enter first string: ");
    gets(str1);
    printf("Enter second string: ");
    gets(str2);

    // Move i to the end of the first string
    while (str1[i] != '\0') {
        i++;
    }

    // Copy each character of str2 to str1
    while (str2[j] != '\0') {
        str1[i] = str2[j];
        i++;
        j++;
    }

    // Add null terminator at the end
    str1[i] = '\0';

    // Display concatenated string
    printf("Concatenated string = %s\n", str1);
    return 0;
}
Output:
Enter first string: Hello
Enter second string: World
Concatenated string = HelloWorld
Explanation:
  1. gets() reads both strings from the user.
  2. The first while loop iterates i to find the end of the first string (the null character \0).
  3. The second while loop copies characters from str2 to the end of str1, starting from the position of the null character.
  4. A new null character \0 is added at the very end to mark the end of the newly combined string.

3. Write a C program using two-dimensional arrays to perform Matrix Multiplication.

Algorithm:
  1. Start
  2. Input the number of rows (r1) and columns (c1) of matrix A.
  3. Input the number of rows (r2) and columns (c2) of matrix B.
  4. Check if c1 != r2. If true, multiplication is not possible; exit.
  5. Read elements of matrix A (r1 x c1).
  6. Read elements of matrix B (r2 x c2).
  7. Initialize a result matrix C (r1 x c2) with all elements as 0.
  8. Use three nested loops (for i, j, k) to perform multiplication:
    C[i][j] = C[i][j] + A[i][k] * B[k][j]
  9. Display the resultant matrix C.
  10. Stop
C Program:
#include <stdio.h>

int main() {
    int a[10][10], b[10][10], c[10][10];
    int r1, c1, r2, c2, i, j, k;

    // Input size of first matrix
    printf("Enter rows and columns of first matrix: ");
    scanf("%d %d", &r1, &c1);

    // Input size of second matrix
    printf("Enter rows and columns of second matrix: ");
    scanf("%d %d", &r2, &c2);

    // Check matrix multiplication condition
    if (c1 != r2) {
        printf("Matrix multiplication not possible!");
        return 0;
    }

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

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

    // Initialize result matrix to 0
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            c[i][j] = 0;
        }
    }

    // Matrix multiplication logic
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            for (k = 0; k < c1; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    // Display result matrix
    printf("Resultant Matrix:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            printf("%d ", c[i][j]);
        }
        printf("\n");
    }
    return 0;
}

4. Write a C program to find the number of Vowels, Consonants, Digits and White spaces in a string.

Algorithm:
  1. Start
  2. Read a string from the user.
  3. Initialize counters vowels, consonants, digits, and spaces to 0.
  4. Traverse each character of the string using a loop until the null character \0.
  5. Convert the character to lowercase (using tolower()) for easy comparison.
  6. Check if the character is a vowel ('a', 'e', 'i', 'o', 'u'). If yes, increment vowels.
  7. Else, check if it's an alphabet (between 'a' and 'z'). If yes, increment consonants.
  8. Else, check if it's a digit (between '0' and '9'). If yes, increment digits.
  9. Else, check if it's a space (' '). If yes, increment spaces.
  10. After the loop, display all the counts.
  11. Stop.
C Program:
#include <stdio.h>
#include <ctype.h> // For tolower() and isalpha()

int main() {
    char str[200];
    int i, vowels = 0, consonants = 0, digits = 0, spaces = 0;

    printf("Enter a string: ");
    gets(str);

    for (i = 0; str[i] != '\0'; i++) {
        char ch = str[i];

        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
            ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
            vowels++;
        }
        else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
            consonants++;
        }
        else if (ch >= '0' && ch <= '9') {
            digits++;
        }
        else if (ch == ' ') {
            spaces++;
        }
    }

    printf("\nVowels: %d", vowels);
    printf("\nConsonants: %d", consonants);
    printf("\nDigits: %d", digits);
    printf("\nWhite spaces: %d\n", spaces);
    return 0;
}
Sample Input:
Enter a string: Hello World 2025
Output:
Vowels: 3
Consonants: 7
Digits: 4
White spaces: 2

5. Explain the single dimensional array and 2D array with an example.

1. Single Dimensional Array (1D Array)

A Single Dimensional Array (1D Array) is a collection of elements of the same data type, stored in contiguous memory locations and accessed using a single subscript (index).

Syntax:
data_type array_name[size];
Example:
#include <stdio.h>
int main() {
    int marks[5] = {80, 75, 90, 85, 70};
    int i;
    
    printf("Marks of students:\n");
    for (i = 0; i < 5; i++) {
        printf("%d ", marks[i]);
    }
    printf("\n");
    return 0;
}
Output:
Marks of students:
80 75 90 85 70
Explanation:

2. Two Dimensional Array (2D Array)

A Two-Dimensional Array (2D Array) stores data in rows and columns (matrix form). It can be considered as an array of arrays.

Syntax:
data_type array_name[rows][columns];
Example:
#include <stdio.h>
int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int i, j;
    
    printf("Matrix elements:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Output:
Matrix elements:
1 2 3
4 5 6
Explanation:

6. Illustrate about a string and its methods with an example.

Definition

A string in C is a sequence of characters terminated by a null character (\0). It is represented as an array of characters.

Example Declaration:
char name[10] = "Ayman";
// This is stored as {'A', 'y', 'm', 'a', 'n', '\0'}

Common String Functions in C (String Methods)

C provides several built-in functions in the header file <string.h> to perform operations on strings.

Function Purpose Example
strlen(str) Finds the length of a string (excluding \0). strlen("Hello") returns 5.
strcpy(s1, s2) Copies string s2 into string s1. strcpy(dest, "Hi")
strcat(s1, s2) Concatenates (joins) string s2 to the end of s1. strcat(s1, "World")
strcmp(s1, s2) Compares two strings. Returns 0 if equal, < 0 if s1 < s2, > 0 if s1 > s2.
strrev(str) Reverses a string (Note: not standard C). strrev("abc") becomes "cba".
Example Program: String Operations
#include <stdio.h>
#include <string.h>

int main() {
    char str1[50] = "Hello";
    char str2[50] = "World";
    char str3[50];

    printf("String 1: %s\n", str1);
    printf("String 2: %s\n", str2);

    // 1. Length
    printf("Length of str1 = %zu\n", strlen(str1));

    // 2. Copy
    strcpy(str3, str1);
    printf("Copied String (str3) = %s\n", str3);

    // 3. Concatenate
    strcat(str1, str2);
    printf("Concatenated String (str1) = %s\n", str1);

    // 4. Compare
    // We compare str3 ("Hello") and str2 ("World")
    if (strcmp(str3, str2) == 0) {
        printf("Strings str3 and str2 are equal\n");
    } else {
        printf("Strings str3 and str2 are not equal\n");
    }
    
    return 0;
}
Output:
String 1: Hello
String 2: World
Length of str1 = 5
Copied String (str3) = Hello
Concatenated String (str1) = HelloWorld
Strings str3 and str2 are not equal

7. Write a C program to perform the following Matrix operation: i. Addition ii. Subtraction

i. C Program for Matrix Addition

#include <stdio.h>
int main() {
    int a[10][10], b[10][10], sum[10][10];
    int i, j, r, c;

    printf("Enter the number of rows and columns: ");
    scanf("%d %d", &r, &c);

    printf("\nEnter elements of first matrix:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    printf("\nEnter elements of second matrix:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            scanf("%d", &b[i][j]);
        }
    }

    // Matrix Addition
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            sum[i][j] = a[i][j] + b[i][j];
        }
    }

    printf("\nMatrix Addition Result:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Sample Output (Addition):
Enter the number of rows and columns: 2 2

Enter elements of first matrix:
1 2
3 4

Enter elements of second matrix:
5 6
7 8

Matrix Addition Result:
6 8
10 12

ii. C Program for Matrix Subtraction

#include <stdio.h>
int main() {
    int a[10][10], b[10][10], diff[10][10];
    int i, j, r, c;

    printf("Enter the number of rows and columns: ");
    scanf("%d %d", &r, &c);

    printf("\nEnter elements of first matrix:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    printf("\nEnter elements of second matrix:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            scanf("%d", &b[i][j]);
        }
    }

    // Matrix Subtraction
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            diff[i][j] = a[i][j] - b[i][j];
        }
    }

    printf("\nMatrix Subtraction Result:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            printf("%d ", diff[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Sample Output (Subtraction):
Enter the number of rows and columns: 2 2

Enter elements of first matrix:
8 6
7 5

Enter elements of second matrix:
3 2
1 4

Matrix Subtraction Result:
5 4
6 1

8. Describe a one dimensional and Two dimensional Array with a suitable example.

1. One-Dimensional Array

A one-dimensional array is a linear collection of elements of the same data type, stored in contiguous memory locations and accessed using a single index. The index starts from 0 and goes up to size-1.

Syntax:
data_type array_name[size];
Example:
#include <stdio.h>
int main() {
    // Initialize an array of 5 integers
    int marks[5] = {80, 70, 90, 85, 75};
    int i;
    
    printf("Marks of students:\n");
    // Loop from index 0 to 4
    for (i = 0; i < 5; i++) {
        printf("%d ", marks[i]); // Access using one index
    }
    printf("\n");
    return 0;
}
Output:
Marks of students:
80 70 90 85 75

2. Two-Dimensional Array

A two-dimensional array stores data in the form of rows and columns, visualized like a matrix or table. It is also known as an "array of arrays".

Syntax:
data_type array_name[rows][columns];
Example:
#include <stdio.h>
int main() {
    // Initialize a 2x3 matrix (2 rows, 3 columns)
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int i, j;
    
    printf("Matrix elements:\n");
    // Outer loop for rows
    for (i = 0; i < 2; i++) {
        // Inner loop for columns
        for (j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]); // Access using two indices
        }
        printf("\n"); // New line after each row
    }
    return 0;
}
Output:
Matrix elements:
1 2 3
4 5 6

Ad Space