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
Should it be med = m[N / 2]; instead of med = m[((N + 1) / 2)];?
是否应该改为med=m[N/2];而不是med=m[((N+1)/2)];?
我是一名优秀的程序员,十分优秀!