Weird Arrays

When I allocate the correct number of arrays, the program goes all weird. But when I allocate one extra spot, the program works correctly. However, I'm not sure why.

The purpose of the code is to arrange the numbers in numerical order.

For example, when I write int array[11], which is the correct number of arrays, the program goes weird. But when I I int write array[12], it works correctly. Any suggestions?

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

int main () 
{ 
	int array[12] = {14, 26, 343, 421, 54, 63, 7, 8, 922, 10, 1111}; 
	int biggest_index = 0; 
	int j = 0;
	int k = 0; 
	int x = 5; 
	bool again = true; 

		for (int k = 0; k < 12; k++)
		{
			for (int i = j; i < 12; i++)
			{	
				if (array[biggest_index] < array[i])
				{
					biggest_index = i;
				}

				if (i == 11)
				{
					int temp = 0; 
					temp = array[biggest_index];
					array[biggest_index] = array[j];
					array[j] = temp;
					biggest_index++; 
				}
			}
			j++; 
		}

	for (int i = 0; i < 12; i++)
	{
		cout << array[i] << endl;
	}

    return 0; 
}
Arrays start at index 0, so... array of 11 would be:

[0] = 14
[1] = 26
....
[9] = 10
[10] = 1111

Look at your For loops, do you notice they are going past the last index of your array?

Softrix,

Yea, good point about the for loop. Thank you for pointing that out.

Also, if I run the program with array[11], my program runs incorrectly. However if I run the program with array[12], it works fine. I know I allocate one extra spot more than I need. But if I don't do that, programs goes all weird.

Best regards,
Jae Kim
@jhkim3 - Were the for loops using < 12 when the array size was 11, or did you change all the 11s to 12s at the same time? (As written in your opening post, with array[12] and i < 12, everything looks fine.)

If you were using < 12 with an array of size 11, then you would be doing as Softrix pointed out: going (one) past the end of the array.

And if you were running your app on Windows then, due to the way local variables are placed on the stack, this extra element would have been where the variable biggest_int is located. So setting it (array[12]) would actually be setting the value of biggest_index.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

int main() {
    int array[11] = {14, 26, 343, 421, 54, 63, 7, 8, 922, 10, 1111}; 
    int biggest_index = 0;

    void* addr_of_element_11 = &array[11];
    void* addr_biggest_index = &biggest_index;

    cout << "addr of \"element\" one beyond end of array\n"
            "&array[11] = " << addr_of_element_11 << "\n"
            "\n"
            "addr of following int\n"
            "&biggest_index = " << addr_biggest_index << "\n"
            "\n";
    if(addr_of_element_11 == addr_biggest_index)
        cout << "addresses are the same!\n";
    else
        cout << "addresses are different!\n";

    return 0;
}


Output on my Windows PC

addr of "element" one beyond end of array
&array[11] = 0x23ff54

addr of following int
&biggest_index = 0x23ff54

addresses are the same!


Andy

PS If you run my code on C++ Shell the addresses do not agree, so it looks like Linux machines behave differently.

1
2
3
4
5
6
7
addr of "element" one beyond end of array
&array[11] = 0x70f4a57dc7fc

addr of following int
&biggest_index = 0x70f4a57dc7cc

addresses are different!

Last edited on
@Andy

Yes, I was going past one at the end of the array. However, my problem still remains, I think. So currently there are 11 elements in the my array[12]. If I wanted to allocate the exact amount of memory, I would set my array to array[11]. However, when I set my array to array[11], my program does not run properly. Yet, when I allocate for one extra room in my array, my code runs correctly. Any thoughts?

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

int main () 
{ 
	int array[12] = {14, 26, 343, 421, 54, 63, 7, 8, 922, 10, 1111}; 
	int biggest_index = 0; 
	int j = 0;
	int k = 0; 
	int x = 5; 
	bool again = true; 

		for (int k = 0; k < 11; k++)
		{
			for (int i = j; i < 11; i++)
			{	
				if (array[biggest_index] < array[i])
				{
					biggest_index = i;
				}

				if (i == 10)
				{
					int temp = 0; 
					temp = array[biggest_index];
					array[biggest_index] = array[j];
					array[j] = temp;
					biggest_index++; 
				}
			}
			j++; 
		}

	for (int i = 0; i < 11; i++)
	{
		cout << array[i] << endl;
	}

    return 0; 
}
So currently there are 11 elements in the my array


Incorrect. You have declared a 12 element array on line 9. It will never be more or less than this.
Last edited on
Any thoughts?

You're not handling biggest_index quite right!? (Have not looked for a solution...)

Output on my Windows PC (using MinGW GCC)

biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (13)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (12)
biggest_index is too big! (12)
biggest_index is too big! (13)
1111
26
29580436
922
421
63
54
14
7
343
8


Output of C++ Shell

biggest_index is too big! (11)
biggest_index is too big! (11)
biggest_index is too big! (11)
1111
922
421
343
63
54
26
14
10
8
7


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

int main () 
{
    // valid indices 0..10
    int array[11] = {14, 26, 343, 421, 54, 63, 7, 8, 922, 10, 1111}; 
    int biggest_index = 0; 
    int j = 0;
    int k = 0; 
    int x = 5; 
    bool again = true; 

    for (int k = 0; k < 11; k++)
    {
        for (int i = j; i < 11; i++)
        {
            // CHECK biggest_index
            if(biggest_index >= 11)
            {
                cout << "biggest_index is too big! (" << biggest_index << ")\n";
            }
            if (array[biggest_index] < array[i])
            {
                biggest_index = i;
            }

            if (i == 10)
            {
                int temp = 0; 
                temp = array[biggest_index];
                array[biggest_index] = array[j];
                array[j] = temp;
                biggest_index++; 
            }
        }
        j++; 
    }

    for (int i = 0; i < 11; i++)
    {
        cout << array[i] << endl;
    }

    return 0; 
}


Andy
Last edited on
@mutexe
@andywestken

Okay. I see where the problem is. Thank you so much for taking the time to answer my questions. Thank you!!!
Topic archived. No new replies allowed.