1

Here is my solution. Is it correct? Ignore bits/stdc++.h etc. I just want to know if it only uses the space allocated for the vector.

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cout << "Size of array?\n";
    cin >> n;
    int x[n];
    for (int i = 0; i < n; i++) {
        cout << "Input x[" << i << "]:\n";
        cin >> x[i];
    }
    for (int i = 0; i < n; i++) {
        cout << x[i] << "; ";
    }
    for (int i = 1; i<n; i++) {
        swap(x[0],x[i]);
    }
    for (int i = 0; i < n; i++) {
        cout << x[i] << "; ";
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Voicu Vlad
  • 23
  • 4
  • 1
    If this works and compiles use codereview instead of stackoverflow please. – Hatted Rooster May 26 '21 at 13:08
  • 1
    To begin with your "array" is actually not valid because C++ doesn't really have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Use `std::vector` instead. – Some programmer dude May 26 '21 at 13:08
  • you cannot use dynamic array in stack. so above code is wrong. if you wnat dynamic array you should malloc or use new or use **std::vector** – N0ll_Boy May 26 '21 at 13:10
  • 1
    Also never use [``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), not even for simple examples. Both the VLA and the header are truly bad habits commonly exposed through so-called "competition" sites. And such sites are not meant for learning anything, unless all you want to learn are really bad habits(habits so bad they can actually make you unemployable). – Some programmer dude May 26 '21 at 13:10
  • *Ignore bits/stdc++.h etc.* -- Why couldn't you have simply put `#include ` and then `#include `? Then there wouldn't have been a need to mention anything about that header. – PaulMcKenzie May 26 '21 at 13:16
  • Your swap loop only accesses valid values in the `x` array. It's valid. – JohnFilleau May 26 '21 at 13:18
  • 2
    Maybe OT, but `` already has a function for rotating a range of elements: https://en.cppreference.com/w/cpp/algorithm/rotate – mediocrevegetable1 May 26 '21 at 13:22
  • @N0ll_Boy i didnt want a variable size array i want an array that only takes as much space as it needs – Voicu Vlad May 26 '21 at 13:22
  • @Some programmer dude i need to have an array of a size that the user inputs, that size will never change after that, should i use an input file then? – Voicu Vlad May 26 '21 at 13:25
  • @mediocrevegetable1 cant use built in algorithms – Voicu Vlad May 26 '21 at 13:26
  • @VoicuVlad `int n; ... int x[n];` isn't C++. GCC copied it from C, where it is called a "variable length array" – Caleth May 26 '21 at 13:28
  • 1
    `int x[n];` *is* a variable lenght array. Thats how they are called. See here: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). If you want an array with size determined only at runtime but fixed after construction, the simple way is to use a `std::vector` and not add or remove elements after construction – 463035818_is_not_an_ai May 26 '21 at 13:29
  • on the one hand you ask "Is this a correct solution?" but on the other hand the requirements are not clearly stated in the question. The "correct" way would not `#include ` and would use `std::rotate`. Did you test the code for same example cases? What more do you want to know? – 463035818_is_not_an_ai May 26 '21 at 13:31
  • @voicu-vlad then you should use standard library containers such as std::array, std::vector,etc they are handling size for you(but beware you said you want as much space as it needs so dont forget use reserve method on vector). – N0ll_Boy May 26 '21 at 13:44
  • thanks everyone, std::vector always seemed overkill for some simple problems but i'll start using it more frequently – Voicu Vlad May 26 '21 at 13:50
  • @largest_prime_is_463035818 if i would use x[10] and then use n to store a logical value would it be c++ – Voicu Vlad May 26 '21 at 14:16
  • imho using a compiler extension for something that is readily available in standard portable C++ is overkill. I don't understand your last comment. Arrays sizes must be compile time constants. You can do `const int x = 10; int x[10];` – 463035818_is_not_an_ai May 26 '21 at 14:18
  • @largest_prime_is_463035818 i wanted to mean if it is ok to declare my array of 10 elements and only use 6 elements and declare a variable (n) to store the number of used elements (6) to use in the algorithms i create ( for example the algorithm from the question wouldnt work with a array that is not fully used unless i tell it how many elements does the array actuslly store ( from a logic point of view, 10 would be the actual size and 6 the logical size) – Voicu Vlad May 26 '21 at 14:38
  • sure, you can do that. You are just wasting some memory and you need to handle the case of user entering something bigger than `10` for `n` to not access the array out of bounds. If you use a `std::vector` properly, both can be avoided completely. – 463035818_is_not_an_ai May 26 '21 at 14:40

1 Answers1

2

For starters variable length arrays like this

int n;
cout << "Size of array?\n";
cin >> n;
int x[n];

is not a standard C++ feature. Instead you should use the standard container std::vector<int>.

To rotate an array or an object of the type std::vector<int> you can use the standard algorithm std::rotate.

As for your code then if you want to rotate the array left then this loop

for (int i = 1; i<n; i++) {
        swap(x[0],x[i]);
}

should be written like

for (int i = 1; i<n; i++) {
        swap(x[i-1],x[i]);
}

Otherwise your loop rotates an array right.

Here is a demonstrative program.

#include <iostream>
#include <utility>
#include <vector>

int main() 
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };

    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    for ( size_t i = 1; i < v.size(); i++ )
    {
        std::swap( v[i-1], v[i] );
    }
    
    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    
    return 0;
}

The program output is

1 2 3 4 5 
2 3 4 5 1 

Here is a demonstrative program where there is used the standard algorithm std::rotate.

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

int main() 
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };

    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    std::rotate( std::begin( v ), std::next( std::begin( v ) ), std::end( v ) );
    
    for ( const auto &item : v )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    
    return 0;
}

The program output is the same as shown above

1 2 3 4 5 
2 3 4 5 1 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335