gpt4 book ai didi

C - 为什么只有第二个命令有效?

转载 作者:太空宇宙 更新时间:2023-11-04 04:14:47 25 4
gpt4 key购买 nike

我想做什么:我正在尝试编写一个程序来分析并提供有关任何给定数字集的一些相关信息。

为了使其不仅对我的研究有用,我编写它来生成任意给定数量的元素的随机数组。之后,我打算为其添加选项以处理包含一组数字的文件。

我的问题是什么:把我带到这里的问题是,当我被要求重复、修改数组大小或退出程序时,它只会对第二个输入做出相应的响应,我不知道这种行为的原因是什么。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

/*This program creates an array, calculates its standard deviation and identify its large element.*/
int DV(int n){ /*DV -> Compute the standard deviation, identify the large element and its respective position*/
int i, l, p = 0;
float x[n], MVx = 0, j = 0, k = 0;
char rst;
printf("Be a random array with %d elements, created from 0 to %d: \n", n, n-1);
for(i = 0; i < n; i++){ //Generates an array with n random elements
x[i] = rand() % n;
MVx += x[i];
printf("%.0f ", x[i]);
}
MVx /= n; // Here MV assumes the Arithmetic mean
printf("\nThe arithmetic mean is: %.5f\n", MVx);
for (i = 0; i < n; i++) {
j = x[i] - MVx;
j *= j;
k += j;
}
printf("The standard deviation is %.5f\n\n", sqrt(k/n));
j = 0; // j needs to be reseted
for(i = 0, l = i + 1; i < n - 1, l < n; i++, l++){ //Compares all elements and puts its largest value on j
switch (x[i] > x[l]){
case 1: if(j < x[i]){ j = x[i]; p = i;} break;
case 0: if(j < x[l]){ j = x[l]; p = l;} break;
}
}
printf("The largest value on sample is: %.2f\nIt occurs for the first time on the %dº element\n", j, p+1);
printf("\n\nRepeat? (y/n)\nA/a to change array size: ");
while((getchar()) != '\n');
rst = getchar();
switch (rst) {
case 'N':
case 'n': return 1; break;
case 'A':
case 'a': return 2; printf("\n\n"); break;
case 'Y':
case 'y': return 3; printf("\n\n"); break;
default: return -100;
}
}

int main(void){
int n;
char rst = 'a', qtd = 'a';
while(rst == 'a') {
if(qtd == 'a'){
printf("Insert the amount of elements to be computed: ");
scanf("%d", &n);
}
srand(time(NULL)); //Generates randomic seed
DV(n);
switch(DV(n)){
case 3: printf("\n\n"); rst = 'a'; qtd = 'n'; break;
case 2: printf("\n\n"); rst = qtd = 'a'; break;
case 1: printf("\n\n"); return 0; break;
default: printf("Invalid entry\n\n"); return 0;
}
}
}

最佳答案

我不知道这是否会像 @Dmitri 所说的那样发生两次 DV() 调用的可能性。但这肯定是我可以对 @Neil Edelman 所说的另一种解释。

当我取出DV() 函数的接口(interface)解释并将提示和决策制定代码放在main() 函数中时,程序就像假装一样工作。

我将不得不对使用 Welford 的在线算法发表评论...因为它可能对大量元素表现得很奇怪我会保留我的代码,一旦它对一定数量的元素工作正常直到有点在 2,090,000.00 和 2,100,000.00 之间。而且我没有使用双...

现在可以运行的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

/*This program creates an array, calculates its standard deviation and identify its large element.*/
int DV(int n){ /*DV -> Compute the standard deviation, identify the large element and its respective position*/
int i, l, p = 0;
float x[n], MVx = 0, j = 0, k = 0;
char rst;
printf("Be a random array with %d elements, created from 0 to %d: \n", n, n-1);
for(i = 0; i < n; i++){ //Generates an array with n random elements
x[i] = rand() % n;
MVx += x[i];
printf("%.0f ", x[i]);
}
MVx /= n; // Here MV assumes the Arithmetic mean
printf("\nThe Arithmetic Mean is: %.5f\n", MVx);
for (i = 0; i < n; i++) {
j = x[i] - MVx;
j *= j;
k += j;
}
printf("The standard deviation is: %.5f\n\n", sqrt(k/n));
j = 0; // j needs to be reseted
for(i = 0, l = i + 1; i < n - 1, l < n; i++, l++){ //Compares all elements and puts its value on j
switch (x[i] > x[l]){
case 1: if(j < x[i]){ j = x[i]; p = i;} break;
case 0: if(j < x[l]){ j = x[l]; p = l;} break;
}
}
printf("The largest value on sample is: %.2f\nIt occurs for the first time on the %dº element\n", j, p+1);
}

int main(void){
int n;
char rst = 'a', qtd = 'a';
while(rst == 'a') {
if(qtd == 'a'){
printf("Insert the amount of elements to be computed: ");
scanf("%d", &n);
}
srand(time(NULL)); //Generates randomic seed
DV(n);
printf("\n\nRepeat? (y/n)\nA/a to change array size: ");
while((getchar()) != '\n');
rst = getchar();
switch (rst) {
case 'N':
case 'n': return 0; break;
case 'A':
case 'a': printf("\n\n"); rst = qtd = 'a'; break;
case 'Y':
case 'y': printf("\n\n"); rst = 'a'; qtd = 'n'; break;
default: printf("Invalid Entry\n\n"); return 0;
}
}
}

关于C - 为什么只有第二个命令有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53347276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com