前提
model-tree.exeの接続行列データ出力をパイプで受け取り、接続行列を出力し、引数のノードから接続しているノードをプリントしたい。
また、ファイル名をmatrix.c とすると、gcc -o matrix matrix.c でコンパイル
実現したいこと
そのためのmatrix.cのプログラムに足りていない部分(matrix.cの/*以下を作成せよ*/)に適切なプログラムを挿入したい。
試したこと
このように作ってみたが分割コンパイルによって実行したが何も表示されなかった
void printAdj() { int i, j; for (i = 0; i < nmax; i++) { for (j = 0; j < nmax; j++) { if (j > 1) { adj[i][j]; } } } }
実行例
./model-tree 2 3 | ./matrix 4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 . 1 1 . . . . . . . . . . . .
1 . . . 1 1 . . . . . . . . . .
2 . . . . . 1 1 . . . . . . . .
3 . . . . . . . 1 1 . . . . . .
4 . . . . . . . . . 1 1 . . . .
5 . . . . . . . . . . . 1 1 . .
6 . . . . . . . . . . . . . 1 1
7 . . . . . . . . . . . . . . .
8 . . . . . . . . . . . . . . .
9 . . . . . . . . . . . . . . .
10 . . . . . . . . . . . . . . .
11 . . . . . . . . . . . . . . .
12 . . . . . . . . . . . . . . .
13 . . . . . . . . . . . . . . .
14 . . . . . . . . . . . . . . .
4: 9 10
4からは 9と10 に接続
該当のソースコード
コード model-tree.c #include <stdio.h> #include <stdlib.h> /************************************************************ * State ************************************************************/ typedef int stid; /************************************************************ * Model Tree ************************************************************/ int BF; // Branching Factor int MD; // Max Depth int NOCH; // treeSize(D) // size of a model tree with branching factor BF and depth D // \sum_{j=0}^D B^j ... if j >= 0 int treeSize(int d){ if(d < 0) return 0; if(d == 0) return 1; else return 1 + BF * treeSize(d-1); } // treeDepth(N) // depth of the node N in a model tree with branching factor BF // (root = 0) int treeDepth(stid n){ if(n == 0) return 0; else return 1 + treeDepth((n - 1) / BF); } // firstChild(N) // the first child of the node N in a model tree with branching factor BF // (root = 0, NOCH = all 1) stid firstChild(stid st){ int d; d = treeDepth(st); if(d == MD) return NOCH; else return treeSize(d) + BF * (st - treeSize(d - 1)); } /********************************************************************** * Main **********************************************************************/ int main(int argc, char *argv[]){ int tsize; int i, j, fc; if( argc != 3 ){ exit(0); } BF = atoi(argv[1]); MD = atoi(argv[2]); NOCH = ~(stid) 0; printf("# BF: %d, MD: %d\n", BF, MD); tsize = treeSize(MD); printf("# Tree Size %d\n", tsize); for(i = 0; i < tsize; i++){ fc = firstChild(i); /* printf("%d: ", i); if(fc != NOCH) for(j = 0; j < BF; j++) printf(" %d", fc + j); printf("\n"); */ if(fc != NOCH) for(j = 0; j < BF; j++) printf("%d %d\n", i, fc + j); } } matrix.c #include <stdio.h> #include <stdlib.h> /* 格納可能なノード最大値 */ #define NMAX 0xFFF /* プロトタイプ */ void initAdj(void); /* adjacency 隣接 */ void printAdj(void); int adj[NMAX][NMAX]; /* 確保。かなり大きい */ int nmax; /* 実際のモデル木の最大ノード */ /* 接続行列の初期化 */ void initAdj() { int i,j; int bf, md; /* bとmの値 */ /* 最初の2行の制御用情報から、読み込み */ scanf("# BF: %d, MD: %d\n", &bf, &md); scanf("# Tree Size %d\n", &nmax); /* 使用する部分に0をセット */ for (i = 0; i < nmax; i++) for (j = 0; j < nmax; j++) adj[i][j]=0; /* 3行目から、2つずつのデータを読み込む。最後はEOF。例題0101では、control z(UNIXはcontrol d) */ while (scanf("%d %d", &i, &j)!=EOF) { if(i < nmax && j < nmax){ adj[i][j]=1; } } } void printAdj(){ int i,j; /* 以下を作成せよ */ } int main(int argc, char *argv[]){ int start, j; if (argc != 2){ /* コマンドと引数で2つなければ、すぐに終了 */ exit(0); } start = atoi(argv[1]); /* 引数の文字列を整数に変換して、start変数に代入 */ initAdj(); /* 初期化せよ。標準入力からデータを読み込み、セットせよ */ printAdj(); /* 接続行列をプリント */ printf("%d:", start); /* startノードに隣接しているノードを出力せよ */ return 0; }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。