warning: uninitialized in the function.

hi. i am getting a warning in my program that says "small is uninitialized in the function" and the same thing for large. and im getting the wrong answer when i call the printlargestandandsmallest but i get the correct answer in the function itself. the warning are for this function too.
here is my program:
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
the largest, smallest and the range of the values.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int SIZE = 50;
void readthearrays (int score1[], int score2[], int score3[], int k);
void printarray (int nums[], int k);
void makenewarray (int score1[], int score2[], int score3[], int sumscores[] , int k);
void findlargesmallrange (int numb[], int n, int &, int &, int &);
void sort1array (int numb[], int n);
void printlargestandsmallest (int numb[], int n);

ifstream infile ("input.txt");
ofstream outfile ("output.txt");

int main()
{
    int score1[SIZE], score2[SIZE], score3[SIZE], sumscores[SIZE];
    int largest, smallest, howfarapart, large, small;

    for (int n = 17; n < 18; n++) {
    outfile << "n is " << n << endl;
    outfile << "here is the original data:" << endl;
    outfile << setw(12) <<"array1" << setw(12) << "array2" << setw(12) << "array3" << endl;

    readthearrays(score1, score2, score3, n);
    outfile << endl;

    outfile << "here is the score1 array:" << endl;
    printarray(score1, n);
    outfile << endl << endl;

    outfile << "here is the score2 array:" << endl;
    printarray(score2, n);
    outfile << endl << endl;

    outfile << "here is the score3 array:" << endl;
    printarray(score3, n);
    outfile << endl << endl;

    outfile << "here is the new array:" << endl;
    makenewarray(score1, score2, score3, sumscores, n);
    printarray(sumscores, n);
    outfile << endl << endl;

    findlargesmallrange(score1, n, largest, smallest, howfarapart);
    outfile<< "in the score1 array:" << endl;
    outfile << "largest is " << largest << endl;
    outfile << "smallest is " << smallest << endl;
    outfile << "they are " << howfarapart;
    outfile << " apart"<< endl << endl;

    findlargesmallrange(score2, n, largest, smallest, howfarapart);
    outfile << "in the score2 array:" << endl;
    outfile << "largest is " << largest << endl;
    outfile << "smallest is " << smallest << endl;
    outfile << "they are " << howfarapart << " apart" << endl << endl;

    findlargesmallrange(score3, n, largest, smallest, howfarapart);
    outfile << "in the score3 array:" << endl;
    outfile << "largest is " << largest<< endl;
    outfile << "smallest is " << smallest << endl;
    outfile << "they are " << howfarapart << " apart" << endl << endl;

    findlargesmallrange(sumscores, n, largest, smallest, howfarapart);
    outfile << "in the new array:" << endl;
    outfile << "largest is " << largest<< endl;
    outfile << "smallest is " << smallest << endl;
    outfile << "they are " << howfarapart << " apart" << endl << endl;

    sort1array (score1, n);
    outfile << "here is the score1 array in sorted array:" << endl;
    printarray (score1, n);
    outfile << endl << endl;

    sort1array (score2, n);
    outfile << "here is the score2 array in sorted array:" << endl;
    printarray (score2, n);
    outfile << endl << endl;

    sort1array (score3, n);
    outfile << "here is the score3 array in sorted array:" << endl;
    printarray (score3, n);
    outfile << endl << endl;

    sort1array (sumscores, n);
    outfile << "here is the sumscores array in sorted array:" << endl;
    printarray (sumscores, n);
    outfile << endl << endl;

    printlargestandsmallest (score1, n);
    outfile << "the smallest value in score1 sorted array is: " << small << endl;
    outfile << "the largest value in score1 sorted array is: " << large << endl << endl;

    printlargestandsmallest (score2, n);
    outfile << "the smallest value in score2 sorted array is: " << small << endl;
    outfile << "the largest value in score2 sorted array is: " << large << endl << endl;

    printlargestandsmallest (score3, n);
    outfile << "the smallest value in score3 sorted array is: " << small << endl;
    outfile << "the largest value in score3 sorted array is: " << large << endl << endl;

    printlargestandsmallest (sumscores, n);
    outfile << "the smallest value in sumscores sorted array is: " << small << endl;
    outfile << "the largest value in sumscores sorted array is: " << large << endl;
    }

    outfile.close();
    infile.close();
    return 0;
}
void readthearrays (int score1[], int score2[], int score3[], int k) {
    int j;

    for (j = 0; j < k; j++) {
        infile >> score1[j];
        outfile << setw(10) << score1[j];
        infile >> score2[j];
        outfile << setw(12) << right << score2[j];
        infile >> score3[j];
        outfile << setw(12) << right << score3[j] << endl;
    }
}

