I'm trying to set an array as an input parameter to a self-made function, in which I want to modify the values of the array. For that, I tried to set the input array in the definition of the function as a pointer, but gave me some trouble.
The part of the *.hpp file can be seen here:
void CrossWall(int, int, bool[]);
The part of the *.cpp file is the next one:
void NODE::CrossWall(int robot_x, int robot_y, bool done_checking[]){
if (((robot_x+1) > (current_map.CheckLength() - 1)) && !done_checking[3] ){
available_movements[3] = 0;
done_checking[3] = true;
}
if (((robot_x-1) < 0 ) && !done_checking[2]){
available_movements[2] = 0;
done_checking[2] = true;
}
if (((robot_y+1) > (current_map.CheckHeight() - 1)) && !done_checking[0]){
available_movements[0] = 0;
done_checking[0] = true;
}
if (((robot_y-1) < 0 ) && !done_checking[1]){
available_movements[1] = 0;
done_checking[1] = true;
}
}
The array I want to modify is the array of bools (the only one there).
I think I found a point of confusion:
won't do it because it isn't a pointer.
In fact, it IS a pointer: What is array to pointer decay?
Related
{"1":"value","data":[A,B,C]}
1 returns value.
data returns null.
Adding quotations "[A,B,C]" isn't an array anymore.
rapidjson::Value &arr = document["data"];
if( !arr.IsArray() || arr.Size() != 3 )
{
Return;
}
else
{
x = arr[rapidjson::SizeType(0)].GetDouble();
y = arr[rapidjson::SizeType(1)].GetDouble();
z = arr[rapidjson::SizeType(2)].GetDouble();
};
How should I properly handle the [bracketed array]?
I have quite a lot of objects without quotes. Can I handle the objects without quotes?
So I have a vector of pointers to a class I defined. I have a function that takes the 0 index of the pointer and returns it. After that I need to remove the data in that index then take the item in the last index of the vector and put it into the 0 index. As of right now I am just setting the pointers to NULL if I return them, and then I pushback the final object in the vector and finally pop it back. I am not sure if this method is the best way of solving my issue. Here is my code though:
Instrument* loanOut() {
for (int i = 0, i < library.size(), i++) {
if (library[i] != NULL) {
return library[i];
}
else {
return NULL;
}
}
library[0] = NULL;
library.push_back(library[library.size()]);
}
Algorithmically, this is what you are describing :
Object PopFrontAndReplace(std::vector<Object>& objects) {
if (!objects.size()) {return Object();}
Object o = objects[0];
objects[0] = objects.back();
objects.pop_back();
return o;
}
Does that answer the question?
The code is to read instructions from text file and print out graphic patterns. One is my function is not working properly. The function is to read the vectors of strings I've got from the file into structs.
Below is my output, and my second, third, and sixth graphs are wrong. It seems like the 2nd and 3rd vectors are not putting the correct row and column numbers; and the last one skipped "e" in the alphabetical order.
I tried to debug many times and still can't find the problem.
typedef struct Pattern{
int rowNum;
int colNum;
char token;
bool isTriangular;
bool isOuter;
}Pattern;
void CommandProcessing(vector<string>& , Pattern& );
int main()
{
for (int i = 0; i < command.size(); i++)
{
Pattern characters;
CommandProcessing(command[i], characters);
}
system("pause");
return 0;
}
void CommandProcessing(vector<string>& c1, Pattern& a1)
{
reverse(c1.begin(), c1.end());
string str=" ";
for (int j = 0; j < c1.size(); j++)
{
bool foundAlpha = find(c1.begin(), c1.end(), "alphabetical") != c1.end();
bool foundAll = find(c1.begin(), c1.end(), "all") != c1.end();
a1.isTriangular = find(c1.begin(), c1.end(), "triangular") != c1.end() ? true : false;
a1.isOuter = find(c1.begin(), c1.end(), "outer") != c1.end() ? true : false;
if (foundAlpha ==false && foundAll == false){
a1.token = '*';
}
//if (c1[0] == "go"){
else if (c1[j] == "rows"){
str = c1[++j];
a1.rowNum = atoi(str.c_str());
j--;
}
else if (c1[j] == "columns"){
str = c1[++j];
a1.colNum = atoi(str.c_str());
j--;
}
else if (c1[j] == "alphabetical")
a1.token = 0;
else if (c1[j] == "all"){
str = c1[--j];
a1.token = *str.c_str();
j++;
}
}
}
Before debugging (or posting) your code, you should try to make it cleaner. It contains many strange / unnecessary parts, making your code harder to understand (and resulting in the buggy behaviour you just described).
For example, you have an if in the beginning:
if (foundAlpha ==false && foundAll == false){
If there is no alpha and all command, this will be always true, for the entire length of your loop, and the other commands are all placed in else if statements. They won't be executed.
Because of this, in your second and third example, no commands will be read, except the isTriangular and isOuter flags.
Instead of a mixed structure like this, consider the following changes:
add a default constructor to your Pattern struct, initializing its members. For example if you initialize token to *, you can remove that if, and even the two bool variables required for it.
Do the parsing in one way, consistently - the easiest would be moving your triangular and outer bool to the same if structure as the others. (or if you really want to keep this find lookup, move them before the for loop - you only have to set them once!)
Do not modify your loop variable ever, it's an error magnet! Okay, there are some rare exceptions for this rule, but this is not one of them.
Instead of str = c1[++j];, and decrementing later, you could just write str = c1[j+1]
Also, are you sure you need that reverse? That makes your relative +/-1 indexing unclear. For example, the c1[j+1 is j-1 in the original command string.
About the last one: that's probably a bug in your outer printing code, which you didn't post.
I'm Java guy trying to solve discrete knapsack problem in c++. However, I'm having trouble with pointers. I have an object with a field
Item ** items;
representing array of items to choose from. I also created a method to add an item which works like insertion sort (at least I hope so).
void Knapsack::addItem(Item item) {
int k = itemCount - 1;
if (this->items[k] != NULL) {
return;
}
while (k > 0 && this->items[k - 1] == NULL) {
k--;
}
if (k == 0) {
this->items[0] = &item;
} else {
int i = 0;
while (i < k && item < *(this->items[i])) {
i++;
}
for (int n = k; n > i; n--) {
this->items[n] = this->items[n - 1];
}
this->items[i] = &item;
}
}
Later, in my main I invoke the method by
knapsack->addItem(*(new Item(values.at(0), values.at(1))));
values being a vector of ints. The method itself seems to work fine, however, debugger shows that everytime I invoke the method with new Item, the previous values already put in my array are set to the same values as the new item.
(ex. if items[0] has value of 5, and I invoke the method with an item valued as 10, the items[0] instantly is set to 10).
Why are the values overwritten? I am creating a new object everytime I invoke the method.
EDIT:
Problem was fixed by replacing
this->items[0] = &item;
this->items[i] = &item;
with
this->items[0] = new Item(item.getWeight(), item.getValue());
this->items[i] = new Item(item.getWeight(), item.getValue());
SECOND EDIT:
The answer shows better (and probably correct) way to do this. Now the function takes a pointer instead of an object.
void Knapsack::addItem(Item * item);
this->item[i] = item;
knapsack->addItem(new Item(values.at(0), values.at(1)));
You are storing a pointer to a temporary copy of an Item object in the array in your addItem function, once the function returns the temporary object will be destroyed and you will be left with an invalid pointer. Make sure to allocate an Item object on the heap and passing a pointer to your addItem function or just use a vector of type std::vector<Item> and save your objects in there.
Somewhere in my code I have the following:
_buttonStates[0] = _upButtonState; _buttonStates[1] = _leftButtonState;
is there a way to do this:
buttonStates[0] = true;
changing the variable _upButtonStates, instead of replacing it with the value 'true'?
I am doing the following:
for(int i = 0; i < sizeof(_buttons)/sizeof(_buttons[0]); i++){
if(_buttonStates[i] != digitalRead(_buttons[i])){
_stateChanged[i] = true;
_buttonStates[i] = digitalRead(_buttons[i]);
}
else{
_stateChanged[i] = false;
}
But later references to '_upButtonState' end up being outdated.
How do I change the value of a variable in the list instead of overwriting it when accessing it via its index?
You are passing the literal values in now; you want to store pointers (memory addresses of the variables) in the array instead.
Example