Make it easy

Is there an easier way of doing this where I can seclude the const int s(10); part and still accomplish the same result? The task is to create a program with the smallest amount of code inside 'main'

1
2
3
4
5
6
7
8
9
10
int main()
{
    const int s(10);
    int v[s], i;

    for(i = 0; i < s; i++)
    {
        cin >> v[i];
    }
}
Hasimatsu wrote:
The task is to create a program with the smallest amount of code inside 'main'

Yeah that's soooooo easy...

Edit: Ok so I've editted the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 const int s(10);
int v[s];

int* func(void)
{
    int i;

    for(i = 0; i < s; i++)
    {
        cin >> v[i];
    }
    return v;
}

int main()
{
     func();
}
Last edited on
Does it have to be an int* pointer, and why is that? If I want to put the func(void) in a class using mulitple files, should I use a *.h file to include or a *.cpp file included in a *.h file?

Or can I use just the *.h file for my class and put all my functions in it?
Does it have to be an int* pointer, and why is that?


He's attempting (erroneously) to return the array. The array ceases to exist when the function returns, so the pointer it returns isn't worth much.

The short answer is: yes, you can move the constant out of main.

1
2
3
4
5
6
7
8
9
10
11
const int s = 10 ;

int main()
{
    int v[s], i;

    for(i = 0; i < s; i++)
    {
        cin >> v[i];
    }
}
Topic archived. No new replies allowed.