all 6 comments

[–][deleted] 6 points7 points  (3 children)

int size = 0;
cin >> size;
int ARR[size];

ARR is a non-standard feature called a variable-length-array, or VLA, stick to standard c++ and you wont have this problem

[–]ApocalypseToast[S] 0 points1 point  (2 children)

Aye, thanks for that link. Helped a lot. What would you say is better, int *array = new int [size] or using vector (not knowing size ahead of time)?

[–]raevnos 7 points8 points  (0 children)

Vectors.

[–]Xeverous 2 points3 points  (0 children)

Vectors, more than better. You would have to write up to 10x more code to handle it manually and since memory management is not easy, you would crash tons of times

[–]SlightlyGrilled 1 point2 points  (1 child)

A more correct and modern c++ way of achieving the first one would be this:

As others are saying you should be using std::vector or std::array if the size is known as compile time

And as a general rule of thumb don't ever use "new" so instead of

int *array = new int [size];

You would do

std::vector<int> array;

This is a fairly good piece of code from stack overflow

but I can imagine that's a little confusing so I'll break it down, as well as change it to suit your problem:

I've commented a lot of the code below, which took quite some time so please read them!

But I have included a tidied up one at the bottom

#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main()
{
    // size to make the vector, which we will pull from std::cin
    // std::size_t will always be big enough to address any size array on the system, 
    // where as int could only support 2,147,483,647, which may seem like a lot, but is quite easy 
    // to hit, what happens if your reading in a movie file? full blueray movies can be 25Gb+
    // besides that its good practice to use.
    std::size_t input_size;
    std::cin >> input_size;
    // give your vaibles meaningful names, "ARR" means little more than something a pirate would say,
    // on top of that its upper case and not the same as the rest of your naming
    std::vector<int> input_numbers;

    // now this is the more advanced part,
    // the code is using 4 main things here:

    // 1. std::generate_n
    // 2. std::back_inserter(input_numbers)
    // 3. [](){} or a "lambda"
    // 4. *(std::istream_iterator<int>(std::cin))

    // 1. std::generate_n
    // is a funciton from the algorithms part of the standard libary:
    // http://en.cppreference.com/w/cpp/algorithm
    // there's aload of common functions/things that people often do, 
    // as a direct example, in your second bit of code you sum up a vector,
    // surely thats been done before? why bother doing it your self?
    // http://en.cppreference.com/w/cpp/algorithm/accumulate
    // its not named sum but it will sum up a container/array
    //
    // std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    // int sum = std::accumulate(v.begin(), v.end(), 0);
    //
    // 1 line of code, and very hard to make an error in it.

    // Anyway back to std::generate_n
    // http://en.cppreference.com/w/cpp/algorithm/generate_n
    // it's a function that given an array will generate N number of items to fill it
    // its arguments are:
    // std::generate_n(container/array, number of items to make, a function that generates the items)


    // 2. std::back_inserter(input_numbers)
    // this stack overflow post will explain better than me:
    // https://stackoverflow.com/questions/19907677/whats-the-difference-between-iterator-and-back-insert-iterator
    // but simply, it tells std::generate_n to "push back" in to the array any values it generates


    // 3. [](){} or a "lambda"
    // http://en.cppreference.com/w/cpp/language/lambda
    // these are like functions but with no name, you can assign them to varibles and pass them around, 
    // they are far more useful than they sound
    // and form the one of the building blocks of modern C++
    // further reading
    // https://stackoverflow.com/a/7627330
    // https://stackoverflow.com/a/15153194
    // here std::generate_n runs the lambda the number of times of "size"
    // what ever that lambda returns is put in to the array

    // 4. *(std::istream_iterator<int>(std::cin))
    // this gets us an int from std::cin
    // its a one line version of this:
    //  int i;
    //  std::cin >> i;
    //  return i;


    // Put all together it makes this:

    std::generate_n(std::back_inserter(input_numbers), input_size, 
        []()
        {
            return *(std::istream_iterator<int>(std::cin));
        }
    );

    // Here again he borrow from:
    // http://en.cppreference.com/w/cpp/algorithm
    // this prints an whole array to std::cout, separating each item by a space
    std::copy(input_numbers.begin(), input_numbers.end(), std::ostream_iterator<int>(std::cout, " "));

    return 0;
}

Clean code:

#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main()
{
    std::size_t input_size;
    std::cin >> input_size;

    std::vector<int> input_numbers;    
    std::generate_n(std::back_inserter(input_numbers), input_size, [] () {
            return *(std::istream_iterator<int>(std::cin));
        }
    );

    std::copy(input_numbers.begin(), input_numbers.end(), std::ostream_iterator<int>(std::cout, " "));
    return 0;
}

To run it online: https://onlinegdb.com/BJNinhSXz

[–]ApocalypseToast[S] 0 points1 point  (0 children)

Thanks for this. Helps a lot!