-7

I want to create the program to reverse the array. But I am getting an error that storage size of array a[] is not known.

#include<iostream>
using namespace std;

int main()
{
    int a[];
    int b, c;
    cin >> b;
    for (int i = 0; i < b; i++)
        cin >> a[i];
    for (c = b; c >= 0; c--)
        cout << a[c] << endl;

    return 0;
}
JeJo
  • 26,381
  • 6
  • 42
  • 81

4 Answers4

3

i want to create the program to reverse the array

Simply use std::vector for that.

A good starting would be:

#include <iostream>
#include <vector>

int main()
{
    std::size_t size;
    std::cin >> size;        // size of the array    
    std::vector<int> a(size);// allocate memory for provided size and 
                             // initialize with 0's
    // using range based loop iterate though referances of
    // each elements in the array and update with user input.
    for (int& element : a) std::cin >> element;

    return 0;
}
πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
JeJo
  • 26,381
  • 6
  • 42
  • 81
1

Extension to JeJo's answer:

The latter is simple, elegant and efficient – for any type similarly simple as int. If you are dealing with more complex types, though, then with this approach you'd first default-initialise all elements and then copy- or move-assign the final objects, which you would most likely want to avoid. In such a case, the following approach is superior:

std::vector<SomeComplexType> v;
v.reserve(NumberOfObjectsYouNeed);

// appropriate loop definition here!
{
    v.emplace_back(/* the parameters you want to/must provide to constructor */);
}
Aconcagua
  • 22,474
  • 4
  • 33
  • 56
1

You are not defining a size for the a[] array, thus the error message. Arrays must have a size specified. In your case, you need to use the new[] operator to allocate the array after you determine that size from the user, eg:

#include <iostream>

int main() {
    int *a;
    int b;

    std::cin >> b;
    a = new int[b];

    for(int i = 0; i < b; ++i)
        std::cin >> a[i];

    for(int c = b - 1; c >= 0; --c)
        std::cout << a[c] << std::endl;

    delete[] a;
    return 0;
}

However, the preferred way to use a dynamically sized array in C++ is to use the standard std::vector container instead, eg:

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

int main() {
    std::vector<int> a;
    int b;

    std::cin >> b;
    a.reserve(b);

    std::copy_n(
        std::istream_iterator<int>(std::cin), n,
        std::back_inserter(a)
    );

    std::for_each(a.rbegin(), a.rend(),
        [](int i){ std::cout << i << std::endl; }
    );

    /* alternatively:
    std::reverse(a.begin(), a.end());
    for(int i : a)
        std::cout << i << std::endl;
    */

    return 0;
}
Ted Lyngmo
  • 60,763
  • 5
  • 37
  • 77
Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
-2

You need to must assign a maximum size of the array Just try this by assigning a length of an array Ex Int a[5];

  • 5
    Not *that* simple - you impose a maximum length for the array this way, and user might input a larger number! So you then need to check this case, too, and provide some appropriate error handling. – Aconcagua Feb 06 '19 at 17:52
  • 1
    Look at the answers using a `std::vector` instead. – Ted Lyngmo Feb 06 '19 at 17:57
  • @Aconcagua can u suggest me a good programming book to learn programming in c++ or any online free course? – Shivam Sharma Feb 06 '19 at 17:58
  • @ShivamSharma Have a look at [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Aconcagua Feb 06 '19 at 18:06