如何在数字数组读,在2阵列分割,然后找到这两个单独的阵列的平均? [英] How to read in an array of numbers, split the array in 2, and then find the average of those two separate arrays?
问题描述
我的计划,到目前为止要求输入n个数的列表,然后它发现整个列表的平均值。我怎么能劈成两半的列表,然后找到两者的平均分裂阵列?
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;诠释主(){
诠释N,I;
浮动NUM [100],和= 0.0,平均值;
输出(输入数字的合计量:);
scanf函数(%d个,&安培; N); 对于(i = 0; I< N ++我){
scanf的(%F,试验#[I]);
总和+ = NUM [I]
}
平均=总和/ N;
的printf(平均为=%.3f,平均);
返回0;
}
下面是我的code确实至今:
输入数字的总金额:10
10
8
9
15
12
2
3
8
7
11平均值为:8.500
我希望它做的是:
输入数字的总金额:10
10
8
9
15
12
2
3
8
7
11阵列的第一半的平均值为:10.8
阵列的第二半的平均值为:6.2
有几种方法之
和平均
1/2阵列,本质上都做同样的事情用不同的方式。当你阅读每个阵列1/2,你可以总结/平均,或者您也可以在最后等待,各占一半的迭代产生相同的号码。您也可以将您的阵列分成两个单独的数组(这是非常低效的,因为你已经在内存中的数据,你只需要解决你想用适当的索引来隔离值)。
在看之前的方式,还有你需要使用 scanf函数
的方式使一个改进。所有 scanf函数
家庭成员返回总的成功转换次数的基础上的格式字符串的你提供。你需要的检查返回和验证的,你必须在你的格式字符串指定的成功转换次数的。始终。
一个办法分裂是阅读所有数字,然后遍历1/2阵列来获得每个之
和平均值
值所需。
的#include<&stdio.h中GT;诠释主要(无效){
诠释N,I;
浮NUM [100],总和[2] = {0.0},平均[2] = {0.0}; 输出(输入数字的合计量:);
如果(scanf函数(%d个,&安培;!N)= 1){/ *验证您的输入* /
fprintf中(标准错误,错误:无效的输入\\ n);
返回1;
} 对于(i = 0; I< N;我++){
的printf(请输入总和[%2d]的I);
如果(scanf函数(%F,&安培;!NUM [I])= 1){/ *验证输入* /
fprintf中(标准错误,错误:无效的输入\\ n);
返回1;
}
} / *总和/平均1半* /
对于(i = 0; I< N / 2; ++ I)
总结[0] + = NUM [I];
平均[0] =总和[0] * 2 / N; / *总和/平均第二一半* /
对于(I = N / 2; I< N ++ I)
总结[1] + = NUM [I];
平均[1] =总和[1] * 2 / N; 的printf(\\ n此一日半平均=%.3f \\ n,平均[0]);
的printf(第二届半平均=%.3f \\ n \\ n,平均[1]); 返回0;
}
(注意:您无需使用数组为之
或平均
如果你只是立即将你的的printf
语句每次计算后)。
第二种方法使用两个循环。从第i = 0; I< N / 2
,从第二i = N / 2; I< ñ
。同样,没有必要之
或平均
数组如果移动的printf
到总和紧接着
和平均
算了一笔账:
的#include<&stdio.h中GT;诠释主要(无效){
诠释N,I;
浮NUM [100],总和[2] = {0.0},平均[2] = {0.0}; 输出(输入数字的合计量:);
如果(scanf函数(%d个,&安培;!N)= 1){/ *验证您的输入* /
fprintf中(标准错误,错误:无效的输入\\ n);
返回1;
} / *读/总和/平均1半* /
对于(i = 0; I< N / 2; ++ I){
的printf(请输入总和[%2d]的I);
如果(scanf函数(%F,&安培;!NUM [I])= 1){/ *验证输入* /
fprintf中(标准错误,错误:无效的输入\\ n);
返回1;
}
总结[0] + = NUM [I];
}
平均[0] =总和[0] * 2 / N; / *读/总和/平均第二一半* /
对于(I = N / 2; I< N ++我){
的printf(请输入总和[%2d]的I);
如果(scanf函数(%F,&安培;!NUM [I])= 1){/ *验证输入* /
fprintf中(标准错误,错误:无效的输入\\ n);
返回1;
}
总结[1] + = NUM [I];
}
平均[1] =总和[1] * 2 / N; 的printf(\\ n此一日半平均=%.3f \\ n,平均[0]);
的printf(第二届半平均=%.3f \\ n \\ n,平均[1]); 返回0;
}
输出
无论哪种方式所得到的输出是相同的:
$ ./bin/array_split
输入数字的合计量:8
进入总和[0] 1
进入总和[1] 2
输入总和[2] 3
输入总和[3] 4
输入总和[4] 5-
进入总和[5] 6
进入总和[6] 7
进入总和[7] 8第1半平均= 2.500
第二一半的平均值为6.500 =
或者,你比如数字:
$ ./bin/array_split
输入数字的总量:10
进入总和[0] 10
进入总和[1] 8
进入总和[2] 9
进入总和[3] 15
进入总和[4] 12
输入总和[5] 2
输入总和[6] 3
进入总和[7] 8
进入总和[8] 7
进入总和[9] 11第1半平均= 10.800
第二一半的平均值为6.200 =
My program so far asks to input a list of n numbers, then it finds the average of that entire list. 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
There are several ways to sum
and average
1/2 your array, all essentially do the same thing in different ways. 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. 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
. All of the scanf
family members return the total number of successful conversions based on the format-string you provide. You need to check the return and validate that you have the number of successful conversions specified in your 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.
#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).
The second approach uses two loops. The first from i = 0; i < n/2
, the second from 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:
#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
这篇关于如何在数字数组读,在2阵列分割,然后找到这两个单独的阵列的平均?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!