这段代码要求用户填写to matrix,然后调用一个void函数将它们相加。我有一个www.ideone.com
我也不能改变很多代码。要求所有这些define语句和一个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;
        }
    }
}

最佳答案

编辑:更好的解决方案是:

#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-从double arrsum[NCOL3][NROW3];中删除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-制造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;
                }
        }
}

关于c - 如何使用void函数操作两个数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10323848/

10-13 01:12