Since it's not possible to assign values to a const after it's initialized, how can I achieve this?
const double input1[1000][2];
for(int i = 0; i < 1000; i++){
input1[i][0] = i;
input1[i][1] = i*2;
}
Since it's not possible to assign values to a const after it's initialized, how can I achieve this?
const double input1[1000][2];
for(int i = 0; i < 1000; i++){
input1[i][0] = i;
input1[i][1] = i*2;
}
You'll have to generate the explicit initialization (i.e. the { {0, 0}, {1, 2}, {2, 4}, ... etc etc ... {1000, 2000} }
.
You can do this with map/apply macros, or with your build system - having it run some script which generates that initialization sequence and plants it into the file.
You mentioned in the comments that this data is a training session data for a neural network.
I assume that you don't want to input 2000 values by hand and I assume that these values come from another file, perhaps a csv or the output of another program that generates the training data.
In that case, I'd write a script that generates a header file from the training data that looks like this:
// training_data.h
// trainng data, automatically generated by script gen_training_data.py
// do not modify this file
#ifndef TRAINING_DATA_H
#define TRAINING_DATA_H
const double traing_data[1000][2] = {
{ 0, 1 },
{ 0, 2 },
....
};
#endif
The script (that you can write with other languages, bash, python, perl) would take the training data and generate the header file for you, so you don't have to write 2000 values by hand. Then you can use it in your C program like this:
#include <stdio.h>
#include "training_data.h" // include auto generated file
int main(void)
{
...
feed_to_neural_network(training_data);
...
}
If you use a build system like cmake, you can let cmake execute your script so that it autogenerates the header file before compiling. That would save you on the long run a lot of time.
I don't understand why would you want to store the index value in the first index. But removing const
can solve your issue. But better thing to do would be, to just store the value instead of index. Like following:
double input1[1000];
for (int i = 0; i < 1000; i++)
{
input1[i] = i * 2;
}