Ad Space
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 6Explanation:
a[2][3] contains 2 rows and 3 columns.i) controls rows, and the second loop (for j) controls columns.a[i][j].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 8Explanation:
a[2][2][2] is a 3D array having 8 elements in total (2*2*2).a[i][j][k].#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 = HelloWorldExplanation:
gets() reads both strings from the user.while loop iterates i to find the end of the first string (the null character \0).while loop copies characters from str2 to the end of str1, starting from the position of the null character.\0 is added at the very end to mark the end of the newly combined string.c1 != r2. If true, multiplication is not possible; exit.i, j, k) to perform multiplication:
C[i][j] = C[i][j] + A[i][k] * B[k][j]#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;
}
vowels, consonants, digits, and spaces to 0.\0.tolower()) for easy comparison.vowels.consonants.digits.spaces.#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 2025Output:
Vowels: 3 Consonants: 7 Digits: 4 White spaces: 2
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 70Explanation:
marks[5] is an integer array that stores 5 elements.marks[0], marks[1], etc.).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 6Explanation:
matrix[2][3] has 2 rows and 3 columns.matrix[i][j].A string in C is a sequence of characters terminated by a null character (\0). It is represented as an array of characters.
char name[10] = "Ayman";
// This is stored as {'A', 'y', 'm', 'a', 'n', '\0'}
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". |
#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
#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
#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
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
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