Unexpected array error

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>

using namespace std;

double* sort_array(double *array,int length)//Bubble sort function
{
        double* result = new double[100];
        int i,j;
        for(i=0;i<100;i++)
        {
            for(j=i;j<100;j++)
            {
                if(array[i]<array[j])
                {
                int temp=array[i]; //swap
                array[i]=array[j];
                array[j]=temp;


                }

            }

        }
        for (i=0; i<100;i++) result[i]=array[i];
        return result;
}

void printElements(double *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<100;i++)
        cout<<array[i]<<", ";
    cout << endl;
}

int main(void)

{
double select[100];
srand(time(0));
for(double i=0; i<250; i++) select[i]=rand()%251;
sort_array (select,100);
cout << "The random array of 100 doubles between 0 and 250 sorted in descending order are:"<< endl;
printElements(select,100);
}


I get an error in the main, the error being invalid types:'double [100] double' for array subscript, however when i change the double i=o to int i=0 it works fine??

The array must be of double numbers between 0 and 2560, and the function will sort these, but it only appears to function when int i=0?

Can anyone explain this?

thank you!
1
2
for( double i = 0; ... ) {
    select[ i ] = .. // <--- your error is here 

you cannot use floating point type as the index of the array, rather it must be an integral type
You have declared an array of 100 doubles, the integral in the brackets is not a double. Hence when you declare an variable to iterate through your array you declare it as the same type as the integral used to declare the array of doubles.

BTW - your code is going to result in an out of bounds error as you are going to go beyond the size of the array [100] as you will iterate through the array 250 times! The size of the array and the condition in the for loop need to be the same value or less, I would expect you meant it to be:

1
2
3
4
for (int i=0; i<100; ++i)
{
...
}
The type of data you're storing in the array is double.

However, you're not using i for data that will be stored in the array. You're using it as an index, to identify which element in the array you want to access. The index must be an integer, regardless of the type of data you're storing in the array.

I hope that's clear?
Last edited on
main errors are:
1. in sort_array you create a useless copy (result) of the original array. Return array instead.
2. in sort_array and printElements you have not used the length parameter.
3. the statement int temp=array[i]; //swap is wrong: the variable must be double, not int.
4. the statement for(double i=0; i<250; i++) is wrong: i must be of integral type, and also you will get an out of bounds because the select array has 100 elements, not 250.

If you have a C++11 compiler, you can rewrite your program as:
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
#include <iostream>
#include <cstdlib>
#include <ctime>

template<class T, size_t length>
T* sort_array(T (&array)[length])//Bubble sort function
{
	for (size_t i = 0; i < length; ++i)
	{
		for (size_t j = i; j < length; ++j)
		{
			if (array[i] < array[j])
			{
				T temp = array[i]; //swap
				array[i] = array[j];
				array[j] = temp;
			}
		}
	}

	return array;
}

template<class T, size_t length>
void printElements(T (&array)[length]) //print array elements
{
	for (size_t i = 0; i < length; ++i)
	{
		std::cout << array[i] << ", ";
	}
	std::cout << std::endl;
}

int main(void)
{
	const size_t array_size = 100;
	double select[array_size] = {0};
	srand(static_cast<unsigned int>(time(NULL)));
	for (size_t i = 0; i < array_size; ++i)
	{
		select[i] = rand() % 251;
	}
	sort_array(select);
	std::cout << "The random array of 100 doubles between 0 and 250 sorted in descending order are:" << std::endl;
	printElements(select);
	return 0;
}


Or you can use std::sort instead of you sort_array function:
replace
sort_array(select);
with
std::sort(std::begin(select), std::end(select), std::greater<double>());
including also
1
2
#include <algorithm>
#include <functional> 

1. in sort_array you create a useless copy (result) of the original array. Return array instead.

In fact, you don't need to return array at all. The contents of the memory that array points to are changed by the function to contain the sorted data, but the memory address that's stored in array isn't changed at all. The calling code will already see the new, sorted contents of array once the function has completed.

4. the statement for(double i=0; i<250; i++) is wrong: i must be of integral type, and also you will get an out of bounds because the select array has 100 elements, not 250.

One way to help prevent making mistakes like this is to declare a const variable equal to the array size you're using, and use that variable everywhere. So:

1
2
3
4
5
6
7
8
9
10
const int ARRAY_SIZE = 100;

// ...

        for(i=0; i<ARRAY_SIZE; i++)
        {
            for(j=i; j<ARRAY_SIZE; j++)
            {

// ...  


If you use ARRAY_SIZE everywhere, there's no danger of you accidentally using the wrong number. Plus, if you decide to change the array size, you just have to change it in one place, not in every loop you're written.

EDIT: An even better way is to use std::vector, rather than C-style arrays.
Last edited on
Thank you for the replies, I'm alot clearer now. Even with the errors pointed out, the program still functions perfectly without any changes other than having i as an int. Can anyone explain why this is please?

Here is the 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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>

using namespace std;

double* sort_array(double *array,int length)//Bubble sort function
{
        double* result = new double[100];
        int i,j;
        for(i=0;i<100;i++)
        {
            for(j=i;j<100;j++)
            {
                if(array[i]<array[j])
                {
                int temp=array[i]; //swap
                array[i]=array[j];
                array[j]=temp;


                }

            }

        }
        for (i=0; i<100;i++) result[i]=array[i];
        return result;
}

void printElements(double *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<length;++i)
        cout<<array[i]<<", ";
    cout << endl;
}

int main(void)

{
double select[100];
srand(time(0));
for(int i=0; i<250; i++) select[i]=rand()%251;
sort_array (select,100);
cout << "The random array of 100 doubles between 0 and 250 sorted in descending order are:"<< endl;
printElements(select,100);
}

Thank you for the replies, I'm alot clearer now. Even with the errors pointed out, the program still functions perfectly without any changes other than having i as an int. Can anyone explain why this is please?

Explain what? Why it functions perfectly? Or why i needs to be int?
Why it functions perfectly? I tried taking out the 'unused int length' and it has an effect on the programme compiling it seems?

Thanks
Well, you've changed printElements so that it uses the length argument - at line 36. So obviously, if you take it out, it won't compile.

You're still not using the argument of the same name in sortArray, so you could get rid of it there. Or you could use it in your loops instead of your hard-coded "100" everywhere.

Line 46 is still a big problem though. You're writing beyond the end of your array. You've completely ignored everything we've told you about that. It's pure luck that it isn't causing you any problems.

Why do you bother coming to us for advice, when you're going to ignore what we say?
Topic archived. No new replies allowed.