【问题标题】:Is it possible to have a for loop run through multiple arrays/variables in C programming?是否可以在 C 编程中通过多个数组/变量运行 for 循环?
【发布时间】:2014-03-09 22:39:21
【问题描述】:

我有一个程序,它从 25 个人中抽取 20 个样本,然后必须将该样本中的每个数字与一个百分比相关联。

有没有办法通过一个函数创建一个 for 循环,该循环将在我手动执行的情况下运行所有​​示例?

例如。

int sample1[20]={1,2...20};
...
int sample25[20]={1,7..97};

我有一个 for 循环遍历其中一个数组并将其与包含 500 个数字的更大数组相关联。我需要知道是否有一种方法可以运行 sample1,然后是 2,然后是 3,等等。无需我手动进入函数并放入一个新数组。

或者您可以将数组发送给函数吗?

    #include <stdio.h>

int main()
{
int x;
float valid,mean,total=0;


for (x = 0;x < 20; x++)
{
    float percent[500]={4.268,  4.014,  3.905,  3.853,  3.765,  3.949,  3.832..etc};
    int sample1[20]={66,20,221,321,...};


    sample1[x];
    valid=percent[sample1[x]-1];

    printf("\n%d = %.3f",sample1[x],percent[sample1[x]-1]);

    total= total+ percent[sample1[x]-1];
}
    mean= total/20;
    printf("\n\nThe mean percentage for the sample is %.3f",mean);

return 0;
}

【问题讨论】:

  • 是的,有。通过创建一个新的数组(或向量)数组。这正是容器的用途,不要像这样创建多个变量。
  • 这里可以使用模板魔法,但我认为你不会满意 :)
  • 很容易,如果我只知道背后的逻辑......例如,为什么sample25[1] == 7sample25[19] == 97

标签: c arrays logic


【解决方案1】:

使用一个二维数组,即int sample[25][20],并使用两个嵌套的for来处理整个。您当然可以将这样的数组传递给函数。查看一个好的 C 教程,例如 ones by Steve Summit。是的,它们已经很老了,但仍然很重要。更现代的方法是here

【讨论】:

  • 酷。但我必须在每个样本后计算一个平均百分比。我怎么能把它结合起来。就像在每个样本或数组 [20] 之后,计算并显示平均值
  • 就像你为一个人做的那样做,比如sample15[]
  • 我把我的代码贴在了上面。也许这对我想做的事情更有意义
【解决方案2】:

除非我错过了重点(完全有可能),否则看起来很简单。怎么样:

for(int i = 0; i < people; i++)
{
    // Set person to work on
    for(int j = 0; j < sample; j++)
    {
        //process numbers
    }
}

或者你可以改变循环,让外面是样本,里面是人。

【讨论】:

    【解决方案3】:

    同意海报说这可以用一个数组来处理。请原谅随机数据生成位:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define DATA_SIZE  500
    #define NUM_PEOPLE  25
    #define NUM_SAMPLES 20
    
    float random_samples[DATA_SIZE] = {};
    
    void create_random_samples();
    float calculate_average( float *arr, int size );
    float random_i_to_j( float i, float j );
    
    int main()
    {
        float sample_average[NUM_SAMPLES] = {};
    
        int i, j;
        for ( i = 0; i < NUM_PEOPLE; i++ ) {
            for ( j = 0; j < NUM_SAMPLES; j++ ) {
                create_random_samples();
                sample_average[j] = calculate_average( random_samples, DATA_SIZE );
            }
        }
    
        for ( i = 0; i < NUM_PEOPLE; i++ ) {
            for ( j = 0; j < NUM_SAMPLES; j++ ) {
               printf( " person: %d sample: %d sample average: %.2f \n", i, j, sample_average[j] );
            }
        }
        return 0;  
    }
    
    void create_random_samples( int num )
    {
        int i;
        for ( i = 0; i < DATA_SIZE; i++ ) {
            random_samples[i] = random_n_to_m( 2.00, 5.00 );  // random samples between 2.00 and 5.00
        }
    }
    
    float calculate_average( float *arr, int size )
    {
        float average = 0;
        float total = 0;
        int i;
        for ( i = 0; i < size; i++ ) {
            total += arr[i];
        }
        average = total / size;
        return average;
    }
    
    float random_i_to_j( float i, float j )
    {
        return i + ( rand() / ( RAND_MAX / ( j - i )));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2021-11-12
      • 1970-01-01
      • 2020-03-05
      • 2019-12-30
      • 1970-01-01
      相关资源
      最近更新 更多