-3

I am trying to write a code that will take an input of 20 integers from the user and manipulate it to find the mean, max, min, and standard deviation. Everything I have found online says to pass an array by address, which I think I did correctly, but may be my issue.

I keep getting "Segmentation fault (core dumped)" after I enter in the 20 numbers and don't know why. I also get this warning "hw06.c:38: warning: format '%d' expects type 'int', but argument 2 has type 'int **'" and I don't know how to fix that either.

After these errors are fixed, I don't think my loop for max/min and possible standard deviation are correct.

I have tried many different things. I finally got rid of the errors I was previously having, for not passing my array by address but I don't even know how to tackle this error. I pasted my entire code below:

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

The code should get an array of 20 values from the user, then pass it to the next function where it will print the numbers (this time separated by commas and that last one doesn't need one but I'm not sure how to get rid of that). It then needs to find the average, which was working correctly before but I have changed things since then.

Next it needs to pass the average to the standard deviation function where it calculates standard deviation (the sum of each value - average divided by 20) and finds the max/min of the array.

I am currently just getting an error.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Fredrick93
  • 15
  • 3
  • 1
    The warning (as surprising as it is) is closely related to your crash... There is so much wrong is this program you should ask your professor or teacher to go over it with you... – Nidhoegger Apr 07 '19 at 18:08
  • A step back is needed, as your array is an array of pointers. `int *score[SIZE] = {0};` should be `int score[SIZE] = {0};` and all the consequences of that which follow through, for example `void getInput(int *score[SIZE])` should be `void getInput(int score[])` and `scanf("%d ", score[count]);` should be `scanf("%d ", &score[count]);` and `printf("%d", &score[count]);` should be `printf("%d", score[count]);` . And so on. – Weather Vane Apr 07 '19 at 18:11
  • `getInput(&score[SIZE]);` is certainly an error - passing the address outside array `score` to `getInput()`. – chux - Reinstate Monica Apr 07 '19 at 18:17
  • @WeatherVane This is what I previously had, but when I change it to that I get "c:29: error: conflicting types for 'getInput' hw06.c:13: note: previous declaration of 'getInput' was here" and I was told by students in another class that it is because we need to pass the array by address and not value, which is why I tried adding the * and & symbols – Fredrick93 Apr 07 '19 at 18:19
  • You pass the (redefined) array `int score[SIZE] = {0};` to the functions with `getInput(score);` – Weather Vane Apr 07 '19 at 18:21
  • `It then needs to find the average, which was working correctly before but I have changed things since then.` Then change it back! – Deanie Apr 07 '19 at 18:21
  • @Deanie the average was the only thing working in the previous version and I had almost everything in the same function which will cause me to lose points when graded – Fredrick93 Apr 07 '19 at 18:23
  • How do I edit it to pass the function that the user inputs in the getInput function ? I am trying to send those numbers to the defined function score[SIZE] = {0} to replace the zeros with the numbers the user types @WeatherVane – Fredrick93 Apr 07 '19 at 18:25
  • 1
    As I wrote, with `int score[SIZE] = {0};` and `getInput(score);` and changes to `scanf` and `printf` mentioned in my first comment. – Weather Vane Apr 07 '19 at 18:29
  • basically *every single `*` here needs to be removed.* Then what does not compile must be fixed. The correct program does not have a single `*`. – Antti Haapala -- Слава Україні Apr 07 '19 at 18:34
  • Please don't change the program as posted: this is not a dynamic code-improvement site. Put the changes as an edit, or when complete you can answer your own question. – Weather Vane Apr 07 '19 at 18:35
  • When I get rid of all the * and & I get these warnings/errors " In function 'main': .c:22: warning: passing argument 1 of 'getInput' makes pointer from integer without a cast c:13: note: expected 'int *' but argument is of type 'int' c:23: warning: passing argument 1 of 'getMean' makes pointer from integer without a cast .c:14: note: expected 'int *' but argument is of type 'int' c:24: warning: passing argument 1 of 'getCalc' makes pointer from integer without a cast c:15: note: expected 'int *' but argument is of type 'int' c:29: error: expected ')' before '[' token" @WeatherVane – Fredrick93 Apr 07 '19 at 18:40
  • I now have " hw06.c:29: error: expected ')' before '[' token hw06.c:43: error: conflicting types for 'getMean' hw06.c:14: note: previous declaration of 'getMean' was here hw06.c:62: error: conflicting types for 'getCalc' hw06.c:15: note: previous declaration of 'getCalc' was here" as my errors. I checked and all of my ()'s are closed, so there is no extra one or anything. Also, this was the same error I was getting before that I got rid of by adding the *s and &s (which obviously wasn't correct but I'm not sure how else to fix it) – Fredrick93 Apr 07 '19 at 18:50

1 Answers1

1

You made a very good attempt at the program, so I have edited it with extensive comments. The fundamental problem was trying to work with an array of pointers, instead of an array of values.

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

Program output:

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
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Thank you so much! I made these edits but then I get the errors " hw06.c:68: error: 'INT_MIN' undeclared (first use in this function) hw06.c:68: error: (Each undeclared identifier is reported only once hw06.c:68: error: for each function it appears in.) hw06.c:69: error: 'INT_MAX' undeclared (first use in this function)" I tried inititlaizing these the only way I know hob but then I got the warning that they were unused. I have never used these before so I'm not sure how to remove this warning/error. – Fredrick93 Apr 07 '19 at 19:16
  • Did you add the library `#include ` as commented? – Weather Vane Apr 07 '19 at 19:16
  • yes I did, which is why I'm really confused on why its an error – Fredrick93 Apr 07 '19 at 19:18
  • The variables INT_MAX and INT_MIN appear red, instead of the normal black, meaning that they are special characters and should be working with the library I added – Fredrick93 Apr 07 '19 at 19:20
  • Uh, sorry, try `#include ` instead. – Weather Vane Apr 07 '19 at 19:25
  • It worked!!! Thank you so much! So now the mean, max and min are correct, do you have any ideas on how to calculate the correct deviation? – Fredrick93 Apr 07 '19 at 19:31
  • Well... that's another matter, there are different definitions of deviation. The main problem is solved. If this answer helped you please "accept" it. – Weather Vane Apr 07 '19 at 19:31
  • I believe that I accepted it. I got the deviation, all I needed was to add in the absolute value earlier. Do you have any ideas on how to get the comma to not appear after the last value when printed? – Fredrick93 Apr 07 '19 at 19:41
  • Thank you. By printing the comma separately first, instead of after, like this `if(count > 0) { printf(","); }` and remove it from the other line. – Weather Vane Apr 07 '19 at 20:02
  • I got it to work! Thank you so much, I couldn't have gotten this without you!! – Fredrick93 Apr 07 '19 at 20:06