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.

6
  • int l[x]; is not legal c++. Commented May 23, 2019 at 18:11
  • 2
    Use a std::vector instead of arrays for this. Commented May 23, 2019 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? Commented May 23, 2019 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. Commented May 23, 2019 at 18:16
  • Partial duplicate of How to return an array from a function. Commented May 23, 2019 at 18:20

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 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
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]].
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
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.

1 Comment

Interesting, will further research about it.
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;
}

6 Comments

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 []
Prefer std::vector over std::unique_ptr for this unless you have a really good reason for the latter.
Point taken. I've added a version with std::vector.
Thanks for the explanation! Will take this into consideration and test it out.
How can I access the information inside any of your codes?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.