我的程序到目前为止要求输入n个数字的列表,然后找到整个列表的平均值。如何将该列表拆分为一半,然后找到两个拆分阵列的平均值?
#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;
}
以下是我的代码到目前为止所做的事情:
Enter the total amount of numbers:10
10
8
9
15
12
2
3
8
7
11
The average is: 8.500
我想要它做的是:
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
答案 0 :(得分:1)
简单地改变为具有平均数组和平均数组 - 每个都有两个成员 - 会这样做(这样,你只需要一个循环):
#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;
}
答案 1 :(得分:1)
有几种方法sum
和average
1/2你的数组,所有基本上都以不同的方式做同样的事情。您可以在读取阵列的每个1/2时求和/平均值,或者您可以在最后等待并迭代每一半以产生相同的数字。您还可以将数组分成两个独立的数组(这是非常低效的,因为您已经将数据存储在内存中,您只需要通过正确的索引来解决您想要隔离的值。)
在查看方法之前,您需要使用scanf
进行一项改进。所有scanf
家庭成员都会根据您提供的 format-string 返回成功转化的总数。您需要检查返回并验证您拥有 format-string 中指定的成功转化次数。总是
分割的一种方法是读取所有数字,然后迭代1/2数组以获得所需的sum
和average
值。
#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;
}
(注意:如果您只是在每次计算后立即移动sum
语句,则不需要为average
或printf
使用数组。< / p>
第二种方法使用两个循环。第一个来自i = 0; i < n/2
,第二个来自i = n/2; i < n
。同样,如果您将sum
移至紧跟average
和printf
计算之后,则无需sum
或average
数组:
#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;
}
<强>输出强>
无论哪种方式,结果输出都是相同的:
$ ./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
或者,对于您的示例数字:
$ ./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