gpt4 book ai didi

c - 单击代码块中的“构建并运行”后,错误 : CentralTendencies. exe 已停止工作

转载 作者:行者123 更新时间:2023-11-30 14:48:23 26 4
gpt4 key购买 nike

此代码用于查找给定数字序列的平均值、中位数和众数。

#include <stdio.h>
#include <math.h>

int main()
{ /*Inputs*/
int n;
int a[n];
/*Outputs*/
float mean;
int median;
int mode;

printf("Enter the size of array: ");
scanf("%d",&n);
for(int i=0; i<n; i++)
{
printf("\nEnter the values of array: ");
scanf("%d",&a[i]);
}
/*arrange in ascending order */
int i=0;
for(i=0; i<n-1; i++)
{for(i=0; i<n-1; i++)
{
if(a[i]>a[i+1])
{
int temp = a[i+1];
a[i+1] = a[i];
a[i] = temp;
}
}
}
/*Mean of the series*/
for(i=0; i<n; i++)
{
int sum = 0;
sum = sum + a[i];
mean = sum/n;
}

/*Median of the series*/

我希望这里的类型转换语法是正确的,对吧?

int m = floor((double)n/2);
if(n%2==0)
{
median = (a[m]+a[m-1])/2;
}
else
{
median = a[m];
}

/*Mode of the series*/
int count;
int b = 0;
for(i=0; i<n-1; i++)
{
for(int t=0; t<n; t++) //Compare a[i] with all the elements of the array
{if(a[i]==a[t])
{
count = 0;
count++;
}
if(b<count) //Update the new value of mode iff the frequency of one element is greater than that
{ // of its preceding element.
mode = a[i];
}
}
}

printf("The value of mean: %f",mean);
printf("\nThe value of mean: %d",median);
printf("\nThe value of mean: %d",mode);
return 0;
}

代码中没有错误。

在“CentralT……工作”后,我收到以下提示消息:进程返回-1073741571(0xC00000FD)执行时间:47.686 s按任意键继续。

最佳答案

在 C 语言中,你无法调整数组的大小。

这一行

int n;

定义n并将其值保留为垃圾值,一个随机值,可能是0

基于n持有这条线

int a[n];

a 定义为数组,其元素数量与 n now 的元素数量相同。

读入本行中的n

scanf("%d",&n);

改变a元素的数字。

要解决此问题,请仅在 n 具有明确定义的有效值后定义 a:

scanf("%d",&n);
int a[n];

要真正确保 n 已设置,您需要测试 scanf() 的结果,例如:

do {
if (1 != scanf("%d, &n))
{
puts("Bad input or failure reading n!\n");
continue;
}
} while (0);

关于c - 单击代码块中的“构建并运行”后,错误 : CentralTendencies. exe 已停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432487/

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