简体   繁体   English

如何读取一个数字数组,将该数组拆分为2,然后找到这两个单独数组的平均值?

[英]How to read in an array of numbers, split the array in 2, and then find the average of those two separate arrays?

My program so far asks to input a list of n numbers, then it finds the average of that entire list. 到目前为止,我的程序要求输入n个数字的列表,然后找到整个列表的平均值。 How can I split that list in half, and then find the average of the two split up arrays? 如何将列表拆分成两半,然后找到两个拆分数组的平均值?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    int n, i;
    float num[100], sum = 0.0, average;
    printf("Enter the total amount of numbers: ");
    scanf("%d", &n);

    for (i = 0; i < n; ++i) {
        scanf("%f", &num[i]);
        sum += num[i];
    }
    average = sum / n;
    printf("The average is = %.3f", average);
    return 0;
} 

Here is what my code does so far: 到目前为止,这是我的代码执行的操作:

Enter the total amount of numbers:10  
10   
8  
9   
15  
12    
2  
3  
8  
7  
11  

The average is: 8.500

What I want it to do is: 我想要它做的是:

Enter the total amount of numbers: 10  
10  
8  
9  
15  
12  
2  
3  
8  
7  
11  

The average of the first half of the array is: 10.8  
The average of the second half of the array is: 6.2

Simple changing to have array of sum and array of average - each having two members - would do (this way, you only need a single loop): 简单地更改为具有和数组和均值数组(每个都有两个成员)即可(这样,您只需要一个循环):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int n, i;
    float num[100], sum[2] = {0,0}, average[2]; //have arrays here
    printf("Enter the total amount of numbers: ");
    scanf("%d",&n);

   for(i=0; i<n; ++i)
   {
      scanf("%f",&num[i]);
      sum[(i*2)/n] +=num[i]; //note the trick here
   }
   average[0]=sum[0]/n;
   average[1]=sum[1]/n;
   printf("The average of the first half of the array is = %.3f\n", average[0]);
   printf("The average of the second half of the array is = %.3f", average[1]);
   return 0;
} 

There are several ways to sum and average 1/2 your array, all essentially do the same thing in different ways. 有几种方法可以对数组sum并求average 1/2,所有这些本质上都以不同的方式完成相同的操作。 You can sum/average as you read each 1/2 of the array, or you can wait and iterate of each half at the end to produce the same numbers. 您可以在读取数组的每个1/2时求和/求平均值,也可以等待并在最后对每个一半进行迭代以产生相同的数字。 You could also separate your array into two separate arrays (which is quite inefficient, since you already have the data in memory, you need only address the values you want to segregate with proper indexing). 您还可以将数组分成两个单独的数组(效率很低,因为您已经在内存中存储了数据,因此只需要使用适当的索引处理要隔离的值)。

Before looking at the ways, there is one improvement you need to make in the way you use scanf . 在研究这些方法之前,您需要对使用scanf的方式进行一些改进。 All of the scanf family members return the total number of successful conversions based on the format-string you provide. 所有scanf系列成员均根据您提供的格式字符串返回成功转换的总数。 You need to check the return and validate that you have the number of successful conversions specified in your format-string . 您需要检查返回值验证您是否具有在format-string中指定的成功转换次数。 Always. 总是。

One approach to the split is to read all numbers and then iterate over 1/2 the array to get each of the sum and average values desired. 拆分的一种方法是读取所有数字,然后在数组的1/2上进行迭代以获得所需的sumaverage

#include <stdio.h>

int main (void) {
    int n, i;
    float num[100], sum[2] = {0.0}, average[2] = {0.0};

    printf("Enter the total amount of numbers: ");
    if (scanf("%d", &n) != 1) {  /* validate your input */
        fprintf (stderr, "error: invalid input.\n");
        return 1;
    }

    for (i = 0; i < n; i++) {
        printf (" enter sum[%2d] ", i);
        if (scanf("%f", &num[i]) != 1) { /* validate input */
            fprintf (stderr, "error: invalid input.\n");
            return 1;
        }
    }

    /* sum/average 1st-half */
    for (i = 0; i < n/2; ++i)
        sum[0] += num[i];
    average[0] = sum[0] * 2 / n;

    /* sum/average 2nd-half */
    for (i = n/2; i < n; ++i)
        sum[1] += num[i];
    average[1] = sum[1] * 2 / n;

    printf("\nThe 1st-half average is = %.3f\n", average[0]);
    printf("The 2nd-half average is = %.3f\n\n", average[1]);

    return 0;
}

( note: you do not need to use arrays for sum or average if you just move your printf statements immediately after each calculation). 注意:如果只在每次计算后立即移动printf语句,则不需要使用数组sum或求average )。

The second approach uses two loops. 第二种方法使用两个循环。 The first from i = 0; i < n/2 i = 0; i < n/2的第一个i = 0; i < n/2 i = 0; i < n/2 , the second from i = n/2; i < n i = 0; i < n/2 ,从i = n/2; i < n的第二秒i = n/2; i < n i = n/2; i < n . i = n/2; i < n Again, no need for sum or average arrays if you move the printf to immediately following the sum and average calculations: 同样,如果将printf移动到紧接着进行sumaverage计算,则不需要sumaverage数组:

#include <stdio.h>

int main (void) {
    int n, i;
    float num[100], sum[2] = {0.0}, average[2] = {0.0};

    printf("Enter the total amount of numbers: ");
    if (scanf("%d", &n) != 1) {  /* validate your input */
        fprintf (stderr, "error: invalid input.\n");
        return 1;
    }

    /* read/sum/average 1st-half */
    for (i = 0; i < n/2; ++i) {
        printf (" enter sum[%2d] ", i);
        if (scanf("%f", &num[i]) != 1) {  /* validate input */
            fprintf (stderr, "error: invalid input.\n");
            return 1;
        }
        sum[0] += num[i];
    }
    average[0] = sum[0] * 2/ n;

    /* read/sum/average 2nd-half */
    for (i = n/2; i < n; ++i) {
        printf (" enter sum[%2d] ", i);
        if (scanf("%f", &num[i]) != 1) {  /* validate input */
            fprintf (stderr, "error: invalid input.\n");
            return 1;
        }
        sum[1] += num[i];
    }
    average[1] = sum[1] * 2 / n;

    printf("\nThe 1st-half average is = %.3f\n", average[0]);
    printf("The 2nd-half average is = %.3f\n\n", average[1]);

    return 0;
} 

Output 输出量

Either way the resulting output is the same: 无论哪种方式,结果输出都是相同的:

$ ./bin/array_split
Enter the total amount of numbers: 8
 enter sum[ 0] 1
 enter sum[ 1] 2
 enter sum[ 2] 3
 enter sum[ 3] 4
 enter sum[ 4] 5
 enter sum[ 5] 6
 enter sum[ 6] 7
 enter sum[ 7] 8

The 1st-half average is = 2.500
The 2nd-half average is = 6.500

Or, for your example numbers: 或者,对于您的示例数字:

$ ./bin/array_split
Enter the total amount of numbers: 10
 enter sum[ 0] 10
 enter sum[ 1] 8
 enter sum[ 2] 9
 enter sum[ 3] 15
 enter sum[ 4] 12
 enter sum[ 5] 2
 enter sum[ 6] 3
 enter sum[ 7] 8
 enter sum[ 8] 7
 enter sum[ 9] 11

The 1st-half average is = 10.800
The 2nd-half average is = 6.200

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM