An array with one const and many other variable

Dear all,

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

Best
TontonBaboum
I don't think you're expected to write a class for this. It's more likely that you're expected to write a single line of code that defines and initialises the array.
Sure, but can I do it in a way, that it is becomes impossible to change the value of array[0]
The requirement doesn't mention whether element 0 should me mutable or not, it just says the array should have element 0 set to 1.0.

You've introduced the notion of it being const, but that isn't the requirement as you've stated it.
Yes, my bad,
I need the first element to remain to the value 1. All other elements can be edited.
The c-style array (and C++ sequence containers) hold sequences of elements which are all of the same static type.

In an array T[N], all the elements of the array are of type T.
If T is float, then all elements are of type float;
if T is const float, then all elements are of type const float.


> I could also create a Class for such object, but I was wondering if there is a simpler way.

There is no simpler way;
either a class for the simulated array, or a class (union-like-class) for the elements of the array.
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.

float weird[100];
weird[0] = 1.0;
float *access = &weird[1];

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:

struct thing
{
const float one = 1.0;
float f[somesize];
};

If it is a sentinel value, use the array index for that, e.g. if index < 0 then (its 1.0 value time!)

or option 4: knowing what you know, just don't change array[0].

Last edited on
option 5: question the need.

The "array" is for some purpose. Does it really have to be a flock with one black sheep in it? Will it really be used in a way that allows only "arrayish" objects?

Question the design. When a design demands something overly complex and unorthodox, then the real problem might not be in the implementation.
Thank you, for the feedback.

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.
Last edited on
Topic archived. No new replies allowed.