Help with Output

Hello. My main works properly but I believe my functions are not working. When I run the program, there is no output.
What I'm trying to do is create an array from a data file with an unknown amount of elements, use bubble sort ascending order, find the median of the array, and output results.
Could anyone direct me to the "obvious" errors I am unable to recognize? Thank you in advance!!!

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>
#include <cmath>
#include <fstream>
#include <iomanip>


using namespace std;

void bubble_sort(int, int & );
void output(int scores[], int );
double median(int scores[], int );

int main()
{
        ifstream score;
        const int SIZE = 100;
        int scores[SIZE];
        score.open("scores.dat");
        int i(0), temp;
        bool end = false;

        while(end == false)
        {
                score >> temp;
                if (!score.eof())
                {
                        scores[i] = temp;
                        i++;
                }
                else
                end = true;
        }

        score.close();

        return 0;
}


void bubble_sort(int scores[], int &SIZE)

{
        bool swaps = true;
        int j = 0, tmp;

        while(swaps)
        {
        swaps = false;
        j++;
        for(int i = 0; i < SIZE - j; ++i)
                {
                if (scores[i] > scores[i + 1])
                        {
                        tmp = scores[i];
                        scores[i] = scores[i + 1];
                        scores[i + 1] = tmp;
                        swaps = true;
                        }
                }
        }
}


double median(int scores[], int &n)

{
        double median;

        if(n % 2 == 0)
        {
        median = (scores[n / 2] + scores[n / 2] -1) / 2.0;
        }
        median = scores[n / 2];

        return median;
}


void output(int scores[], int &SIZE)
{
        for (int count = 0; count < SIZE; count++)
        cout << scores[count] << " ";
        cout << endl;
}
closed account (28poGNh0)
what this data file looks like gives the elements that contains
In your main() function you never call any other function. You just read values, then program ends. That is all.
Thank you MiiNiPaa but I'm not sure where I'm calling another function. I thought I needed prototypes above my function (if you're talking about lines 9-11). If you're talking about within the main(), I'm not sure where I'm calling another function.
I'm not sure where I'm calling another function.
This is exactly the cause: you do not. So nothing is outputted because you didn't ask anything to be outputted.
Last edited on
Okay...I think I get it. I'll repost the code here in a second. Thank you.
Hello, I've attempted to call the output function from the Main but I'm unsure where it should be located and how it should look. If I want to call the 'output' function, should it look like output() or cout output()? Please help. I've been looking at this so such I'm driving myself crazy. Thanks!

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
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>


using namespace std;

void bubble_sort(int scores[], int);
double median(int scores[], int );
void output(int scores[], int );


int main()
{
        ifstream score;
        const int size = 100;
        int scores[size];
        score.open("scores.dat");
        int i(0), temp;
        bool end = false;

        while(end == false)
        {
                score >> temp;
                if (!score.eof())
                {
                        scores[i] = temp;
                        cout << scores[i] << " ";
                        i++;
                }
                else
                end = true;
        }
        cout << endl;
        score.close();

        return 0;
}


void bubble_sort(int scores[], int size)

{
        bool swaps = true;
        int j = 0, tmp;

        while(swaps)
        {
        swaps = false;
        j++;
        for(int i = 0; i < size - j; ++i)
                {
                if (scores[i] > scores[i + 1])
                        {
                        tmp = scores[i];
                        scores[i] = scores[i + 1];
                        scores[i + 1] = tmp;
                        swaps = true;
                        }
                }
        }
}


double median(int scores[], int n)

{
        double median;

        if(n % 2 == 0)
        {
                median = (scores[n / 2] + scores[n / 2] -1) / 2.0;
        }
        median = scores[n / 2];
}


void output(int scores[], int size)
{
        for (int count = 0; count < size; count++)
        cout << scores[count] << " ";
        cout << endl;
        cout << bubble_sort << endl;
        cout << "The median is: " << median << endl;
}
Thank you MiiNiPaa. I have everything working now except for the 25th and 75th percentile median. I'm not 100% sure how to code it in my median function. Any help or direction is appreciated! Thank you.

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
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>


using namespace std;



void bubble_sort(int scores[], int size)

{
        bool swaps = true;
        int j = 0, tmp;

        while(swaps)
        {
        swaps = false;
        j++;
        for(int i = 0; i < size - j; ++i)
                {
                if (scores[i] > scores[i + 1])
                        {
                        swaps = true;
                        tmp = scores[i];
                        scores[i] = scores[i + 1];
                        scores[i + 1] = tmp;
                        //cout << scores[i] << " ";
                        }
                }
        }
}


double median(int scores[], int n)
{
        if(n % 2)
                return scores[n / 2];
        else
                return(scores[n / 2] + scores[n / 2 - 1]) / 2.0;
}


void print(int scores[], int n, int num_across)
{
        int cc = 0;
        cout << "The file after sort: " << endl;
        for(int k = 0; k < n; ++k)
        {
                cout << setw(10) << scores[k];
                ++cc;
                if(cc % num_across == 0) cout << endl;
        }
        cout << endl;

}


int main()
{
        ifstream score;
        int scores[100];
        score.open("scores.dat");
        int i(0), temp;
        int x = 0;
        bool end = false;
        cout << "Integers in file before sort: " << endl << endl;
        while(end == false)
        {
                score >> temp;
                if (!score.eof())
                {
                        scores[i] = temp;
                        cout << scores[i] << " ";
                        if(scores[i] != 0)
                        {
                                x = x + 1; //to determine number of elements in file
                        }
                        i++;
                }
                else
                end = true;
        }
        score.close();

int size = x;

bubble_sort(scores, size);
        for(int i = 0; i < size; ++i)
        // cout << scores[i] << endl;
        cout << endl;

int n = x;

double med = median(scores, n);
cout << "\nThe 50% median of the sorted file = " << med << endl;
cout << endl;


print(scores, n, 10);
cout << endl;

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