首页 > 解决方案 > 数组在 C 中显示不同的输出

问题描述

这是我卡住的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] 的输出。谁能告诉我错误在哪里?

标签: arrayscinputoutput

解决方案


该数组int Force_V[2]有两个单元格,因此您必须像这样更改循环条件:

for(i=0; i<2; i++)

推荐阅读