首页 > 解决方案 > 什么是分段核心转储?我该如何解决?

问题描述

我正在尝试编写一个代码,该代码将从用户那里输入 20 个整数并对其进行操作以找到平均值、最大值、最小值和标准偏差。我在网上找到的所有内容都说按地址传递数组,我认为我做得对,但可能是我的问题。

输入 20 个数字后,我不断收到“分段错误(核心转储)”,但不知道为什么。我也收到此警告“hw06.c:38: warning: format '%d' expects type 'int', but argument 2 has type 'int **'”,我也不知道如何解决这个问题。

修复这些错误后,我认为我的最大/最小循环和可能的标准偏差不正确。

我尝试了很多不同的东西。我终于摆脱了以前遇到的错误,因为我没有按地址传递数组,但我什至不知道如何解决这个错误。我在下面粘贴了我的整个代码:

#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)并找到数组的最大值/最小值。

我目前只是收到一个错误。

标签: cfor-loopmaxminstandard-deviation

解决方案


你在这个程序上做了一个很好的尝试,所以我用大量的评论编辑了它。基本问题是尝试使用指针数组,而不是值数组。

#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;
}

程序输出:

输入的数据集:1, 3, 5, 2, 5, 8, 9, 1, 2, 3, 4, 5, 5, 1, 1, 7, 3, 7, 9, 0,
 平均值:4.05
平均偏差:0.00
取值范围:0、9


推荐阅读