2012-04-25 22 views
1

此代碼要求用戶填寫矩陣,然後調用void函數將它們添加到一起。我有一個www.ideone.com 我也不能改變很多代碼。需要具有所有這些定義語句和無效函數。如何使用void函數操作兩個數組

#include <stdio.h> 
#include <math.h> 

#define NCOL1 2 
#define NCOL2 2 
#define NROW1 2 
#define NROW2 2 
#define NCOL3 2 
#define NROW3 2 

int main (void) 
{ 
    //Initiate variables 
    double a, b; 
    int i, j; 
    void addarray(double a, double b); 
    double ans; 
    double arr1[NCOL1][NROW1], arr2[NCOL2][NROW1]; 

    //Ask user to enter numbers for the first matrix 
    printf("Please enter numbers for Matrix 1 :\n "); 
    for (i = 0; i < NCOL1; i++) { 
     for (j = 0; j < NROW1; j++) { 
      scanf("%lf", &arr1[i][j]); 
     } 
    } 

    //Ask user to enter numbers for the second matrix 
    printf("Please enter numbers for Matrix 2 :\n "); 
    for (i = 0; i < NCOL2; i++) { 
     for (j = 0; j < NROW2; j++) { 
      scanf("%lf", &arr2[i][j]); 
     } 
    } 

    //Iterate through void function and print out result 
    for (i = 0; i < NCOL3; i++) { 
     for (j = 0; j < NROW3; j++) { 
      addarray(arr1[i][j], arr2[i][j]); 
      printf("%lf", ans); 
     } 
    } 
    return 0; 
} 

void addarray (double a, double b) 
{ 
    int i,j; 
    double arrsum[NCOL3][NROW3]; 
    for (i = 0; i < NCOL3; i++) { 
     for (j = 0; j < NROW3; j++) { 
      arrsum[i][j] = a + b; 
     } 
    } 
} 
+1

我不清楚你的問題是什麼。你能澄清一下嗎? – 2012-04-25 21:12:21

+0

考慮將數組傳遞給void函數的引用。有輸出數組的第三個參考。 – mert 2012-04-25 21:12:37

+0

如果函數必須是'void',那麼您必須傳入另一個參數或將第一個參數添加到第二個參數中。你的函數也應該是數組,而不是標量值。 – 2012-04-25 21:12:38

回答

0

編輯:更好的解決問題的方法是:

#include<stdio.h> 
#include<math.h> 
#define NCOL1 2 
#define NCOL2 2 
#define NROW1 2 
#define NROW2 2 
#define NCOL3 2 
#define NROW3 2 


void addarray(double a[NROW1][NCOL1], double b[NROW1][NCOL1], double (*c)[NCOL1]); 


int main(void) 
     { 
    //Initiate variables 
    double a,b; 
    int i,j; 
    double ans; 
    double arr1[NCOL1][NROW1], arr2[NCOL2][NROW1]; 
    double arrsum[NCOL1][NROW1]; 

    //Ask user to enter numbers for the first matrix 
    printf("Please enter numbers for Matrix 1 :\n "); 
    for(i=0;i<NCOL1;i++){ 
     for(j=0;j<NROW1;j++){ 
     scanf("%lf",&arr1[i][j]); 
     } 
    } 
    //Ask user to enter numbers for the second matrix 
    printf("Please enter numbers for Matrix 2 :\n "); 
    for(i=0;i<NCOL2;i++){ 
     for(j=0;j<NROW2;j++){ 
     scanf("%lf",&arr2[i][j]); 
     } 
    } 


    addarray(arr1, arr2, &arrsum[0]); 

    printf("Output of added arrays\n"); 
    for(i=0;i<NCOL3;i++){ 
      for(j=0;j<NROW3;j++){ 
        printf("%lf ", arrsum[i][j]); 
      } 
     printf("\n"); 
    } 


    return 0; 
} 

void addarray(double a[NROW1][NCOL1], double b[NROW1][NCOL1], double (*c)[NCOL1]) 
{ 
    int i,j; 

    for(i=0;i<NCOL3;i++) 
    { 
     for(j=0;j<NROW3;j++) 
     { 
      c[i][j] = a[i][j] + b[i][j]; 
     } 
    } 
} 

