Variable Used Before Initialized, But...

Nov 27, 2014 at 5:08am
OK, the problem is to write a program to create and populate an array with random numbers. Then, sort the numbers and output the array. The first half is done. I've created an array, and it populate it without issue. (Not without a little help here, by the way.)

The issue comes when I add my sort function. The first use of 'swap' is an assignment. That should initialize it. However, I get a debug error with the following text:
Run-Time Check Failure #3 - The variable 'swap' is being used without being initialized.

The code compiles without an error and the only warning I have is about the implicit conversion of a time_t value to integer. (This is from seeding the random number generator.)

Here is my code.

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
//randSort.cpp
//Generate 100 random numbers to populate an array
//Sort the numbers
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;

void sort(int array[]);
void output(int array[]);

int main(void)
{
    int large[1000];

    srand(time(NULL));

    for(int i = 0; i < 1000; i++)
    {
        large[i] = 1 + rand() % 1000;
    }

    output(large);
    cout << endl << endl;

    sort(large);

    output(large);
    cout << endl;

    return 0;
}

void sort(int array[])
{
    int i, j, swap;
    int n = sizeof(array);

    for(j = 0; j < 1000; j++)
    {
        for(i = 0; i < 1000; i++)
        {
            if(array[i] > array[i] + 1)
                swap = array[i];
                array[i] = array[i + 1];
                array[i + 1] = swap;
        }
    }

}

void output(int array[])
{
    for(int i = 0; i < 1000; i++)
    {
        if(i % 10 == 0)
            cout << endl;
        cout << setw(4) << array[i] << " | ";
    }
}
Last edited on Nov 27, 2014 at 5:30am
Nov 27, 2014 at 5:30am
You need to look over your if statement in the sort function and consider what's going on with it. :)
Nov 27, 2014 at 5:52am
Thanks, Ikaron. I had to sit and stare at it, but I finally realized I had forgot my brackets.

Now, can you see what I've done wrong with my bubble sort? It's not sorting.
Nov 27, 2014 at 5:54am
Already saw it. I say unto you once again, consider your if statement. ;)
Nov 27, 2014 at 6:07am
Ikaron, thank you again.

Simple placement of the + 1.
Nov 27, 2014 at 6:11am
You're welcome. :)

As a final note, you also have a bug concerning the last iteration of your sort loop (when i == 999) as you may notice after running it.
Nov 27, 2014 at 6:28am
Yep. I was getting a Run-Time Check Failure #2 - Stack around the variable 'large' was corrupted.

Fixed it by changing it to i < 999 and j < 999.

The question I have is if the array has 1000 values, wouldn't the largest value be 999?
Nov 27, 2014 at 6:32am
Yes, the last element in a 1000 element array would be 999.

Once again I direct you to consider what happens with the same if statement when i == 999.
Nov 27, 2014 at 6:40am
It looks for the value of "i + 1" and dies a horrible death.

Thanks. Now to figure this one out.
Nov 27, 2014 at 6:44am
My final code:
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
//randSort.cpp
//Generate 100 random numbers to populate an array
//Sort the numbers
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;

void sort(int array[]);
void output(int array[]);

int main(void)
{
    int large[1000];

    srand(time(NULL));

    for(int i = 0; i < 1000; i++)
    {
        large[i] = 1 + rand() % 1000;
    }

    output(large);
    cout << endl << endl;

    sort(large);

    output(large);
    cout << endl;

    return 0;
}

void sort(int array[])
{
    int i, j, swap;

    for(j = 0; j < 1000; j++)
    {
        for(i = 0; i < 1000; i++)
        {
            if(i = 999)
            {
                break;
            }
            if(array[i] > array[i + 1])
            {
                swap = array[i];
                array[i] = array[i + 1];
                array[i + 1] = swap;
            }
        }
    }

}

void output(int array[])
{
    for(int i = 0; i < 1000; i++)
    {
        if(i % 10 == 0)
            cout << endl;
        cout << setw(4) << array[i] << " | ";
    }
}


Last edited on Nov 27, 2014 at 6:44am
Nov 27, 2014 at 6:50am
Check that new if statement, common error in it. ;)

For someone learning their way around the basics you tackled those problems pretty quick. Keep it up.
Last edited on Nov 27, 2014 at 6:51am
Nov 27, 2014 at 7:05am
I caught that one quick.

I think it's time to put this one to bed.

Thanks again.
Topic archived. No new replies allowed.