An issue after successfully compiling the program

This is my first post here but it won't certainly be my last. I am trying to do this assignment that my professor handed to me and the code compiles everything. The issue I have though is that the exe prompt crashes each time I execute it. Can someone tell me what I am doing wrong here?

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
 #include <iostream>

using namespace std;
int maximum(double scores[],int);
int minimum(double scores[],int);
int sumoftotal(double scores[],int);

int maximum (double scores[],int m)
{
    int maxi=0;
    for(int j=0;j<m;j++)
    {

        if (scores[j]>maxi)
            maxi=scores[j];
    }
    return maxi;
}

int minimum (double scores[],int m)
{
    int mini=0;
    for(int k=0;k<m;k++)
    {

        if (scores[k]<mini)
            mini=scores[k];
    }
    return mini;
}

int sumoftotal(double scores[],int m)
{
    int sum=0;
    for(int l=0;l<m;l++)
    {
        sum+=scores[l];
    }
    return sum;
}

int main()
{
    int id,judge;
    double scores[judge];
    double maxi = 0;
    double mini = 0;
    double sum,avgscore;
    int m = judge;
    int n=0;


    cout << "Enter the ID for Athlete" << endl;
    cin >> id;
    while(id>=0)
    {
    cout << "Enter the amount of judges" << endl;
    cin >> judge;

    cout << "Please enter the " << judge << " scores" << endl;

    for(int i=0;i<judge;i++)
    {
        cin >> scores[i];

    }

    maxi=maximum(scores,m);
    mini=minimum(scores,m);
    sum=sumoftotal(scores,m);



    avgscore = (sum - maxi - mini )/(judge - 2);

    cout << avgscore << " is the higher number" << endl;
    cout << "Enter the ID for the Athlete" << endl;
    cin >> id;
    n++;
    }

    return 0;
}


Is it because I have too many for loops or something?!
EDIT: Quick ninja edit, I am using Code Blocks as my main compiler
Last edited on
1
2
int id,judge;         // judge is uninitialize ...
double scores[judge]; // ... so what size will scores have? 

Last edited on
1. Judge is being used as a cin variable here.
2. I wanted to make the array match the number of judges and my professor recommended you can do it this way according to the lectures.
This is illegal in c++
1
2
	int id, judge;
	double scores[judge];


To make this legal you would have to either make judge const
1
2
const int JUDGE = 10;
double scores[JUDGE];


or use a dynamic array
double *scored = new double[judge];


In codeblocks click settings
compiler
then check the box that says
Treat as errors the warnings demanded by strict ISO C and ISO c++[-pedantic-errors]

This will prevent codeblocks from allowing you to write non standard code

Quick ninja edit, I am using Code Blocks as my main compiler

codeblocks isn't a compiler its an IDE
Last edited on
I can't make it constant because the "Judge" gives the number of the amount of variables in the array. We haven't learned about dynamic array so I kind of have no idea how to approach that. Is there no way around this kind of thing?
You could use vector but I'm guessing your not allowed to use that, if you haven't learned dynamic arrays yet. The only other thing I can think of, is to simply make your array larger than you will probably need, and do a check to make sure you don't exceed this amount.
Last edited on
Or you can just use the non-standard variable length array feature if that's what your professor wants you to use. Just make sure to create the array after judge has been given a value.
Since variable judge is not initialized therefore m is holding a garbage value. When your functions for finding maximum and minimum value will run they will iterate the scores[] array till m whose value can be way larger then the size of bounded scores[] array.

Variable m is not the size of scores[] array, it is a garbage value and when you access an index that is out of boundary your program may crash if reading that memory block is restricted. Otherwise you may get wrong result as value on that memory block can be anything.

One other thing, Line: 74 of your code: What if the value of judge is 2. Dividing by zero
will also crash your program since it goes to infinite. You need to fix this formula for calculating averages.

I have made little corrections as mentioned below:

1) local variable judge was holding garbage value when it was assigned to variable m since it was not initialized.

2) Variable n is useless since it is only increasing without being used anywhere in the code

3) Id of Athlete was asked twice. Before the loop and at the end of loop.

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
#include <iostream>
using namespace std;


int maximum(double scores[], int);
int minimum(double scores[], int);
int sumoftotal(double scores[], int);


int maximum (double scores[], int m)
{
    int maxi=0;
    for(int j=0;j<m;j++)
        if (scores[j]>maxi)
            maxi=scores[j];

    return maxi;
}


int minimum (double scores[], int m)
{
    int mini=0;
    for(int k=0;k<m;k++)
        if (scores[k]<mini)
            mini=scores[k];

    return mini;
}


int sumoftotal(double scores[],int m)
{
    int sum=0;
    for(int l=0;l<m;l++)
        sum+=scores[l];

    return sum;
}


int main()
{
    int id=0, judge=0, m=0, i=0;
    double *scores, maxi = 0.0, mini = 0.0, sum = 0.0, avgscore = 0.0;
    
    while(true)
    {
        cout << "Enter the ID for Athlete" << endl;
        cin >> id;
        
        if(id <= 0)
            break;
        
        cout << "Enter the amount of judges" << endl;
        cin >> judge;
        
        scores = new double[judge];
        m = judge;
        
        cout << "Please enter the " << judge << " scores" << endl;
    
        for(i=0;i<judge;i++)
        {
            cin >> scores[i];
        }
    
        maxi=maximum(scores,m);
        mini=minimum(scores,m);
        sum=sumoftotal(scores,m);
        
        // What if the value of judge is 2 ??? It will be infinite ???
        avgscore = (sum - maxi - mini )/(judge - 2);
    
        cout << avgscore << " is the higher number" << endl;
    }

    return 0;
}
Topic archived. No new replies allowed.