【问题标题】:How to involve array into a function definition in C如何将数组涉及到 C 中的函数定义中
【发布时间】:2014-07-30 16:51:24
【问题描述】:

我尝试定义一个函数来计算两个原子之间的距离。我在主函数中定义了一个二维数组来存储 m 个原子的坐标,例如nn[m][3],m是原子的索引,3代表x

但是当我定义函数时——> float dist() { },我的 Dev C++ 编译器说 'nn' 未声明(首先使用此函数)(每个未声明的标识符仅针对它出现的每个函数报告一次。)

以下是我定义函数 dist() 的代码,错误出现在 x1=nn[a][0] 行 谢谢你,最好的

float dist(int a, int b){  // a,b are parameters of function dist(), a is one order of 
//a atom, b is another, e.g. nn[a][x,y,z], and nn[b][x,y,z]
float d;
float f1(float);// f1 is a fn to calculate the square of a number
float x1,x2,y1,y2,z1,z3;
x1=nn[a][0];
x2=nn[b][0];
y1=nn[a][1];
y2=nn[b][1];
z1=nn[a][2];
z2=nn[b][2];
float nn[a][0],nn[a][1],nn[a][2],nn[b][0],nn[b][1],nn[b][2];
d=sqrt(f1(x1-x2)+f1(y1-y2)+f1(z1-z2));
return d;
}

这是我现在拥有的:

// build an array for 4 Molecules, with initial radius between 2 nearest molecules
// number of 2 nearest number is n-1
// number of not nearest neighbor
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float f1(float p){ //calculat the square
    float k;
    k=p*p;
    return k;
    }
float dist(int a, int b){  // a,b are parameters of function dist(), a is one order of 
//a atom, b is another, e.g. nn[a][x,y,z], and nn[b][x,y,z]
float d;
float f1(float);// f1 is a fn to calculate the square of a number
float x1,x2,y1,y2,z1,z3;
x1=nn[a][0];
x2=nn[b][0];
y1=nn[a][1];
y2=nn[b][1];
z1=nn[a][2];
z2=nn[b][2];
float nn[a][0],nn[a][1],nn[a][2],nn[b][0],nn[b][1],nn[b][2];
d=sqrt(f1(x1-x2)+f1(y1-y2)+f1(z1-z2));
return d;
}

int main (void) 
{
    int n=4;// # of molecules 
    int i=0,j=0,k=0;rj,rk;//i,j,k are dummy variables, rj, rk are the the size of array rn[],rd[]
    rj=(n-1)-1;//the # of nearest neighbor of 4 molecules, rj=(n-1)-1,-1 for c starts at 0
    rk=n*(n-1)/2-(n-1)-1;//the # of non-nearest neighbor of 4 molecules, for n molecules, it will be 


float rn[rj],rd[rk];// nn is the number array of the atoms
// nn[][] is the coordinates of the atom, n[][0]<-x,n[][1]<-y,n[][2]<-z,
// rn[] is the distance between nearest neighbor
// rk[] is the distance between non-nearest neighbors
/* give intiate coordinate */
int nn[4][3]={{0,0,0},{1,0,0},{2,0,0},{3,0,0}};

  for(j=0;j<n;j++)
    {
            {printf("\n");
        for(i=0;i<3;i++)

        printf("nn[%d][%d] is %d   ",j,i,nn[j][i]);
 }
printf("\n");
}
printf("\n");

float d,dd;
printf("%d,  \n%d  \n",nn[1][0],nn[2][0]);
d=sqrt(f1(nn[1][0]-nn[2][0])+f1(nn[1][1]-nn[2][1])+f1(nn[1][2]-nn[2][2])); //calculate the distance   
dd=dist(1,2); // test the function
printf("%f\n%f\n",d,dd);

 system("pause");
 return 0;
}

【问题讨论】:

  • 这个问题太乱了,无法正确回答。一般来说,如果您在函数main 中声明了数组nn,那么您应该在调用它时将它传递给函数dist。但同样,我看到这个数组也被声明在这个函数的底部,那么你到底打算在那里做什么?另外,函数定义float f1(float) 在函数dist 内部做什么?这里显然缺少一些基本的编码规则。
  • 嗨巴拉克,我在“main()”函数中定义了数组“nn[m][3]”,我不知道如何将数组传递给函数“dist”。我认为这是我的问题。函数 float f1(float) 将计算数字的平方。例如。 f1(3)=9。我是一个愿意学习的新程序员,请给我一些建议
  • 添加整个代码可能会有所帮助...
  • 添加了我现在拥有的,谢谢
  • 看看我问的这个问题,我想给自己 4 年前的时间,对于谁是我的鞋子,一些建议:1)虽然你用 c 编码

标签: c arrays function


【解决方案1】:

改变

float dist(int a, int b){

float dist(int a, int b, int nn[4][3]){ 

改变

dd=dist(1,2);

dd=dist(1,2, nn);

改变

int i=0,j=0,k=0;rj,rk;

int i=0,j=0,k=0,rj,rk;

改变

float nn[a][0],nn[a][1],nn[a][2],nn[b][0],nn[b][1],nn[b][2];

//float nn[a][0],nn[a][1],nn[a][2],nn[b][0],nn[b][1],nn[b][2];

改变

float x1,x2,y1,y2,z1,z3;

float x1,x2,y1,y2,z1,z2;

【讨论】:

  • 谢谢Bluepixy,它有效!我不明白的一件事是行 y'float dist(int a,int b, int nn[4][3])' 为什么你对数组 nn[4][3] 使用 int 类型而不是 float 类型?
  • 嗨,bluepixy,我现在明白了,我在 dist() 和 main() 中定义的 nn[][] 必须一致,我认为这就是原因。再次感谢您并致以最良好的祝愿
  • 其实这是一个愚蠢的问题。如果我们想使用双精度数据,我们需要使用 %lf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 2021-11-18
  • 2020-04-14
  • 1970-01-01
  • 2013-06-28
相关资源
最近更新 更多