【发布时间】:2021-02-20 08:53:09
【问题描述】:
这是我卡住的 C 语言代码。
#include<stdio.h>
int main(){
int Force_V[2], w;
int i, j, Disp_V[2];
printf("Enter Force Vector: ");
for(i=0; i<=2; i++){
scanf("%d", &Force_V[i]);
}
printf("Enter Displacement Vector: ");
for(j=0; j<=2; j++){
scanf("%d", &Disp_V[j]);
}
printf("Force vector: %di+%dj+%dk", Force_V[0],Force_V[1],Force_V[2]);
printf("\nDisplacement vector== %di+%dj+%dk", Disp_V[0],Disp_V[1],Disp_V[2]);
w= (Force_V[0]*Disp_V[0])+(Force_V[1]*Disp_V[1])+(Force_V[2]*Disp_V[2]);
printf("The work: %df", w);
return 0;
}
Force_V[2] 的输出显示 Disp_V[0] 的输出。谁能告诉我哪里出错了?
【问题讨论】:
-
您需要声明大小为 3 的数组 Force_V 和 Disp_V。
-
由于您似乎使用的是 3D 向量,因此数组的大小必须为 3,而不是 2。您的循环应该是
for (int i = 0; i < 3; i++)— 这是在三个数组上循环的惯用形式元素。