2010-03-02 29 views
2

嘿那裏,我有問題顯示我的結果在這個程序,但程序編譯。任何想法是什麼問題?該程序的目的是取兩個2×2的矩陣並添加它們以創建稱爲結果的矩陣。但是當涉及在每個矩陣(A,B和結果)中顯示值時,它會掛起。爲什麼在搜索(A,0,0)時打電話?幫助顯示兩個矩陣的總和(使用鏈表)

-Thanks

#include <stdio.h> 

int gvalue; 

struct Node { 
    int row; 
    int column; 
    int value; 
    Node *next; 
}; 

void AddNode(Node *&listpointer,int r,int c,int v); 
void getValue(Node *listpointer, int grow, int gcol); 
void search(Node *listpointer, int srow, int scol); 
void display(Node *listpointer,int drow,int dcol,int dvalue); 
Node *A,*B,*result; 

int main(){ 
    A = NULL; 
    B = NULL; 
    result = NULL; 

    int row1p1,row1p2,row2p1,row2p2; //row 1 position 1 etc 

    //matrix A values! 
    printf("Enter the first row of values for matrix A: "); 
    scanf("%d %d",&row1p1,&row1p2); 
    printf("\nEnter the second row of values for matrix A: "); 
    scanf("%d %d",&row2p1,&row2p2); 

    AddNode(A,0,0,row1p1); //matrix created... 
    AddNode(A,0,1,row1p2); 
    AddNode(A,1,0,row2p1); 
    AddNode(A,1,1,row2p2); 

    //matrix B values! 
    printf("\n\nEnter the first row of values for matrix B: "); 
    scanf("%d %d",&row1p1,&row1p2); 
    printf("\nEnter the second row of values for matrix B: "); 
    scanf("%d %d",&row2p1,&row2p2); 

    AddNode(B,0,0,row1p1); //matrix created... 
    AddNode(B,0,1,row1p2); 
    AddNode(B,1,0,row2p1); 
    AddNode(B,1,1,row2p2); 


    //next part... 
    int a_row1p1,a_row1p2,a_row2p1,a_row2p2; 
    int b_row1p1,b_row1p2,b_row2p1,b_row2p2; 
    int sum; 
    //-------------------------------------------------------------------------------| 
    getValue(A,0,0);   //RESULT NODE for position 0,0 
    a_row1p1=gvalue; 
    getValue(B,0,0); 
    b_row1p1=gvalue; 

    sum=a_row1p1+b_row1p1; 
    AddNode(result,0,0,sum); 

    getValue(A,0,1);    //RESULT NODE for position 0,1 
    a_row1p2=gvalue; 
    getValue(B,0,1); 
    b_row1p2=gvalue; 

    sum=a_row1p2+b_row1p2; 
    AddNode(result,0,1,sum); 

    getValue(A,1,0);    //RESULT NODE for position 1,0 
    a_row2p1=gvalue; 
    getValue(B,1,0); 
    b_row2p1=gvalue; 

    sum=a_row2p1+b_row2p1; 
    AddNode(result,1,0,sum); 

    getValue(A,1,1);    //RESULT NODE for position 1,1 
    a_row2p2=gvalue; 
    getValue(B,1,1); 
    b_row2p2=gvalue; 

    sum=a_row2p2+b_row2p2; 
    AddNode(result,1,1,sum); 
    printf("success\n"); 
    //-------------------------------------------------------------------------------| 
    search(A,0,0); 
    printf("success\n"); //issue???? 
    search(A,0,1); 
    search(A,1,0); 
    search(A,1,1); 


    search(B,0,0); 
    search(B,0,1); 
    search(B,1,0); 
    search(B,1,1); 

    search(result,0,0); 
    search(result,0,1); 
    search(result,1,0); 
    search(result,1,1); 



    return 0; 
} 

void AddNode(Node *&listpointer,int r,int c,int v){ 
    Node *temp; 
    temp = new Node; 
    temp->row = r; 
    temp->column = c; 
    temp->value = v; 
    temp->next = listpointer; 
    listpointer = temp; 
} 

void getValue(Node *listpointer, int grow, int gcol){ 
    Node *current; 
    current = listpointer; 
    while (current != NULL){ 
     if (current == NULL){break;} 
     if ((current->row == grow) && (current->column == gcol)){ 
      gvalue = current->value; 
      break; 
     } 
     current = current->next; 
    } 
} 

