DSDFSDC
DSDFSDC
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
sm[0].row = n;
sm[0].col = n;
sm[0].value = n;
srand((unsigned)time(NULL));
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++;
}
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
free(sm_transpose_result);
sm_transpose_result = (term*)malloc(11 * sizeof(term));
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));
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);
錯誤或警告:語法錯誤 C2143
解決方法:更改語法並注意一些細節
問題二:
錯誤或警告:語法錯誤 C259
解決方法:加上括號
四、 討論與心得
請討論於練習中之學習心得,若此程式練習有題目需要回答,
請於本章節中回答。
(字體:新細明體,大小:14,單行間距,左右對齊)
雖然排列的演算法相對複雜,但由於固定的排列方式,讓
coding 的時候不用再自己去想太多,可以跟著老師的脈絡慢慢
搞懂