0

I'm a newbie in C++, I was learning to code in python. I believe the solution is simple but I have no idea how to do it.

Here is what I was trying to do in C++ (not working):

int createBoard(int x, int y) {
    int l[x];
    int board[y, l[x]];
    return board;
}
int main() {
    int x = 5;
    int y = 6;
    board = createBoard(x,y);
    return 0;
}

Here is what I wanted to replicate (working, but in python):

def createBoard(x,y):
    length = [i for i in range(0,10)]
    area = [y,length]
    return area

area = createBoard(5,6)

Basically I want to create a function that returns an array with the y value and an array counting until x.

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • `int l[x];` is not legal c++. – drescherjm May 23 '19 at 18:11
  • 2
    Use a `std::vector` instead of arrays for this. – NathanOliver May 23 '19 at 18:12
  • 1
    Assuming your compiler allows `int l[x];` through some extension, what do you expect `l[x]` to evaluate to in the following line? – François Andrieux May 23 '19 at 18:13
  • An additional problem is the return type `int createBoard(int x, int y) {` you promise to return a single integer. Nothing more. Although with that said you cant return a c array. You can return a `std::vector`. – drescherjm May 23 '19 at 18:16
  • Partial duplicate of [How to return an array from a function](https://stackoverflow.com/questions/4264304/how-to-return-an-array-from-a-function). – François Andrieux May 23 '19 at 18:20
  • Thanks for all the answers! @NathanOliver could you please further explain how to use std::vector? François Andrieux no I don't have any extension, didn't even knew that existed in C++, you can see what I wanted in the python example, it is hard to explain. – Gustavo Machado May 23 '19 at 22:49

3 Answers3

4

As far as I understood from your Python code, you want to create a 2D array. For a complete beginner in C++ that might be a challenging task. Many recommend to use std::vector and they are right but 2D "array" using such container could be very slow. So this example will work but undesirable in the future case when you gain more experience in C++:

#include <vector>

std::vector< std::vector<int> > createBoard(size_t x, size_t y)
{
    return std::vector< std::vector<int> >(x, std::vector<int>(y));
}

So if you want to use a more efficient way of creating 2D arrays, see this example: LINK

aikhs
  • 949
  • 2
  • 8
  • 20
  • But this creates `x` copies of the vector. The Python programs creates an array with two elements, the first being an integer and the second being an array. – oz1cz May 23 '19 at 18:59
  • @oz1cz No it does not. You create a pointer which it will dynamically allocate memory in heap for you. I do not know how Python handles inside but I am pretty sure it does the same under the hood – aikhs May 23 '19 at 19:04
  • I think you're wrong. If you run the Python code and then print the value of `area`, you get this output: `[6, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]`. – oz1cz May 23 '19 at 19:14
  • From what I could understand from the code above it appears to solve the issue in an interesting manner, will test it out and give feedback later on, thanks – Gustavo Machado May 23 '19 at 22:55
3

Translating code line by line is almost guaranteed to fail. You better do it in two steps: 1) fully understand what code in language A does. 1a) forget about the code in language A. 2) Write the same in language B.

I am not very proficient with python so I start from this:

Basically I want to create a function that returns an array with the y value and an array counting until x.

You declared the function to return a single int. A single int is not two arrays.

Next, this

int l[x];

Is not standard c++ because x is not a compile time constant. Some compilers offer it as an extension, but there is no reason to use it because c++ has std::vector.

Then, this

int board[y, l[x]];

is problematic in multiple ways. l[x] is accessing an element in the array l that is out of bounds. Valid indexes are 0 till x-1 because l has x elements. Accessing the array out of bounds is undefined behaviour. We could stop at this point, because in the presece of undefined behaviour anything can happen. However, y, l[x] invokes the comma operator. It evaluates both sides and results in the right operand. Then you again have the same problem, l[x] is no compile time constant.

In this place I had code in c++, but it turned out that I completely misunderstood what your code is supposed to do. I'll leave the answer and refer you to others for the solution.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

There are several problems with your code. The main one is that the Python array area contains objects of two different types: The first is the integer y, the second is the array length. All elements of a C++ array must have the same type.

Depending on what you want to use it for, you can replace the board array with a std::pair. This is an object containing two elements of different types.

Also, in C++ arrays with non-constant lengths must be dynamically created. Either using the new operator or (better) using std::unique_ptr. (Or you might want to use std::vector instead.)

Here's a small C++ program that does something like what you want to do:

#include <utility>
#include <memory>

auto createBoard(int x, int y) {
    return std::make_pair(y, std::make_unique<int[]>(x));
}

int main() {
    auto board = createBoard(5,6);

    return 0;
}

(This will only work if your compiler supports C++14 or newer.)

But this is actually rather much above "newbie" level, and I doubt that you will find it very useful.

It would be better to start with a specification of what your program should do, rather than try to translate code from Python.

EDIT

Same code with std::vector instead of a dynamic array:

#include <utility>
#include <vector>

auto createBoard(int x, int y) {
    return std::make_pair(y, std::vector<int>(x));
}

int main() {
    auto board = createBoard(5,6);

    return 0;
}
oz1cz
  • 5,504
  • 6
  • 38
  • 58
  • I think your example is too complicated and unnecessary. Why use ```make_unique``` for such a simple task of making a 2D array? Second of all, OP won't be able to access board as he would in python by ```[]``` – aikhs May 23 '19 at 18:46
  • 2
    Prefer `std::vector` over `std::unique_ptr` for this unless you have a really good reason for the latter. – user4581301 May 23 '19 at 18:48
  • Point taken. I've added a version with `std::vector`. – oz1cz May 23 '19 at 19:02
  • Thanks for the explanation! Will take this into consideration and test it out. – Gustavo Machado May 23 '19 at 22:51
  • How can I access the information inside any of your codes? – Gustavo Machado May 23 '19 at 23:02
  • In both versions of my code you access the integer (5) as `board.first`, and you access an element in the array/vector as `bord.second[i]`. – oz1cz May 24 '19 at 06:42