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

时间:2018-11-17 01:08:03

标签: c loops switch-statement

我要尝试做的事情: 我正在尝试做一个程序,以分析并提供有关任何给定数字集的一些相关信息。

为了使它不仅对我的研究有用,我正在编写它来生成任意给定数量的元素的随机数组。之后,我计划放置该选项以处理带有一组数字的文件。

我的问题是什么 我遇到的问题是,当我被要求重复,修改数组大小或退出程序时,它仅在第二个输入上做出相应的响应,而我不知道这种行为的原因是什么。

代码如下:

#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;
    }
  }
}

2 个答案:

答案 0 :(得分:0)

我不知道这种情况是否还会继续发生像DV()这样的两次@Dmitri通话。但这当然是我可以对@Neil Edelman所说的内容做出的另一种解释。

当我取出DV()函数的接口解释并将提示和决策代码放在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;
    }
  }
}

答案 1 :(得分:0)

使用C99的新功能float x[n]将数组存储在堆栈上时,请注意n较大时的堆栈溢出。参见How to declare and use huge arrays of 1 billion integers in C?https://wiki.sei.cmu.edu/confluence/display/c/MEM05-C.+Avoid+large+stack+allocations

如果RAND_MAX为2,147,483,647,则x = rand()返回[准]均匀分布E[x] = 1,073,741,823。假设IEEE 754是32位float,则只能准确存储16,777,217个整数,请参见Which is the first integer that an IEEE 754 float is incapable of representing exactly?。当添加n个副本时,精度下降为1/n。当n大时,这也很重要。在物理实验室中,我们总是被提醒,使用double来操纵测量几乎总是合适的,这就是为什么它很重要的原因(对于时间而言,但通常也是)https://randomascii.wordpress.com/2012/02/13/dont-store-that-in-a-float/

此代码是Welford在线算法的实现,https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford%27s_Online_algorithm(用C语言除外)。这样做的优点是您不需要知道元素的数量,也不必存储元素,(处于在线状态,内存为O(1));而且,从数值上讲,它比增加可能很大的和更稳定。

#include <stdlib.h> /* EXIT_ size_t */
#include <stdio.h>  /* printf */
#include <math.h>   /* sqrt */

/** Measurement. C version of Python
 \url{ https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_Online_algorithm }. */
struct Mx {
    size_t count;
    double mean, ssdm;
};

static void mx_reset(struct Mx *const measure) {
    if(!measure) return;
    measure->count = 0;
    measure->mean  = 0;
    measure->ssdm  = 0;
}

static void mx_add(struct Mx *const measure, const double replica) {
    size_t n;
    double delta;
    if(!measure) return;
    n     = ++measure->count;
    delta = replica - measure->mean;
    measure->mean += delta / n;
    measure->ssdm += delta * (replica - measure->mean);
}

static double mx_mean(const struct Mx *const measure) {
    if(!measure || !measure->count) return NAN;
    return measure->mean;
}

static double mx_sample_variance(const struct Mx *const measure) {
    if(!measure || measure->count <= 1) return NAN;
    return measure->ssdm / (measure->count - 1);
}

static double mx_population_variance(const struct Mx *const measure) {
    if(!measure || !measure->count) return NAN;
    return measure->ssdm / measure->count;
}

/** This is the example from
 \url{ https://en.wikipedia.org/wiki/Standard_deviation }. */
int main(void) {
    const float fulmars_f[] = { 727.7f, 1086.5f, 1091.0f, 1361.3f, 1490.5f,
        1956.1f }, fulmars_m[] = { 525.8f, 605.7f, 843.3f, 1195.5f, 1945.6f,
        2135.6f, 2308.7f, 2950.0f };
    const size_t fulmars_f_size = sizeof fulmars_f / sizeof *fulmars_f,
        fulmars_m_size = sizeof fulmars_m / sizeof *fulmars_m;
    struct Mx f, m;
    size_t i;
    mx_reset(&f), mx_reset(&m);
    /* Converts float -> double. */
    for(i = 0; i < fulmars_f_size; i++) mx_add(&f, fulmars_f[i]);
    for(i = 0; i < fulmars_m_size; i++) mx_add(&m, fulmars_m[i]);
    printf("female breeding Northern fulmars\nmean:\t%f.\nstddev:\t%f\n"
        "population stddev: %f\n\nmale breeding Northern fulmars\n"
        "mean:\t%f.\nstddev:\t%f\npopulation stddev: %f\n", mx_mean(&f),
        sqrt(mx_sample_variance(&f)), sqrt(mx_population_variance(&f)),
        mx_mean(&m), sqrt(mx_sample_variance(&m)),
        sqrt(mx_population_variance(&m)));
    return EXIT_SUCCESS;
}

在这种情况下,用户界面是一个常数,但是对某些估算的for(i = 0; i < n; i++) mx_add(rand() % n);进行n会将用户界面与统计计算分开。