Practice Problems on Arrays and Strings in C

Posted on December 18, 2023 by Vishesh Namdev
Python C C++ Java
C Programming Language

Basic Practice Problems based on chapter which you learn in previous Tutorials.

1. Largest Element in Array

#include <stdio.h>Copy Code
int main() {
int size;

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

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

// Find the largest element in the array
int largest = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}

// Display the result
printf("The largest element in the array is: %d\n", largest);

return 0;
}

2. Addition of two Matrics

#include <stdio.h>Copy Code
int main() {
int rows, columns;

// Input the dimensions of the matrices
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &columns);

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

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

// Perform matrix addition
int sumMatrix[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the result
printf("Sum of the matrices:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d\t", sumMatrix[i][j]);
}
printf("\n");
}

return 0;
}

3. Multiply two Matrics

#include <stdio.h>Copy Code
#define MAX_SIZE 10

int main() {
int firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
int rowFirst, colFirst, rowSecond, colSecond;

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

// Input elements of the first matrix
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < colFirst; j++) {
scanf("%d", &firstMatrix[i][j]);
}
}

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

// Input elements of the second matrix
printf("Enter the elements of the second matrix:\n");
for (int i = 0; i < rowSecond; i++) {
for (int j = 0; j < colSecond; j++) {
scanf("%d", &secondMatrix[i][j]);
}
}

// Check if matrices can be multiplied
if (colFirst != rowSecond) {
printf("Matrices cannot be multiplied.\n");
return 1; // Exit with an error code
}

// Perform matrix multiplication
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < colSecond; j++) {
result[i][j] = 0;
for (int k = 0; k < colFirst; k++) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}

// Display the result
printf("Product of the matrices:\n");
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < colSecond; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}

return 0;
}

4. Length of string

#include <stdio.h>Copy Code
#include <string.h>

int main() {
char str[100]; // Assuming a maximum length of 100 characters

// Input a string
printf("Enter a string: ");
scanf("%s", str);

// Calculate and display the length of the string
int length = strlen(str);
printf("Length of the string: %d\n", length);

return 0;
}

5. Concatination of string

The concatenation operators combine two strings to form one string by appending the second string to the right-hand end of the first string.

#include <stdio.h>Copy Code
#include <string.h>

int main() {
char str1[100], str2[100]; // Assuming maximum lengths of 100 characters

// Input two strings
printf("Enter the first string: ");
scanf("%s", str1);

printf("Enter the second string: ");
scanf("%s", str2);

// Concatenate the two strings
strcat(str1, str2);

// Display the concatenated string
printf("Concatenated string: %s\n", str1);

return 0;
}