什么是分段核心转储?我该如何解决?
What is Segmentation Core dump? And how do I fix it?
我正在尝试编写一个代码,它将从用户那里获取 20 个整数的输入并对其进行操作以找到平均值、最大值、最小值和标准差。我在网上找到的所有内容都说按地址传递数组,我认为我做对了,但可能是我的问题。
输入 20 个数字后,我一直得到 "Segmentation fault (core dumped)",但不知道为什么。我也收到此警告 "hw06.c:38: warning: format '%d' expects type 'int', but argument 2 has type 'int **'",但我也不知道如何解决。
修复这些错误后,我认为 max/min 的循环和可能的标准偏差不正确。
我尝试过很多不同的东西。我终于摆脱了我以前遇到的错误,因为我没有通过地址传递我的数组,但我什至不知道如何解决这个错误。我在下面粘贴了我的整个代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 20
void getInput(int score[SIZE]);
double getMean(int *score[SIZE]);
void getCalc(int *score[SIZE], double avg);
int main()
{
int score[SIZE] = {0};
double avg;
getInput(score[SIZE]);
avg = getMean(&score[SIZE]);
getCalc(&score[SIZE], avg);
return 0;
}
void getInput(int score[SIZE])
{
int count = 0;
printf("Enter 20 integer values -> ");
for (count = 0; count < SIZE; count++)
{
scanf("%d ", &score[count]);
printf("%d", score[count]);
}
return;
}
double getMean(int* score[])
{
int count = 0;
int totalNum = 0;
double avg;
printf("\nData set as entered: ");
for (count = 0; count < SIZE; count++)
{
totalNum = totalNum + *score[count];
printf("%d, ", *score[count]);
}
avg = ((double)totalNum / 20.0);
printf("\nMean: %.2lf", avg);
return avg;
}
void getCalc(int* score[], double avg)
{
int count = 0;
double deviation;
double standard;
int max;
int min;
for (count = 0; count < SIZE; count++)
{
deviation += (*score[count] - avg);
//printf("%lf", deviation);
if (*score[count] > *score[count - 1])
{
max = *score[count];
}
else
{
min = *score[count];
}
}
standard = (double)deviation / 20.0;
printf("\nMean Deviation: %.2lf ", standard);
printf("\nRange of Values: %d, %d", min, max);
return;
}
代码应该从用户那里得到一个包含 20 个值的数组,然后将它传递给下一个打印数字的函数(这次用逗号分隔,最后一个不需要,但我不知道如何摆脱它)。然后它需要找到平均值,它之前工作正常,但从那以后我改变了一些东西。
接下来它需要将平均值传递给标准偏差函数,在该函数中计算标准偏差(每个值的总和 - 平均值除以 20)并找到数组的 max/min。
我目前只是遇到一个错误。
你在程序上做了很好的尝试,所以我编辑了它并附上了很多评论。根本问题是尝试使用指针数组,而不是值数组。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // added a library for integer range
#include <math.h>
#define SIZE 20
void getInput(int score[]); // revised these function protoypes
double getMean(int score[]);
void getCalc(int score[], double avg);
int main(void) // added void for modern definition
{
int score[SIZE] = {0}; // change to array of int not pointers
double avg;
getInput(score); // you were passing a out-of-bounds element
avg = getMean(score); // ditto
getCalc(score, avg); // ditto
return 0;
}
void getInput(int score[]) // passing an array of int not pointers
{
int count = 0;
printf("Enter 20 integer values -> ");
for (count = 0; count < SIZE; count++)
{
scanf("%d", &score[count]); // removed a space from scanf, added the &
//printf("%d", score[count]); // removed the & but commented out
}
return;
}
double getMean(int score[]) // passing an array of int not pointers
{
int count = 0;
int totalNum = 0;
double avg;
printf("\nData set as entered: ");
for (count = 0; count < SIZE; count++)
{
totalNum = totalNum + score[count]; // removed the *
printf("%d, ", score[count]); // ditto
}
avg = ((double)totalNum / 20.0);
printf("Mean: %.2lf\n", avg); // repostioned the newline
return avg;
}
void getCalc(int score[], double avg) // passing an array of int not pointers
{
int count = 0;
double deviation = 0; // initialise this to 0
double standard;
int max = INT_MIN; // initialise these two
int min = INT_MAX;
for (count = 0; count < SIZE; count++)
{
deviation += score[count] - avg; // removed the *
//printf("%lf", deviation);
if (score[count] > max) // removed the *s, changed the comparison
{ // it was indexing out-of-bounds
max = score[count]; // removed the *
}
if (score[count] < min) // replaced the else with this line
{
min = score[count]; // removed the *
}
}
standard = (double)deviation / 20.0;
printf("Mean Deviation: %.2f\n", standard); // repostion \n, remove `l` from `lf`
printf("Range of Values: %d, %d\n", min, max); // ditto
return;
}
程序输出:
Data set as entered: 1, 3, 5, 2, 5, 8, 9, 1, 2, 3, 4, 5, 5, 1, 1, 7, 3, 7, 9, 0,
Mean: 4.05
Mean Deviation: 0.00
Range of Values: 0, 9
我正在尝试编写一个代码,它将从用户那里获取 20 个整数的输入并对其进行操作以找到平均值、最大值、最小值和标准差。我在网上找到的所有内容都说按地址传递数组,我认为我做对了,但可能是我的问题。
输入 20 个数字后,我一直得到 "Segmentation fault (core dumped)",但不知道为什么。我也收到此警告 "hw06.c:38: warning: format '%d' expects type 'int', but argument 2 has type 'int **'",但我也不知道如何解决。
修复这些错误后,我认为 max/min 的循环和可能的标准偏差不正确。
我尝试过很多不同的东西。我终于摆脱了我以前遇到的错误,因为我没有通过地址传递我的数组,但我什至不知道如何解决这个错误。我在下面粘贴了我的整个代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 20
void getInput(int score[SIZE]);
double getMean(int *score[SIZE]);
void getCalc(int *score[SIZE], double avg);
int main()
{
int score[SIZE] = {0};
double avg;
getInput(score[SIZE]);
avg = getMean(&score[SIZE]);
getCalc(&score[SIZE], avg);
return 0;
}
void getInput(int score[SIZE])
{
int count = 0;
printf("Enter 20 integer values -> ");
for (count = 0; count < SIZE; count++)
{
scanf("%d ", &score[count]);
printf("%d", score[count]);
}
return;
}
double getMean(int* score[])
{
int count = 0;
int totalNum = 0;
double avg;
printf("\nData set as entered: ");
for (count = 0; count < SIZE; count++)
{
totalNum = totalNum + *score[count];
printf("%d, ", *score[count]);
}
avg = ((double)totalNum / 20.0);
printf("\nMean: %.2lf", avg);
return avg;
}
void getCalc(int* score[], double avg)
{
int count = 0;
double deviation;
double standard;
int max;
int min;
for (count = 0; count < SIZE; count++)
{
deviation += (*score[count] - avg);
//printf("%lf", deviation);
if (*score[count] > *score[count - 1])
{
max = *score[count];
}
else
{
min = *score[count];
}
}
standard = (double)deviation / 20.0;
printf("\nMean Deviation: %.2lf ", standard);
printf("\nRange of Values: %d, %d", min, max);
return;
}
代码应该从用户那里得到一个包含 20 个值的数组,然后将它传递给下一个打印数字的函数(这次用逗号分隔,最后一个不需要,但我不知道如何摆脱它)。然后它需要找到平均值,它之前工作正常,但从那以后我改变了一些东西。
接下来它需要将平均值传递给标准偏差函数,在该函数中计算标准偏差(每个值的总和 - 平均值除以 20)并找到数组的 max/min。
我目前只是遇到一个错误。
你在程序上做了很好的尝试,所以我编辑了它并附上了很多评论。根本问题是尝试使用指针数组,而不是值数组。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // added a library for integer range
#include <math.h>
#define SIZE 20
void getInput(int score[]); // revised these function protoypes
double getMean(int score[]);
void getCalc(int score[], double avg);
int main(void) // added void for modern definition
{
int score[SIZE] = {0}; // change to array of int not pointers
double avg;
getInput(score); // you were passing a out-of-bounds element
avg = getMean(score); // ditto
getCalc(score, avg); // ditto
return 0;
}
void getInput(int score[]) // passing an array of int not pointers
{
int count = 0;
printf("Enter 20 integer values -> ");
for (count = 0; count < SIZE; count++)
{
scanf("%d", &score[count]); // removed a space from scanf, added the &
//printf("%d", score[count]); // removed the & but commented out
}
return;
}
double getMean(int score[]) // passing an array of int not pointers
{
int count = 0;
int totalNum = 0;
double avg;
printf("\nData set as entered: ");
for (count = 0; count < SIZE; count++)
{
totalNum = totalNum + score[count]; // removed the *
printf("%d, ", score[count]); // ditto
}
avg = ((double)totalNum / 20.0);
printf("Mean: %.2lf\n", avg); // repostioned the newline
return avg;
}
void getCalc(int score[], double avg) // passing an array of int not pointers
{
int count = 0;
double deviation = 0; // initialise this to 0
double standard;
int max = INT_MIN; // initialise these two
int min = INT_MAX;
for (count = 0; count < SIZE; count++)
{
deviation += score[count] - avg; // removed the *
//printf("%lf", deviation);
if (score[count] > max) // removed the *s, changed the comparison
{ // it was indexing out-of-bounds
max = score[count]; // removed the *
}
if (score[count] < min) // replaced the else with this line
{
min = score[count]; // removed the *
}
}
standard = (double)deviation / 20.0;
printf("Mean Deviation: %.2f\n", standard); // repostion \n, remove `l` from `lf`
printf("Range of Values: %d, %d\n", min, max); // ditto
return;
}
程序输出:
Data set as entered: 1, 3, 5, 2, 5, 8, 9, 1, 2, 3, 4, 5, 5, 1, 1, 7, 3, 7, 9, 0, Mean: 4.05 Mean Deviation: 0.00 Range of Values: 0, 9