I need to implement an array of float of size N, but for which the first element always needs to be 1.0.
1 - I could write a code, making sure I do not ever modify array. But I was hopping of defining this first value using the const key word, so the compiler would warned me, if I was trying to edit this element.
2 - I could also create a Class for such object, but I was wondering if there is a simpler way.
you can make a class and then make an array of that class to do it, but this is serious bloatware. The class would overload the assignment operator and have a member for "read only". if read-only, the assignment operator would do nothing or fail loudly. Only the first element would have this flag set.
This however isnt an array of float; its an array of your class, so that violates the design already, even though your class can be made to look and behave exactly like a float and even be interchanged with one everywhere, its still technically not a float.
you can also use a pointer to hide the first element.
access can't get to the first location with positive index, so you can use it wherever you write to the array, but the index is off by one from the real array.
Personally I would go with option 3 which is:
do not store the 1.0. Whatever purpose it serves can be replaces with an array of float + a const that = 1.0 in your logic. You can tie the 2 together in a C struct:
This array is supposed to be the input layer or hidden layer of an artificial neural network. Mathematically, when you have N inputs, it is convenient to consider N+1 inputs with the first one equal to 1.
I won't go into details but this is the short version.
if you are doing a sum of products over it, I recommend option 4, just set it to 1 and don't change that location. Oddball logic would just slow your SOP code down and the last thing ANN code needs is slowness.