1. Practice: Matrix Operations Using Functions
Problem Statement
Write a C program to perform matrix addition and matrix multiplication using functions.
Matrix Addition Program
Logic
- Use a function to read matrices
- Use a function to add matrices
- Use a function to display the result
Code
#include <stdio.h>
void readMatrix(int a[10][10], int r, int c) {
int i, j;
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
}
void addMatrix(int a[10][10], int b[10][10], int sum[10][10], int r, int c) {
int i, j;
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
sum[i][j] = a[i][j] + b[i][j];
}
void displayMatrix(int a[10][10], int r, int c) {
int i, j;
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
int main() {
int a[10][10], b[10][10], sum[10][10];
int r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("Enter first matrix:\n");
readMatrix(a, r, c);
printf("Enter second matrix:\n");
readMatrix(b, r, c);
addMatrix(a, b, sum, r, c);
printf("Sum of matrices:\n");
displayMatrix(sum, r, c);
return 0;
}
Matrix Multiplication Program
#include <stdio.h>
void multiplyMatrix(int a[10][10], int b[10][10], int result[10][10], int r1, int c1, int c2) {
int i, j, k;
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
result[i][j] = 0;
for(k = 0; k < c1; k++)
result[i][j] += a[i][k] * b[k][j];
}
}
}
int main() {
int a[10][10], b[10][10], result[10][10];
int r1, c1, r2, c2, i, j;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
if(c1 != r2) {
printf("Matrix multiplication not possible");
return 0;
}
printf("Enter first matrix:\n");
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
multiplyMatrix(a, b, result, r1, c1, c2);
printf("Result matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++)
printf("%d ", result[i][j]);
printf("\n");
}
return 0;
}
2. Practice: Reverse a String Using Function
Problem Statement
Write a C program to reverse a string using a function.
Logic
- Pass string to function
- Reverse string using loop
- Display reversed string
Code
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int i, len;
char temp;
len = strlen(str);
for(i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
reverseString(str);
printf("Reversed string: %s", str);
return 0;
}
3. Practice: Count Vowels in a String Using Function
Problem Statement
Write a C program to count the number of vowels in a string using a function.
Logic
- Pass string to function
- Check characters against vowels
- Return vowel count
Code
#include <stdio.h>
int countVowels(char str[]) {
int i, count = 0;
for(i = 0; str[i] != '\0'; i++) {
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' ||
str[i] == 'O' || str[i] == 'U') {
count++;
}
}
return count;
}
int main() {
char str[100];
int vowels;
printf("Enter a string: ");
gets(str);
vowels = countVowels(str);
printf("Number of vowels: %d", vowels);
return 0;
}
Practice Tips for Learners
- Convert all programs to menu-driven
- Try using pointers instead of arrays
- Rewrite programs using return types
- Add input validation
- Practice without using built-in functions
Summary
These practice problems help you:
- Apply functions and return types
- Strengthen modular programming
- Improve problem-solving skills
- Prepare for interviews and exams