0% found this document useful (0 votes)
15 views

DSDFSDC

This document contains a student's programming exercise report on sparse matrix transposition. The purpose of the exercise was to learn about matrix storage formats and algorithms for transposition. The student implemented two versions of a sparse matrix transposition function - a basic version and a faster version using an indexing approach. Testing on matrices of varying sizes from 10x10 to 1000x1000 showed that the faster version reduced the number of computation steps compared to the basic approach. The student encountered some syntax errors during coding which were resolved. Overall, following the provided algorithm structure made the coding process clearer despite the complexity of the algorithms.

Uploaded by

GM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

DSDFSDC

This document contains a student's programming exercise report on sparse matrix transposition. The purpose of the exercise was to learn about matrix storage formats and algorithms for transposition. The student implemented two versions of a sparse matrix transposition function - a basic version and a faster version using an indexing approach. Testing on matrices of varying sizes from 10x10 to 1000x1000 showed that the faster version reduced the number of computation steps compared to the basic approach. The student encountered some syntax errors during coding which were resolved. Overall, following the provided algorithm structure made the coding process clearer despite the complexity of the algorithms.

Uploaded by

GM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

112 學年度第 1 學期

EE2120 資料結構
Programming Exercise ?
簡易報告
甲班

2023/09/22
一、 目的
請說明此程式練習之目的。
(字體:新細明體,大小:14,單行間距,左右對齊)
這次主要是學習矩陣的排列方式,以及數值是用甚麼演算法排入矩
陣中,由此特性去做轉換,再來是學習判斷程式執行的步驟數,對
程式的執行好壞做判斷。
二、 程式碼與執行結果
請列出最後版本之程式碼,並將執行結果之截圖貼附於本節中。
(字體:新細明體,大小:14,單行間距,左右對齊) int i,
j, value;
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
//#include <malloc.h>
#include <time.h>

typedef struct {
int col;
int row;
int value;
} term;

int step;

#define STEPCOUNT_EN 1
#define MAX_COL 1001

void SparseMatrix_generator(term sm[], int n) {


int i;

sm[0].row = n;
sm[0].col = n;
sm[0].value = n;

srand((unsigned)time(NULL));

for (i = 0; i < n; i++) {


sm[i + 1].row = i;
sm[i + 1].col = rand() % n;
sm[i + 1].value = rand() % 100;
}
}

void transpose(term a[], term b[]) {


int n, i, j, currentb;
step = 0;
n = a[0].value;
b[0].row = a[0].col;
b[0].col = a[0].row;
b[0].value = n;

if (STEPCOUNT_EN) step++;
if (n > 0) {
if (STEPCOUNT_EN) step++;
currentb = 1;
if (STEPCOUNT_EN) step++;
for (i = 0; i < a[0].col; i++) {
if (STEPCOUNT_EN) step++;
for (j = 1; j <= n; j++) {
if (STEPCOUNT_EN) step++;
if (a[j].col == i) {
if (STEPCOUNT_EN) step++;
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++;
if (STEPCOUNT_EN) step++;
}
if (STEPCOUNT_EN) step++;
}
if (STEPCOUNT_EN) step++;
}
if (STEPCOUNT_EN) step++;
}
if (STEPCOUNT_EN) step++;
}

