gpt4 book ai didi

Incorrect median and mode(错误的中位数和模式)

转载 作者:bug小助手 更新时间:2023-10-25 15:11:30 26 4
gpt4 key购买 nike



Details of the problem are mentioned below:

有关问题的详情如下:


#include <stdio.h>

int main() {

int N, i, j, sum, mode, count, f[100], maxf;
float mean, med;

printf("Enter the number of students\n");
scanf("%d", &N);
printf("Number of students:%d\n", N);

int m[N]; // Array to store the marks //

for (i = 0; i < N; i++) {
printf("Enter the marks of student %d\n", i + 1);
scanf("%d", &m[i]);
}

printf("Scores\n");

for (j = 0; j < N; j += 5) {
printf("%d, %d, %d, %d, %d\n", m[j], m[j + 1], m[j + 2], m[j + 3], m[j + 4]);
}

// Sorting the array //

for (i = 0; i < (N - 1); i++) {
for (j = i + 1; j < N; j++) {
if (m[j] < m[i]) {
int temp = m[j];
m[j] = m[i];
m[i] = temp;
}
}
}

if (N % 2 == 0) {
med = (((m[((N / 2) - 1)] + m[(N / 2)])) / 2);
} else
if (N % 2 != 0) {
med = m[((N + 1) / 2)];
}

sum = 0;

for (i = 0; i < N; i++) {
sum += m[i];
}

mean = (((float)(sum)) / (N));

printf("Mean:%f\n", mean);
printf("Median:%d", med);

for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
if (m[j] != -1) {
if (m[j] == m[i]) {
count += 1;
m[j] = -1;
}

count = f[i]; // Array to store frequency //
}
}
}

maxf = 0;

for (i = 0; i < N; i++) {
if (maxf < f[i]) {
maxf = f[i];
}
}

for (i = 0; i < N; i++) {
if (maxf == f[i]) {
printf("Mode: %d\n", m[i]);
}
}
return 0;
}

I was expecting median to come some value but it is coming 0 every time and I am always getting an incorrect mode.

我期待中位数来一些价值,但它是未来0每一次,我总是得到一个不正确的模式。


更多回答

This for loop for (j = 0; j < N; j += 5) { printf("%d, %d, %d, %d, %d\n", m[j], m[j + 1], m[j + 2], m[j + 3], m[j + 4]); } is unsafe if N is not divisible by 5.

如果N不能被5整除,则这个for循环for(j = 0; j < N; j += 5){ printf(“%d,% d,% d,%d\n”,m[j],m[j + 1],m[j + 2],m[j + 3],m[j + 4]); }不安全。

Should it be med = m[N / 2]; instead of med = m[((N + 1) / 2)];?

是否应该改为med=m[N/2];而不是med=m[((N+1)/2)];?

优秀答案推荐

There are multiple problems in the code:

代码中存在多个问题:



  • you do not test the return values of scanf(): you will have undefined behavior if any of the input is missing or causes a conversion error.

    您不测试scanf()的返回值:如果缺少任何输入或导致转换错误,您将具有未定义的行为。



  • when printing the values, you assume N to be a multiple of 5: you will have undefined behavior accessing elements beyond the end of the array if it is not the case.

    在打印值时,假设N是5的倍数:如果不是这样,您将具有访问数组末尾以外的元素的未定义行为。



  • you compute the median value in case of an even number of elements as the average of the values of the central elements of the array: you should use floating point arithmetics for this computation by casting at least one of the operands as a float or double. For example:

    如果元素数为偶数,则将中间值计算为数组中心元素的值的平均值:应使用浮点算术进行此计算,方法是将至少一个操作数转换为浮点数或双精度数。例如:


      med = ((float)m[N / 2 - 1] + m[N / 2]) / 2;


  • in case of an odd number of elements, you should take the value of the central element, which is not at offset (N + 1) / 2 but simply at offset N / 2. You should check such formulas with trivial cases, eg N=1 -> med=m[(1+1)/2] out of range!

    如果元素数为奇数,则应取中心元素的值,该值不在偏移量(N+1)/2处,而只在偏移量N/2处。您应该用小写来检查这样的公式,例如N=1->med=m[(1+1)/2]超出范围!


      med = m[N / 2];

    Note that you can use the formula med = ((float)m[(N - 1) / 2] + m[N / 2]) / 2; in all cases, assuming N > 0.

    请注意,您可以使用公式med=((Float)m[(N-1)/2]+m[N/2])/2;在所有情况下,假设N>0。



  • printf("Median:%d", med) has undefined behavior because you pass a float value (which is automatically converted to a double) where printf expects an int for the conversion %d. You should use %f instead:

    printf(“Median:%d”,med)有未定义的行为,因为你传递了一个浮点值(它会自动转换为double),而printf在转换%d时需要一个int。你应该使用%f:


      printf("Median: %f\n", med);


  • to compute the mode, you use an array f[100] which will be too small if N > 100.

    要计算模式,可以使用一个数组f[100],如果N>100,这个数组就会太小。



  • you never store count to f[i], instead you read count from f[i] which is uninitialized.

    您永远不会将count存储到f[i],而是从未初始化的f[i]中读取count。



  • furthermore, you do not reset count to 0 or 1 for each different value in m.

    此外,对于m中的每个不同值,不要将Count重置为0或1。



  • the mode of the array is the number that appears more times in the array than any other number. You can determine this without an extra array by computing the maximum number of duplicates in a linear scan and print the values with this number of duplicates in a subsequent scan.

    数组的模式是在数组中出现的次数比任何其他数字都多的数字。您可以通过计算线性扫描中的最大重复数来确定这一点,而无需额外的数组,并在后续扫描中使用此重复数打印值。




更多回答

Thanks a million mate for such a comprehensive solution

非常感谢Mate提供如此全面的解决方案

Done...........

完成..

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com