void search(Node *listpointer, int srow, int scol){ 
    Node *current; 
    current = listpointer; 
    while (current != NULL){ 
     if (current == NULL){break; 
     } 
     if ((current->row == srow) && (current->column == scol)){ 
      display(current,srow,scol,current->value); //call to display 
     } 
    } 
} 

void display(Node *listpointer,int drow,int dcol,int dvalue){ 
    if (listpointer == A){ 
     printf("\n\nMatrix A\n"); 
    } 
    if (listpointer == B){ 
     printf("Matrix B\n"); 
    } 
    if (listpointer == result){ 
     printf("Matrix Result\n"); 
    } 
    //---------------------------------------------------------| 
    if ((drow == 0) && (dcol == 0)){ 
     printf("%d ",dvalue); 
    } 
    if ((drow == 0) && (dcol == 1)){ 
     printf("%d\n",dvalue); 
    } 
    if ((drow == 1) && (dcol == 0)){ 
     printf("%d ",dvalue); 
    } 
    if ((drow == 1) && (dcol == 0)){ 
     printf("%d\n\n",dvalue); 
    } 

} 
+1

您是否設法縮小到比您的整個應用程序短? – stefanB 2010-03-02 02:19:58

+0

我縮小了問題的顯示功能,我似乎無法得到任何值在這個時候打印到屏幕上 – silent 2010-03-02 02:34:20

回答

2

我假定這是功課。如果電流不符合您檢查的兩個條件中的任何一個,循環將如何結束。您需要在某個點將電流設置到列表中的下一個節點。

while (current != NULL){ 
     if (current == NULL){break; 
     } 
     if ((current->row == srow) && (current->column == scol)){ 
      display(current,srow,scol,current->value); //call to display 
     } 
    } 
+0

這只是一個條件,是檢查2次,'退出,如果電流爲NULL' ... – stefanB 2010-03-02 02:31:23

+0

ahh rite ..i在while循環中的最後一條if語句之後添加了一個「break」,但是現在沒有值正在打印在屏幕上? – silent 2010-03-02 02:33:15

+0

你認爲休息是如何完成的。您需要移動到下一個節點。 – rerun 2010-03-02 02:36:00

0

你永遠不更新current

如果當您輸入搜索時,當前不是NULL,您將永久結束while循環。

void search(Node *listpointer, int srow, int scol){ 
    Node *current; 
    current = listpointer; 
    while (current != NULL){ 
     if (current == NULL){break; 
     } 
     if ((current->row == srow) && (current->column == scol)){ 
      display(current,srow,scol,current->value); //call to display 
     } 
    } 
} 
+0

如果你通過調試程序,你會發現這一點。順便說一句,檢查'如果current == NULL然後break'是不需要的,因爲如果current爲NULL,循環將退出 – stefanB 2010-03-02 02:29:54

0

你的搜索應該像(不這樣做......我只是給通過使小改變現有代碼的工作方式的最佳途徑):

void search(Node *listpointer, int srow, int scol){ 
     Node *current; 
     current = listpointer; 
     while (current != NULL){ 
       if ((current->row == srow) && (current->column == scol)){ 
         display(current,srow,scol,current->value); //call to display 
       } 
       current = current->next; 
     } 
} 

在顯示方法中,要打印(1,0)值,而不是(1,1)

if ((drow == 1) && (dcol == 0)){ 
     printf("%d\n\n",dvalue); 
} 

應該是:

if ((drow == 1) && (dcol == 1)){ 
     printf("%d\n\n",dvalue); 
} 

如果在getValue函數中檢查,則存在冗餘。

0

鑑於

該計劃的目的是通過2點矩陣採取 二,2並將它們添加到 創建一個名爲結果矩陣。

我想知道爲什麼三個像下面這樣的二維數組是不夠的。我錯過了什麼?

#define N 2 

int a[ N ][ N ]; 
int b[ N ][ N ]; 
int result[ N ][ N ]; 

for(i = 0; i < N; ++i) 
    for(j = 0; j < N; ++j) 
    result[ i ][ j ] = a[ i ][ j ] + b[ i ][ j ];