void fastTranspose(term a[], term b[]) {

int rowTerms[MAX_COL], startingPos[MAX_COL];


step = 0;
int i, j, NumberCols = a[0].col, NumberTerms = a[0].value;
if (STEPCOUNT_EN) step++;

b[0].row = NumberCols;
b[0].col = a[0].row;
b[0].value = NumberTerms;
if (STEPCOUNT_EN) step++;

if (NumberTerms > 0) {
if (STEPCOUNT_EN) step++;
for (i = 0; i < NumberCols; i++) {
rowTerms[i] = 0;
}
if (STEPCOUNT_EN) step++;
for (i = 1; i <= NumberTerms; i++) {
rowTerms[a[i].col]++;
}
if (STEPCOUNT_EN) step++;
startingPos[0] = 1;
if (STEPCOUNT_EN) step++;
for (i = 1; i < NumberCols; i++) {
if (STEPCOUNT_EN) step++;
startingPos[i] = startingPos[i - 1] + rowTerms[i - 1];
}
if (STEPCOUNT_EN) step++;
for (i = 1; i <= NumberTerms; i++) {
if (STEPCOUNT_EN) step++;
j = startingPos[a[i].col]++;
b[j].row = a[i].col;
b[j].col = a[i].row;
b[j].value = a[i].value;
if (STEPCOUNT_EN) step++;
}
if (STEPCOUNT_EN) step++;
}
if (STEPCOUNT_EN) step++;
}
void main() {
int i, n;

term* sm_array; // single linear list for the input sparse matrix
term* sm_transpose_result; // single linear list for the transpose of the
input sparse matrix

sm_array = (term*)malloc(11 * sizeof(term)); // remember you


have more than 1 space allocated then the number of non-zero elements
sm_transpose_result = (term*)malloc(11 * sizeof(term)); // remember
you have more than 1 space allocated then the number of non-zero elements

SparseMatrix_generator(sm_array, 10); // Call the SparseMatrix


generator function to randomly generate the 10x10 sparse matrix with 10
non-zero elements.
// print out the generated sparse matrix
printf("The generated sparse matrix is :\n");
printf("Index\tROW\tCOL\tValue\n");
for (i = 0; i <= 10; i++) printf("%d\t%d\t%d\t%d\n", i, sm_array[i].row,
sm_array[i].col, sm_array[i].value);

transpose(sm_array, sm_transpose_result); // Call the 1st version of the


sparse matrix transpose algorithm
// print out the result of the transpose of the input sparse matrix using 1st
version of sparse matrix transpose algorithm
printf("\nThe result of first version transpose is :\n");
printf("Index\tROW\tCOL\tValue\n");
for (i = 0; i <= 10; i++) printf("%d\t%d\t%d\t%d\n", i,
sm_transpose_result[i].row, sm_transpose_result[i].col,
sm_transpose_result[i].value);

free(sm_transpose_result);
sm_transpose_result = (term*)malloc(11 * sizeof(term));

fastTranspose(sm_array, sm_transpose_result); // Call the fast version of


the sparse matrix transpose algorithm
// print out the result of the transpose of the input sparse matrix using 1st
version of sparse matrix transpose algorithm
printf("\nThe result of fast transpose version is :\n");
printf("Index\tROW\tCOL\tValue\n");
for (i = 0; i <= 10; i++) printf("%d\t%d\t%d\t%d\n", i,
sm_transpose_result[i].row, sm_transpose_result[i].col,
sm_transpose_result[i].value);

free(sm_array);
free(sm_transpose_result);
// Have the user compare if both results are the same.
printf("Please verify if the result from two versions are the
same...<PRESS ANY KEY TO CONTINUE>\n");
_getch();

// Starting testing with N = 10, 20, 30, 40, 50, 60 70, 80, 90, 100, 200,
300, 400, 500, 600, 700, 800, 900, and 1000
printf("============= Step counts for the comparison of the two
versions of sparse matrix transpose ==============\n");
n = 10;
while (n <= 1000) {
sm_array = (term*)malloc((n + 1) * sizeof(term));
sm_transpose_result = (term*)malloc((n + 1) * sizeof(term));

SparseMatrix_generator(sm_array, n); // Randomly generate nxn


sparse matrix with n non-zero elements

step = 0;
transpose(sm_array, sm_transpose_result); // Call the 1st version of
the sparse matrix transpose algorithm
printf("n=%d => 1st_version_stepcount=%d, ", n, step); // print out
its number of steps

free(sm_transpose_result);
sm_transpose_result = (term*)malloc((n + 1) * sizeof(term));
fastTranspose(sm_array, sm_transpose_result); // Call the fast
version of the sparse matrix transpose algorithm
printf("fast_version_stepcount=%d\n", step); // print out its number
of steps
free(sm_array);
free(sm_transpose_result);

// continue next test instance.


if (n < 100) n += 10;
else n += 100;
}
三、 所遇到之問題與解決方式
請列出 3 項於本程式練習中,所碰到之程式撰寫錯誤或是警告,
並說明解決方法。
(字體:新細明體,大小:14,單行間距,左右對齊)
 問題一:

 錯誤或警告:語法錯誤 C2143
 解決方法:更改語法並注意一些細節
 問題二:
 錯誤或警告:語法錯誤 C259
 解決方法:加上括號
四、 討論與心得
請討論於練習中之學習心得,若此程式練習有題目需要回答,
請於本章節中回答。
(字體:新細明體,大小:14,單行間距,左右對齊)
雖然排列的演算法相對複雜,但由於固定的排列方式,讓
coding 的時候不用再自己去想太多,可以跟著老師的脈絡慢慢
搞懂

You might also like