Simple array issue

Hello, I am to sort an array. I want to call this function to auto-input these preset integers into an array as an option. I am receiving an error and unsure why it's not working, how would this be fixed?

1
2
3
4
5
6
7
  int autoInput(int array[], int size)
{
    size = 20;
    array[size] = {3, 47, 45, 27, 21, 9, 36, 33, 8, 5,
    41, 26, 20, 37, 6, 11, 44, 42, 32, 28};
    return array[size];
}


warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
error: cannot convert β€˜<brace-enclosed initializer list>’ to β€˜int’ in assignment
You're trying to add an array to the 21st element in the array. Then you try and return the 21st element in the array. You also overwrite the size with an arbitrary 20.
So I guess you can only assign using this method when declaring the array? How would you best assign these to an already declared array?
You're going to want to use a loop. Something like:
1
2
3
4
5
6
int const preset[] = {3, 47, 45, 27, 21, 9, 36, 33, 8, 5,
    41, 26, 20, 37, 6, 11, 44, 42, 32, 28};
for(int i = 0; i < size; ++i)
{
    array[i] = preset[i];
}
I'm a bit confused on line 3 and 6 though.
Thanks I'll try that out!

Line 3 was added here just to show you that size is indeed 20 but I guess it didnt matter. I'm not sure why line 6 wouldn't work but i'll believe you. My other function for example returned the entire array fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int sortArray(int array[], int size)
{
    int newValue;
    for(int i=1; i < size; i++)
    {
        newValue = array[i];
        int j;
        for(j=i-1; array[j]>newValue; j--)
        {
            array[j+1] = array[j];
        }
        array[j+1] = newValue;
    }
    return array[size];
}
It returns an arbitrary value because array[size] is out of bounds. When you pass an array to a function it is turned into a pointer and when you pass by pointer or reference you can modify the value directly. That is why the array being passed to the function is modified outside of the function.
Thanks giblit, you've been much help.
So I have been working off and on trying to figure out why the autoInput function route doesn't work. Program ends after outputting "Unsorted array: " and run fails while trying to output the predetermined values. The user input route works fine. Any other input would be appreciated, sorry for lack of comments.

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

void instructions();
void inputSelf(int[], int);
void autoInput(int[], int);
void sortArray(int[], int);


int main()
{
    int size;
    char decision;
    int array[size];
    
    instructions();
    
    cout << "Would you like to input your own integers? (Y for yes): ";
    cin >> decision;
    
    if(decision == 'Y' || decision == 'y')
    {
        cout << "Enter amount of integers to be sorted: ";
        cin >> size;
        inputSelf(array, size);
    }
    else
    {
        size = 20;
        autoInput(array, size);
    }
    
    cout << "Unsorted array:" << endl;
    for(int i=0; i < size; i++)
    {
        cout << array[i] << " ";
    }
    cout << endl;
    
    sortArray(array, size);
    
    cout << "Sorted array:" << endl;
    for(int i=0; i < size; i++)
    {
        cout << array[i] << " ";
    }
    cout << endl;
    
    return 0;
}


void instructions()
{
    cout << "This program will sort integers in order for you." << endl
            << "You may enter your own numbers or use predesignated integers." << endl;
}


void sortArray(int array[], int size)
{
    int newValue;
    for(int i=1; i < size; i++)
    {
        newValue = array[i];
        int j;
        for(j=i-1; array[j]>newValue; j--)
        {
            array[j+1] = array[j];
        }
        array[j+1] = newValue;
    }
}

void inputSelf(int array[], int size)
{
    cout << "Please input " << size << " integers: ";
    for (int i=0; i < size; i++)
    {
        cin >> array[i];
    }
}

void autoInput(int array[], int size)
{
    int const preset[] = {3, 47, 45, 27, 21, 9, 36, 33, 8, 5,
    41, 26, 20, 37, 6, 11, 44, 42, 32, 28};
    for (int i=0; i < size; i++)
    {
        array[i] = preset[i];
    }
}
1
2
    int size;
    int array[size];
this is not valid c++. To do it this way you must do it like so:
1
2
3
4
5
6
int size = 0;

int *array = nullptr;

std::cin >> size;
array = new int[size];
If you don't want a dynamic array you can always make it a static array like:
1
2
std::size_t const size = 20;
int array[size] = {0};


Another thing to mention is your sort function looks very odd and I think has some problems.
Topic archived. No new replies allowed.