什么是Segmentation Core转储?我该如何解决? [关闭]

问题描述 投票:-3回答:1

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

输入20个数字后我不断收到“Segmentation fault(core dumped)”,不知道为什么。我也得到这个警告“hw06.c:38:警告:格式'%d'期望类型为'int',但是参数2的类型为'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)并找出数组的最大值/最小值。

我目前刚收到错误。

c for-loop max min standard-deviation
1个回答
1
投票

你对这个程序做了很好的尝试,所以我编写了大量的评论。根本问题是尝试使用指针数组而不是数组值。

#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
© www.soinside.com 2019 - 2024. All rights reserved.