【问题标题】:What is Segmentation Core dump? And how do I fix it? [closed]什么是分段核心转储?我该如何解决? [关闭]
【发布时间】:2019-08-28 22:26:50
【问题描述】:

我正在尝试编写一个代码,该代码将从用户那里获取 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)并找到数组的最大值/最小值。

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

【问题讨论】:

  • 警告(尽管令人惊讶)与您的崩溃密切相关...这个程序有很多错误,您应该请您的教授或老师与您一起检查...
  • 需要后退一步,因为您的数组是一个指针数组。 int *score[SIZE] = {0}; 应该是 int score[SIZE] = {0}; 并且后续的所有后果,例如 void getInput(int *score[SIZE]) 应该是 void getInput(int score[])scanf("%d ", score[count]); 应该是 scanf("%d ", &amp;score[count]);printf("%d", &amp;score[count]); 应该是 printf("%d", score[count]); 。以此类推。
  • getInput(&amp;score[SIZE]); 肯定是一个错误 - 将数组 score 之外的地址传递给 getInput()
  • @WeatherVane 这是我以前拥有的,但是当我将其更改为“c:29: error: conflicting types for 'getInput' hw06.c:13: note: previous declaration of 'getInput' 在这里”,另一个班级的学生告诉我,这是因为我们需要通过地址而不是值来传递数组,这就是为什么我尝试添加 * 和 & 符号
  • 正如我所写,int score[SIZE] = {0};getInput(score); 以及我在第一条评论中提到的 scanfprintf 的更改。

标签: c for-loop max min standard-deviation


【解决方案1】:

您对该程序进行了非常好的尝试,因此我使用大量 cmets 对其进行了编辑。根本问题是尝试使用指针数组,而不是值数组。

#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

【讨论】:

  • 非常感谢!我进行了这些编辑,但随后出现错误“ hw06.c:68: error: 'INT_MIN' undeclared (first use in this function) hw06.c:68: error: (每个未声明的标识符仅报告一次 hw06.c: 68:错误:对于它出现的每个函数。)hw06.c:69:错误:'INT_MAX'未声明(在此函数中首次使用)未使用。我以前从未使用过这些,所以我不确定如何删除此警告/错误。
  • 您是否按照评论添加了库#include &lt;ctype.h&gt;
  • 是的,我做到了,这就是为什么我真的很困惑为什么它是一个错误
  • 变量 INT_MAX 和 INT_MIN 显示为红色,而不是正常的黑色,这意味着它们是特殊字符,应该与我添加的库一起使用
  • 呃,抱歉,改用#include &lt;limits.h&gt;
猜你喜欢
  • 1970-01-01
  • 2017-04-05
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多