使用具有给定顶点的数组查找三角形的区域,以供用户提示

时间:2018-10-09 04:06:14

标签: c

我正在尝试解决此问题,但是由于某种原因,它无法读取我的值和输出中的区域。当我运行它时,给我零。 我将不胜感激。

这是输出:

Please enter value of your three vertex x1 y1 x2 y2 x3 y3:
1
2
5
6
8
9
with given vertices 0.00, 0.00, 0.00, 0.00, 0.00, and 0.00, the area is 0.00
--------------------------------
Process exited with return value 0
Press any key to continue . . .

这是我到目前为止对代码所做的事情。

谢谢!

#include<stdio.h>
#include<math.h>
int main(){

float arr[6];
int i;
float side1, side2, side3, sidet, area, x1, y1, x2, y2, x3, y3;

x1=arr[0];
y1=arr[1];
x2=arr[2];
y2=arr[3];
x3=arr[4];
y3=arr[5];

printf("Please enter value of your three vertex x1 y1 x2 y2 x3 y3: \n");
for(i=0; i<6; i++){
scanf("%f", &arr[i]);
}

//calculation
side1=pow(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)), 0.5);

side2=pow(((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)), 0.5);

side3=pow(((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)), 0.5);

//calculations

sidet=(side1+side2+side3)*0.5;

area=pow((sidet*(sidet-side1)*(sidet-side2)*(sidet-side3)), 0.5);

printf("with given vertices %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, and %0.2f, the area is %0.2f",x1,y1,x2,y2,x3,y3,area);



return 0;
}

2 个答案:

答案 0 :(得分:0)

JSchema schema = JSchema.Parse(schemaJson); 未建立关系。它会在此时进行分配,而不是基于后来的x1=arr[0];值。

在代码中先移动以下内容:

arr[0]

然后分配

printf("Please enter value of your three vertex x1 y1 x2 y2 x3 y3: \n");
for(i=0; i<6; i++) {
  scanf("%f", &arr[i]);
}

答案 1 :(得分:0)

这是完整的工作代码:

#include<stdio.h>
#include<math.h>
int main(){

    float arr[6];
    int i;
    float side1, side2, side3, sidet, area, x1, y1, x2, y2, x3, y3;

    printf("Please enter value of your three vertex x1 y1 x2 y2 x3 y3: \n");
    for(i=0; i<6; i++){
            scanf("%f", &arr[i]);
    }

    x1=arr[0];
    y1=arr[1];
    x2=arr[2];
    y2=arr[3];
    x3=arr[4];
    y3=arr[5];


    //calculation
    side1=pow(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)), 0.5);

    side2=pow(((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)), 0.5);

    side3=pow(((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)), 0.5);

    //calculations

    sidet=(side1+side2+side3)*0.5;

    area=pow((sidet*(sidet-side1)*(sidet-side2)*(sidet-side3)), 0.5);
    printf("with given vertices %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, and %0.2f, the area is %0.2f",x1,y1,x2,y2,x3,y3,area);



    return 0;
    }