I'm using an Arduino to read a Char array from a file with the following format:
[File Description][\n]
[Col1 title],[Col2 title][\n]
[double1,1],[double1,2][\n]
[double2,1],[double2,2][\n]
[doublen,1],[doublen,2][\n]
what is the best way to separate out [double1,1]to[doublen,n] and represent them as a 2D array of n elements?
Also, is there any way to "pass up," the array such that it becomes accessible to another function that does not call the converting function? I understand a variable global could be created with a very large element allocation, but my value n could range between 2 and 300 and it seems like a waste of good dynamic memory to have that many unused spaces available in a global.
Here's a skeleton of the way I understand to do it so far any input on how I should change the structure or if there's anything I need to change
char input[300]={'H','e','l','l','o','\n','1','.','1',',','2','\n','3',',','4','\n'};
//this is an nx2 array and is read from a file.
void setup()
{
ParseChar(input);
foo(FileArr);
}
int[][] ParseChar(char[] input)
{
//find number of new lines in the input to initialize FileArr
int lines=0;
for (int i=0; i<sizeof(input); i++)
{
if(input[i]=='\n')
{
lines++;
}
}
//set up int array
int FileArr[lines][2];
//test to write to col 1 or col2 of array
int wline=0;
int num=-1;
//start reading for values
for (int i=0; i<sizeof(input); i++)
{
if(input[i]==',')
{
if(num>-1)
{
//change col
FileArr[wline,0]=num;
//protect from possible garbage?
num=-1;
}
}
else if(input[i]=='\n')
{
if(num>-1)
{
//change col
FileArr[wline,1]=num;
//protect from possible garbage?
num=-1;
}
}
else if(isDigit(input[i]))
{
//read until ',' or '\n'
while (isDigit(input[i]))
{
//read in digits
i++;
}
//num=[output of above loop]
}
i++;
}
//turn FileArr into a global variable so an unrelated function can call it
}
void foo (int[][] Arr)
{
//print Arr to serial port one line at a time
}
Edit: To make matters worse, I just realized that these values will be doubles with decimal points