void printarray (int nums[], int k) {

    int j;
    for (j = 0; j < k; j++)
    {
        outfile << setw(5) << nums[j];

		if (j % 5 == 4)
			outfile << endl;
    }
}
void makenewarray (int score1[], int score2[], int score3[], int sumscores[], int k) {

    for (int j = 0; j < k; j++)
        sumscores[j] = score1[j] + score2[j] + score3[j];
}
void findlargesmallrange (int numb[], int n, int &large, int &small, int &apart) {
    large = numb[0];
    small = numb[0];

    for (int j = 0; j < n; j++) {

        if (large < numb[j]){
            large = numb[j];
        }
        if (small > numb[j]){
            small = numb[j];
        }
    }
    apart = large - small;
}
void sort1array (int numb[], int n) {
    int score;

    for (int j = 0; j < (n - 1); j++)
        for (int test = j + 1; test < n; test++)
            if (numb[j] > numb[test]) {
                score = numb[j];
                numb[j] = numb[test];
                numb[test] = score;
                }
    return;
}

void printlargestandsmallest (int numb[], int n) {

   int large, small;

   small = numb [0];
   large = numb [16];

   outfile << small << endl;
   outfile << large << endl;

   return;
}
Last edited on
It compiled for me, maybe you fixed it ?
I am not sure whether you program works now, but it could be improved quite a bit.

Line 22: Why have a for loop that runs once? Why the values of 17 & 18?

Line 45 & 48 - 52: You have a lot of these repeated, they should be part of the function that was called just prior. Always be on the look out for repeated code - it means there is probably a more elegant way of doing it.

Instead of having 3 arrays, have you considered having a struct that holds 3 ints, then have one array of the struct. You could do a lot of processing in 1 loop then. You could do all your processing in about 20 - 30 LOC rather than 180 LOC.

HTH
@samueladams. It complies because I get warnings and not errors but I'm getting the wrong answers when the main program calls the printlargestandsmallest function.
@theideasman. I think I put the loop there so each array has 17 numbers. My prof wants to have 17 numbers but it doesn't have to be a loop. And the repeated parts are part of the assignment and the three arrays. I don't know why but thats how my prof wants them. He likes to take off lots of points for little things.
I think I put the loop there so each array has 17 numbers


But that is not going to put 17 numbers into the array. The start & end conditions only help to determine how many times it loops, nothing else. If you want to put something into an array, you have to assign them explicitly.

If your prof wants 3 arrays that's is fine, but these bits aren't necessary:

1
2
3
4
    outfile << "largest is " << largest << endl;
    outfile << "smallest is " << smallest << endl;
    outfile << "they are " << howfarapart;
    outfile << " apart"<< endl << endl;


They are repeated throughout the code, and it is pointless. If I was the prof, I would take marks off for that. As I was saying, put them in the function. Same for the other functions. Can I suggest that you should be careful that your interpretation of the assignment doesn't lead you to writing inelegant code?

As far as your errors go, can you use the debugger in your IDE? Create a watchlist, and see how the values change, thereby deducing where it all goes wrong. That is what professionals do.

Also, what were the errors - post in full.

HTH
Last edited on
Your compiler is correct. On line 20, you declare large and small. On lines 93 and 94, you write their values to outfile. At no point between those two lines do you ever assign any values to them.

TheIdeasMan is correct - your loop will only run once, with n having the value 17.
when i take away the for loop and only put n=17 i get a lot of weird numbers and way more then 17 numbers. the only way the warnings go away are when i write infile >> small and infile >> large but the answers are still wrong.
Last edited on
What is the point of those variables? What did you intend to use them for? Because at the moment, you're not using them for anything.

What is the point of your loop at line 22? What did you intend it to do? Why did you make it a loop, and how many times did you intend it to run? Because at the moment, it only runs once, with n set to 17.

Last edited on
Topic archived. No new replies allowed.