在C中保留和命名用户输入的变量

时间:2016-03-09 20:41:52

标签: c math

我正在尝试使用多个X和Y值计算渐变。我的问题是,在我的代码取X和Y值的平均值之后,我无法重复使用用户输入的X和Y值来进一步计算渐变,因为我不知道如何命名它们。我在下面包含了我的代码,我认为我必须使用数组或全局变量来执行此任务。

提前感谢您,我的编码知识有限。

#include <stdio.h>

int main () {
int n, i;
float num[1000], total=0, mean;

printf("Enter the amount of x-values:");
scanf("%d", &n);
while (n <= 0 || n > 1000) {
    printf("Print error. The number should in range of 0 to 1000.\n");
    printf("Please try to enter the amount again: ");
    scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
    printf("%d. Input x-value:", i+1);
    scanf("%f", &num[i]);
    total += num[i];
}
mean=total/n;
printf("The mean of all the x-values entered is %.2f to 2 decimal places", mean);

{
    float num[1000], total=0, mean;

    printf("Enter the amount of y-values:");
    scanf("%d", &n);
    while (n <= 0 || n > 1000) {
        printf("Print error. The number should in range of 0 to 1000.\n");
        printf("Please try to enter the amount again: ");
        scanf("%d",&n);
    }
    for (i = 0; i < n; ++i) {
        printf("%d. Input y-value:", i+1);
        scanf("%f", &num[i]);
        total += num[i];
    }
    mean = total / n;
    printf("The mean of all the y-values entered is %.2f to 2 decimal places", mean);

    return 0;
}
}

0 个答案:

没有答案