Reputation: 51
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.
Upvotes: 0
Views: 120
Reputation: 5834
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;
}
Upvotes: 1
Reputation: 122840
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.
Upvotes: 3
Reputation: 979
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
Upvotes: 4