Segmentation Error

I'm getting this segmentation error and I have no idea what's wrong. I'm supposed to create mean median and mode functions that take in an array. I've looked through the test cases and it looks like the max number in the array is 9999, but the max size is probably ~1000. If I comment out one of these 3 functions, my program works fine. But with all 3 I'm getting problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  #include <iostream>
#include <fstream>

using namespace std;

int get_students();
int add_movie_count(int[], const int, ofstream&);
double get_average(int[], const int);
int get_median(int[], const int);
int get_mode(int[], const int);
void bubble_sort(int[], const int);

int main(int argc, char * argv[])
{
  if(!(argc > 1))
  {
    return 1;
  }

  ofstream outfile(argv[1], ios::out);
  if(!(outfile.is_open()))
  {
    std::cout << "Error writing to file";
    return 1;
  }

  outfile << "How many students were surveyed? ";
  const int ARRAY_SIZE = get_students();
  if (ARRAY_SIZE <= 1)
  {
    outfile << "Number of students surveyed must be greater than 1.\n";
    return 1;
  }

  int *movies_per_student = new int[ARRAY_SIZE];

  outfile << "Enter the number of movies each student saw." << endl;
  int check = add_movie_count(movies_per_student, ARRAY_SIZE, outfile);

  if (check == 99)
  {
    return 1;
  }

  bubble_sort(movies_per_student, ARRAY_SIZE);

  double average = get_average(movies_per_student, ARRAY_SIZE);
  //int median = get_median(movies_per_student, ARRAY_SIZE);
  int mode = get_mode(movies_per_student, ARRAY_SIZE);

  outfile << "The average number of movies seen is: " << average << endl;
  //outfile << "The median of the number of movies seen is: " << median << endl;
  outfile << "The mode of the number of movies seen is: " << mode << endl;

  cout << "";

  outfile.close();
  delete [] movies_per_student;
  return 0;

}

int get_students()
{
  int students = 0;
  cin >> students;
  return students;
}

int add_movie_count(int array[],const int SIZE, ofstream& outfile)
{
  int i = 1;
  int movies = 0;
  for(int j = 0; j < SIZE; j++)
  {
    cin >> movies;
    outfile << "Student " << i << ": ";

    if(movies < 0)
    {
      outfile << "Number of movies must be 0 or greater.\n";
      return 99;
    }
    else if (movies >=  0)
    {
      array[j] = movies;
      movies = 0;
    }

    i++;
  }
  return 0;
}

void bubble_sort(int array[], const int SIZE)
{
  for (int i = 1; i < SIZE; i++)
  {
    for (int j = 0; j < SIZE - 1 - i; j++)
    {
      if (array[j] < array[j + 1])
      {
        int temp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = temp;
      }
    }
  }
}

double get_average(int array[], const int SIZE)
{
  double average = 0;
  for(int i = 0; i < SIZE; i++)
  {
    average = average + array[i];
  }
  average = average/SIZE;
  return average;
}

int get_mode(int array[], const int SIZE)
{
  int start = array[0];
  int mode = start;
  int count = 1;
  int counter = 1;

  for(int i = 1; i < SIZE; i++)
  {
    if(array[i] == start)
    {
      count++;
    }
    else
    {
      if(count > counter)
      {
        counter = count;
        mode = start;
      }

     count = 1;
     start = array[i];
    }
  }
  return mode;
}
Last edited on
Your code works. Can you post input which crashes your program?
Topic archived. No new replies allowed.