자료구조 과제 🤓 A+기원🤓
#include<stdio.h>
#define ROWS 6
#define COLS 6
#define MAX_TERMS 10
defining rows, cols, and max terms as constant value to efficient program operation.
typedef struct {
int row;
int col;
int value;
} element;
typedef struct SparseMatrix {
element data[MAX_TERMS];
int rows; //row size
int cols; //cloumn size
int terms; // the number of elemnets
}SparseMatrix;
defining two structures: 'element' and 'SparseMatrix.'
'element' represents a single element in a sparse matrix with attributes for its row, column, and value.
'SparseMatrix' is a structure for representing a sparse matrix and includes an array of 'element' objects, as well as attributes for the total number of rows, columns, and non-zero elements in the matrix.
void printMat(element data[]) {
int k = 0, i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (data[k].row == i && data[k].col == j) {
printf("%d ", data[k].value);
k++;
}
else {
printf("0 ");
}
}
printf("\n");
}
} //print out Matrix
Function printMat is takes an array of ‘element’ structures as its input parameter. This function is used to print a matrix.
//void main()
SparseMatrix B = { {{0,3,7},{1,0,9},{1,5,8},{3,0,6},{3,1,5}, {4,5,1}, {5,2,2}}, ROWS, COLS, 7 };
//Add B as an input
SparseMatrix result;
element temp;
int i, j, k = 0;
result.cols = B.cols;
result.rows = B.rows;
result.terms = B.terms; //Initialize result
Declare and initialize variables.
for (i = 0; i < B.rows; i++) {
for (j = 0; j < B.cols; j++) {
if (B.data[k].row == i && B.data[k].col == j) {
result.data[k].row = j;
result.data[k].col = i;
result.data[k].value = B.data[k++].value;
}
}
}//Perform the transpose operation
if statement checks if the current element at index ‘k’in the ‘B.data’ array has a row and column index matches the current values of ‘i’and ‘j’. If they match, it means that there is non-zero element at this position in the ‘B’ matrixThis neasted ‘for’loops are used for sorting the elements in the ‘result’ matrix to ensures that its elements are oredered by row index.
for (i = 0; i < result.terms; i++) {
for (j = i; j < result.terms - 1; j++) {
if (result.data[j].row > result.data[j + 1].row) {
temp = result.data[j];
result.data[j] = result.data[j + 1];
result.data[j + 1] = temp;
}
}
}//Perform the sorting operation, completing transposed matrix
⇒ reoerders the elements so that they are sorted vby row index in ascending order.
//Print out B and B ^T
printf("== Matrix B ==\n");
printMat(B.data);
printf("== Matrix B^T ==\n");
printMat(result.data);
this lines of code print the original matrix ‘B’ and its transpose ‘b^T’ to the console, allowing visualize both matrices and verify the result of the tanspose operation and sorting
//Implement a function 'mem-alloc_3D_double' of allocationg 3D array of double.
//Then, use this as below
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define HEIGHT 3
#define ROW 3
#define COL 3
defining constant for the dimensions of 3D double array.
void mem_Alloc_3D_double(double ****p) {
(*p) = (double***)malloc(sizeof(double**) * HEIGHT);
int i = 0, j = 0, k=0;
//allocate memory for each dimension of the 3D array
for (i = 0; i < HEIGHT; i++) {
(*p)[i] = (double**)malloc(sizeof(double*) * ROW);
for (j = 0; j < ROW; j++) {
(*p)[i][j] = (double*)malloc(sizeof(double) * COL);
for (k = 0; k < COL; k++){
(*p)[i][j][k] = (i*ROW*COL)+(j*COL)+k+1;
//initialize
printf("%.2f ", (*p)[i][j][k]);
//print result
}
printf("\n");
}
printf("[%d]================\n\n", i+1);
}
printf(">>Allocation Completed!\n\n");
}
function mem_Alloc_3D_double takes a pointer to a 3D double pointer (****p) as its parameter. this function dynamically allocates memory for a 3D array of double values with dimensions specified by the constans.
void addition_3D(double ***p1, double ***p2) {
int i, j, k;
//iterate each element of the matrices and print sum
printf("\nSum of the two 3D arrays!---------------------\n");
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < ROW; j++) {
for (k = 0; k < COL; k++) {
printf("%.2f ", p1[i][j][k] + p2[i][j][k]);
}
printf("\n");
}
printf("[%d]================\n", i+1);
}
printf(">>Addition Completed!\n\n");
}
function addition_3D takes two 3D double arrays represented as pointers p1and p2 as its parametes. It calculates the element addition of the two arrays and prints the result.
void main() {
//Define two matrices A and B using 'mem_alloc_3D_double';
double ***A = NULL;
double ***B = NULL;
mem_Alloc_3D_double(&A);
mem_Alloc_3D_double(&B);
//Perform addition of two matrices using 'addition_3D()'
addition_3D(A, B);
//Deallocate A and B
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < ROW; j++) {
free(A[i][j]);
free(B[i][j]);
}
free(A[i]);
free(B[i]);
}
free(A);
free(B);
printf(">>Deallocation Completed!\n\n");
}
mem_Alloc_3D_double function for each of them.addition_3D function to calculate and print the result of two matrix addition.포인터는 할때마다 헷갈린다. 더 많이 쓰면서 익숙해지는 수 밖에!