但是,以最小的變化,下面也會起作用。

它看起來像addarray()將它的結果放到名爲arrsum [] []的局部矩陣中。爲了讓這個程序起作用,你很可能會想讓arrsum [] []可用於程序的其餘部分(儘管全局數組並不是一個好主意)。

未經測試或編譯的代碼,你應該讓這些變化至少有:

1-從addarray()

void addarray(double a,double b) 
{ 
     int i,j; 

     for(i=0;i<NCOL3;i++){ 
       for(j=0;j<NROW3;j++){ 
         arrsum[i][j]=a+b; 
       } 
} 

2-刪除double arrsum[NCOL3][NROW3];arrsum[][]全球

#include<stdio.h> 
#include<math.h> 
#define NCOL1 2 
#define NCOL2 2 
#define NROW1 2 
#define NROW2 2 
#define NCOL3 2 
#define NROW3 2 

/**** add declaration of arrsum[][] here ****/ 
double arrsum[NCOL3][NROW3]; 

我已經去掉並重寫了程序的一部分,編譯並測試了它。新代碼如下。

#include<stdio.h> 
#include<math.h> 
#define NCOL1 2 
#define NCOL2 2 
#define NROW1 2 
#define NROW2 2 
#define NCOL3 2 
#define NROW3 2 


void addarray(double a,double b); 


double arrsum[NCOL3][NROW3]; 

int main(void) 
     { 
    //Initiate variables 
    double a,b; 
    int i,j; 
    double ans; 
    double arr1[NCOL1][NROW1], arr2[NCOL2][NROW1]; 
    //Ask user to enter numbers for the first matrix 
    printf("Please enter numbers for Matrix 1 :\n "); 
    for(i=0;i<NCOL1;i++){ 
     for(j=0;j<NROW1;j++){ 
     scanf("%lf",&arr1[i][j]); 
     } 
    } 
    //Ask user to enter numbers for the second matrix 
    printf("Please enter numbers for Matrix 2 :\n "); 
    for(i=0;i<NCOL2;i++){ 
     for(j=0;j<NROW2;j++){ 
     scanf("%lf",&arr2[i][j]); 
     } 
    } 
    //Iterate through void function and print out result 
    for(i=0;i<NCOL3;i++){ 
     for(j=0;j<NROW3;j++){ 
     addarray(arr1[i][j],arr2[i][j]); 
//  printf("%lf",ans); 
     } 
    } 

    printf("Output of added arrays\n"); 
    for(i=0;i<NCOL3;i++){ 
      for(j=0;j<NROW3;j++){ 
        printf("%lf ", arrsum[i][j]); 
      } 
     printf("\n"); 
    } 


    return 0; 
} 

void addarray(double a,double b) 
{ 
     int i,j; 

     for(i=0;i<NCOL3;i++){ 
       for(j=0;j<NROW3;j++){ 
         arrsum[i][j]=a+b; 
       } 
     } 
} 
+1

您不能使用像'double a1 [] []''這樣的參數類型將數組傳遞給函數。您只能省略數組中第一個(最左邊)級別的維度。否則,編譯器將不知道如何進行數組運算。類似地,類型'double **'的第三個參數與二維數組(指向一個指針數組的指針,可能不是二維數組)不是一回事。 – bta 2012-04-25 21:52:24

+0

是的,我想通了,刪除了我的部分迴應。謝謝。 – Chimera 2012-04-25 22:15:20

1

你的問題是?感謝您提供代碼,但目前尚不清楚您正在嘗試做什麼或代碼目前做錯了什麼。請更新您的問題,提供更多關於您想要完成的內容以及您需要幫助的具體信息。

一般建議:如果你想使用函數來操作數組,那麼函數需要把一個指向數組的指針作爲參數。

此外,您的addarray()函數僅修改數組arrsum,該數組位於該函數的範圍內。由於該函數在初始化元素之後從未對arrsum執行任何操作,因此整個函數本質上是空操作。