C Programming: Matrix Data Transfer and Calculations
Classified in Computers
Written on in
English with a size of 4.45 KB
Matrix Data Transfer Example
This section presents C code intended to transfer data between matrices based on region identifiers.
#include <stdio.h>
#include <stdlib.h>
int main () {
int a[64][4];
int b[3][15];
int i, region, sum_asup, sum_ahab, j;
j = 0;
region = 1;
sum_asup = 0; // Initialize sums
sum_ahab = 0; // Initialize sums
// Assuming 'a' matrix is populated with data
for (i = 0; i < 64; i++) { // Corrected loop condition
if (a[i][0] == region) {
sum_asup = sum_asup + a[i][2];
sum_ahab = sum_ahab + a[i][3];
}
// Check if the next element belongs to a different region or if it's the end of the array
if (i + 1 < 64 && a[i+1][0] != region) {
b[0][j] = region;
b[1][j] = sum_asup;
b[2][j] = sum_ahab;
j++;
sum_asup = 0;
sum_ahab = 0;
region++;
} else if (i + 1 == 64) { // Handle the last region if it ends exactly at the array boundary
b[0][j] = region;
b[1][j] = sum_asup;
b[2][j] = sum_ahab;
j++;
}
}
// Handling the last element if it was part of the current region and the loop finished
// Note: The logic above might need refinement based on exact requirements for the last entry.
printf("Data transfer process completed. Check matrix b.\n");
return 0;
}
3x3 Matrix: Sum and Difference Based on Position Parity
This C program prompts the user to enter 9 numbers into a 3x3 matrix. Numbers at positions where the sum of subscripts (i+j) is even are added to the result; those where the sum is odd are subtracted.
#include <stdio.h>
#include <conio.h> // For getch()
#define F 3
#define C 3
int main () {
int a[F][C];
int num, result = 0, i, j;
printf("Enter 9 numbers for the 3x3 matrix:\n");
for (i = 0; i < F; i++) { // Corrected loop condition
for (j = 0; j < C; j++) { // Corrected loop condition
printf ( "Enter number a[%d][%d]: ", i, j);
if (scanf ( "%d", &num) != 1) {
// Handle input error if necessary
return 1;
}
a[i][j] = num;
// Position parity check: 0 is considered even
if ((i + j) % 2 == 0) {
result = result + num;
} else {
result = result - num;
}
}
}
printf ( "\nResult (Even positions added, Odd positions subtracted) is: %d\n", result);
printf ( "Press any key to continue...\n");
getch ();
return 0;
}
4x4 Matrix Calculations
This program handles a 4x4 matrix, calculating the sums of:
- The main diagonal.
- The secondary diagonal.
- The first and last rows combined.
- The first and last columns combined.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int matr[4][4];
int suma_dp = 0; // Sum of main diagonal
int suma_ds = 0; // Sum of secondary diagonal
int suma_pu_f = 0; // Sum of first and last row
int suma_pu_c = 0; // Sum of first and last column
printf("Enter 16 numbers for the 4x4 matrix:\n");
// Input Matrix
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) { // Corrected loop condition
printf ( "Enter number matr[%d][%d]: ", i, j);
if (scanf ( "%d", &matr[i][j]) != 1) {
return 1;
}
}
}
// Calculations
for (int l = 0; l < 4; l++) {
// a. Main diagonal (i == j)
suma_dp += matr[l][l];
// b. Secondary Diagonal (i + j == 3)
suma_ds += matr[l][3 - l];
// c. First row (l) and Last row (3)
suma_pu_f += matr[0][l]; // First row
suma_pu_f += matr[3][l]; // Last row
// d. First column (l) and Last column (3)
suma_pu_c += matr[l][0]; // First column
suma_pu_c += matr[l][3]; // Last column
}
printf ( "\nSum of the main diagonal: %d\n", suma_dp);
printf ( "Sum of the secondary diagonal: %d\n", suma_ds);
printf ( "Sum of the first and last row: %d\n", suma_pu_f);
printf ( "Sum of the first and last column: %d\n", suma_pu_c);
printf ( "Press Enter to exit...\n");
system ( "pause");
return 0;
}