Week 7: Arrays (2D Arrays) in C
Note: For all questions, you can define M and N using
#definedirective at the beginning of your program if required.
Q1. Store Total Salary of N Employees
Question:
Write a program to store the total salary of N employees in an array. For each employee calculate the total salary.
- HRA Allowance: 40% of basic salary
- Special Allowance: 25% of basic salary
- Total Salary: Basic + HRA + Special Allowance
After calculating, sort the array in descending order of total salary.
Program:
#include <stdio.h>
int main() {
int i, n;
printf("Enter the total number of employees: ");
scanf("%d", &n);
float a[n][3], basicSalary, houseAllowance, allowance, totalSalary;
for(i = 0; i < n; i++) {
printf("Enter the basic salary for employee %d: ", i + 1);
scanf("%f", &basicSalary);
houseAllowance = basicSalary * 0.4;
allowance = basicSalary * 0.25;
totalSalary = basicSalary + houseAllowance + allowance;
a[i][0] = basicSalary;
a[i][1] = houseAllowance;
a[i][2] = totalSalary;
}
printf("\nSalaries details of employees:\n");
for(i = 0; i < n; i++) {
printf("Employee %d - Basic: %.2f, House Allowance: %.2f, Total Salary: %.2f\n", i + 1, a[i][0], a[i][1], a[i][2]);
}
return 0;
}
````
---
## Q2. Sum of Each Row & Column of an MxN Matrix
**Question:**
Write a program to find the sum of each row and each column of a MxN matrix.
**Program:**
```c
#include <stdio.h>
int main() {
int i, j, m, n;
printf("Enter number of rows and columns: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
printf("Enter elements of the matrix:\n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("Row sums:\n");
for(i = 0; i < m; i++) {
int rowSum = 0;
for(j = 0; j < n; j++) {
rowSum += matrix[i][j];
}
printf("Sum of row %d: %d\n", i + 1, rowSum);
}
printf("Column sums:\n");
for(j = 0; j < n; j++) {
int colSum = 0;
for(i = 0; i < m; i++) {
colSum += matrix[i][j];
}
printf("Sum of column %d: %d\n", j + 1, colSum);
}
return 0;
}
Q3. Check if Matrix is Square & Sum of Diagonals
Question:
Check if the matrix is square; if not, print Invalid. If it is square, calculate the sum of the main diagonal and opposite diagonal.
Program:
#include <stdio.h>
int main() {
int i, j, m, n;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);
if (m != n) {
printf("Invalid\n");
return 0;
}
int matrix[m][n];
printf("Enter elements of the matrix:\n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
int mainDiagonalSum = 0, oppositeDiagonalSum = 0;
for(i = 0; i < m; i++) {
mainDiagonalSum += matrix[i][i];
oppositeDiagonalSum += matrix[i][m - i - 1];
}
printf("Main diagonal sum: %d\n", mainDiagonalSum);
printf("Opposite diagonal sum: %d\n", oppositeDiagonalSum);
return 0;
}
Q4. Linear Search in MxN Matrix
Question: Perform linear search in a MxN matrix and print all occurrences of the key element.
Program:
#include <stdio.h>
int main() {
int i, j, m, n, key, found = 0;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
printf("Enter the elements of the matrix:\n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("Enter the key element to search for: ");
scanf("%d", &key);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
if(matrix[i][j] == key) {
printf("Found %d at position (%d, %d)\n", key, i, j);
found = 1;
}
}
}
if(!found) {
printf("Element %d not found in the matrix.\n", key);
}
return 0;
}