I'm new to arduino and it's programing language. I learnt that we can't have methods returning arrays, but we can use pointers as an alternative.
So I have this method:
byte SUM(byte A, byte B, bool Cyi, byte *sum, bool *Cyo)
{
bool d0;
bool d1;
for(int i = 0; i < 8; ++i)
{
d0 = bitRead(A,i);
d1 = bitRead(B,i);
bitWrite(*sum,i,d0 ^ d1 ^ Cyi);
*Cyo = d0 && d1 || Cyi && (d0 ^ d1);
Cyi = Cyo;
}
}
I'am new to pointers but I guess the problem here is that bitWrite method does not accept a pointer? I don't know what do to next and I need some help.
Thanks in advance.
Adruino is not a programming language but a microcontroler platform for which you could write code in c.
you wrote in your code:
Cyi = Cyo;
but Cyi is of type "bool" and Cyo a type of "bool *" correct it with
Cyi = *Cyo;
Related
recently I have been trying to make a Neural Network with an arduino library and I came across a library, that was quite literally, called Neural Network by George Chousos. I stumbled apon a couple of errors that I managed to fix quite simply, but then I got caught on the error of
sketch_sep22b:24:43: error: incompatible types in assignment of 'float*' to 'float [4]' outputs = NN.FeedForward(inputs[i]);
This is all of my code:
#include <math.h> // Include Math Library
#include <NeuralNetwork.h> // Include Neural Network Framework
const unsigned int layers[] = {4, 9, 4}; // First Value (Inputs), Second Value (Neurons / Hidden Layers), Third Value (Outputs)
float outputs[4] = {}; //Outputs Float
const float* /* Pointer */ inputs[1] = {1};
const float expectedOutputs[1] = {1}; //Training Values
void setup()
{
Serial.begin(9600);
NeuralNetwork NN(layers, NumberOf(layers));
for (int i = 0; i < 3000; i++)
{
for (int j = 0; j < NumberOf(inputs); j++)
{
for (int i = 0; i < NumberOf(inputs) - 1; i++)
{
outputs = NN.FeedForward(inputs[i]);
Serial.println(outputs[0], 7);
}
NN.print();
}
}
}
Edit:
The declaration for FeedForward is:
float *FeedForward(const float *inputs); // Moves Calculated outputs as inputs to next layer.
Okay, I looked at the link and the declaration. The first problem, and the one that's leading to the compiler error is the return type. The declaration is expecting output to be a pointer, not an array. So, changing the declaration of output to:
float* outputs; //Outputs Float
should fix the compiler error. I haven't tried it, but that looks to be the issue the compiler is catching. This will likely return a pointer to an array of 4 floats which you will need to deallocate later (using either free or delete[] depending on how the library allocated the memory) once you're done with it or it will create a memory leak.
As others have noted, your current declaration of input is attempting to access a fixed memory location (1) which leads to undefined behavior, so, you still need to address this as well. Since the library appears to be expecting an input with 4 float values, you should either give it an array with 4 float values declared at compile time, or you could dynamically allocate an array with 4 values at run time.
The first option looks like this:
const float inputs[4] = {1.0, 2.0, 3.0, 4.0};
The second option looks like:
float* input;
...
input = new float[4];
input[0] = 1.0;
input[1] = 2.0;
input[2] = 3.0;
input[3] = 4.0;
...
output = NN.FeedForward(input);
...
delete[] input;
I am trying to find a way of going through a char array with n amount of hexadecimal values i.e {0xA2, 0xE7, 0x5f, 0x1B, 0x11, 0x11, 0x00} and I want to try and do a XOR checksum through all of the items in it - to be more precise the A2 ^ E7 ^ 5F ^ 1B ^ 11 ^ 00 is 10.
Here is the bit of my code:
void MainWindow::checkSum(QByteArray *b)
{
qint16 b_len = b->length();
unsigned char xor = 0;
for ( int i = 0 ; i < b_len ; i ++ )
{
xor = xor ^ b[i];
}
}
I think my code should be doing the job however qt does not compile it and gives me the silly error of "2248: 'QByteArray::operator QNoImplicitBoolCast' : cannot access private member declared in class 'QByteArray'".
Any ideas on how to make it working?
You are using b as a pointer.
In order to use operator[] (and in general, all operator XX and assignment operators) you should use an object or a reference to an object, not a pointer.
You should modify your function to:
void MainWindow::checkSum(const QByteArray &b)
{
//Same code as yours
}
and your calling should be:
{
QByteArray ba;
//Fill ba.
//Now you should not pass a pointer
//checkSum(&ba) //This should be similar to your call.
checkSum(ba); //Now, there is no &.
}
Note: if you want to use opeartor[] with pointers, you should use a different sintax:
void MainWindow::checkSum(QByteArray *b)
{
//...
xor = xor ^ b->operator[](i);
}
I'm building a BigInt class from scratch in C++, but something is driving me nuts: my XOR isn't working properly, and I have no idea why. I was hoping someone could enlighten me. Below is a minimal working example:
class BigInt
{
private:
bool pos;
int size; // Number of binary digits to use
short compare(BigInt input);
public:
BigInt(long long input, int inSize) { pos = true; size = inSize; }
};
short BigInt::compare(BigInt input)
{
// Partial compare function for minimal working example
// Return:
// 1: (*this) > input
// 0: (*this) == input
// -1: (*this) < input
string a = (*this).toDecimal(), b = input.toDecimal();
bool c = (*this).size > input.size, d = (*this).pos ^ input.pos;
bool thispos = (*this).pos, inpos = input.pos;
bool xorpos = (thispos != inpos);
bool x = true, y = true;
bool z = x ^ y;
if ((*this).size > input.size || (*this).pos != input.pos)
return 1 - ((*this).pos ? 0 : 2);
else if ((*this).size < input.size)
return -1 + ((*this).pos ? 0 : 2);
return 0;
}
I have a breakpoint on the first if statement. Below is what I have on my watch list.
thispos true bool
inpos true bool
xorpos true bool
x true bool
y true bool
z false bool
Anyone know what's going on? I'd rather avoid kluging my if statement. I've never had a problem with such simple usage of my XOR.
As far as I can tell, there should be nothing wrong, but there's something about these values that just won't evaluate the way they're expected to.
Edit: Changed code to minimal working example.
Well, even though ^ is a bitwise xor operator, your initializations
bool thispos = (*this).pos, inpos = input.pos;
are required to convert the source values to bool type. Values of bool type are guaranteed to act as either 0 or 1 in arithmetic contexts. This means that
bool xorpos = thispos ^ inpos;
is required to initialize xorpos with false if both thispos and inpos were originally true.
If you observe different behavior, it might be a bug in your compiler. Integral-to-bool conversion might be implemented incorrectly or something like that.
Another opportunity is that someone "redefined" the bool keyword by doing something like
#define bool unsigned char
This will disable the proper bool semantics in the first pair of initializations and cause the bitwise nature of ^ to affect the result.
Why not simply do x != y? This is more consistent with your types as well.
I'm trying to write a tic tac toe game in C++, but whenever I run it I get an error message saying:
TicTacToe.cpp: In instantiation of ‘void copy_array(T*, T*) [with T = std::basic_string<char>]’:
TicTacToe.cpp:115:25: required from here
TicTacToe.cpp:93:3: error: no match for ‘operator*’ in ‘**(new_arr + ((sizetype)(((long unsigned int)i) * 8ul)))’
It points to this function:
86 template<class T>
87 void copy_array(T old_arr[], T *new_arr)
88 {
89 int size = sizeof(old_arr)/sizeof(old_arr[0]);
90 for(int i = 0; i < size; i++)
91 {
92 *new_arr[i] = old_arr[i];
93 }
94 }
An this piece of code:
114 string copy[9];
115 copy_array(board, copy);
Could anyone please explain to me what's causing the error and how to solve it?
That error message is quite strange, it appears to be printing out a transformed version of your code, but I'm pretty sure it's referring to this line:
*new_arr[i] = old_arr[i];
Remove the * and you should be fine.
Although of course it won't function as you expect. Your argument T old_arr[] is going to act exactly like T *old_arr. Notably, sizeof(), which you're relying on to give you the full size of the array, won't.
You could use the following template to get around this:
template<class T, int N>
void copy_array(T (&old_arr)[N], T *new_arr) {
for (int i = 0; i < N; i++) {
new_arr[i] = old_arr[i];
}
}
Or, if you can use C++11, you could switch to std::array<>, which knows its size.
change this:
*new_arr[i] = old_arr[i];
for this:
new_arr[i] = old_arr[i];
when you use the brackets c++ internally dereference the pointer.
This does a safer variation of what you want:
template<typename T, size_t size>
void copy_array(T (&old_arr)[size], T (&new_arr)[size])
{
std::copy(&old_arr[0], &old_arr[0] + size, &new_arr[0]);
}
It enforces that the size of the destination is the same as the size of the source. If you end up wanting to override that, you really should reconsider your design.
When I compile under g++ I get the following errors:
In function 'int search(int, int, int)':
1584:error: no match for 'operator=' in '* tt = & core.<anonymous union>::tt[((hash_stack[ply] >> 16) & 2047ul)]'
1584:error: note: candidate is:
118:note: tt_type& tt_type::operator=(const tt_type&)
118:note: no known conversion for argument 1 from 'tt_type*' to 'const tt_type&'
static int search(int depth, int alpha, int beta) {
int best_score = -INF;
int best_move = 0;
int score;
struct move *moves;
int incheck = 0;
struct tt_type *tt; //LINE 1584
int oldalpha = alpha;
int oldbeta = beta;
int i, count=0;
nodes++;
/* test for draw by repetition */
hash_stack[ply] = compute_hash();
for (i=ply-4; i>=board[LAST]; i-=2) {
if (hash_stack[i] == hash_stack[ply]) count++;
if (count>=2) return 0;
}
/*
* check transposition table
*/
*tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
if (tt->hash == (hash_stack[ply] & 0xffffU)) {
if (tt->depth >= depth) {
if (tt->flag >= 0) alpha = MAX(alpha, tt->score);
if (tt->flag <= 0) beta = MIN(beta, tt->score);
if (alpha >= beta) return tt->score;
}
best_move = tt->move & 07777;
}
Where I have previously defined
struct tt_type { //LINE 118
unsigned short hash; /* - Identifies position */
short move; /* - Best recorded move */
short score; /* - Score */
char flag; /* - How to interpret score */
char depth; /* - Remaining search depth */
};
The most important line in the error message is this:
118:note: no known conversion for argument 1 from 'tt_type*' to 'const tt_type&'
It essentially means that you are trying to assign a pointer to the reference.
Which in turn makes me think that changing * tt = & core.::tt[((hash_stack[ply] >> 16) & 2047ul)] in your code to * tt = core.::tt[((hash_stack[ply] >> 16) & 2047ul)] for deep copy or to tt = & core.::tt[((hash_stack[ply] >> 16) & 2047ul)] for shallow copy will solve the problem (depending on your perspective).
I suspect that your line 1584 is really this one:
*tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
*tt is of type struct tt_type. The RHS is of the form &..., so it's of some pointer type. You can assign a struct to a struct, or a pointer to a pointer, but you can't assign a pointer value to a struct (unless you've overloaded the assignment operator).
I haven't studied the code enough to understand it, but you probably want to change *tt = ... to tt = ....
*tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
You're trying to store a pointer into a variable that's not a pointer.
You need either
*tt = TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
to make a copy of one element of the array (this isn't going to work, since tt is not initialized)
or
tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
to make a pointer into the array.
Another way of writing the second version is
tt = TTABLE + ((hash_stack[ply]>>16) & (CORE-1));
In this line:
*tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
You're trying to assign a variable of type tt_type to another thing of some other type. I don't know what TTABLE is, but as a wild guess, try removing the & (the & would cause an error if TTABLE is an array of tt_types. You'd be trying to assign a tt_type* to a tt_type).
*tt = &TTABLE[/**/];
You're assigning your struct from a pointer. As clarified by no known conversion for argument 1 from'tt_type*' to 'const tt_type&' It cannot convert the tt_type* to a tt_type& to make the copy.
I don't know what TTABLE is, but I'd remove the & from it.