Reading and processing from text file to 2D integer array, with statements in between - c

I'm making a maze solver using Breadth-first search. Consider the following list of numbers in a text file
10 20
1 1
10 20
5 1
4 2
3 3
1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
Where the first row denotes a size of my maze (10x20), the second row denotes the starting position coordinates (1x1),and the third row denotes the ending position(10x20). Every row after the third row represents the coordinate where a block in the maze will be (aka will have to move around it).
Here's what this particular board will look like:
**********************
*s........*..........*
*........*...........*
*..*....*............*
*.*....*.............*
**....*..............*
*....*...............*
*...*................*
*..*.................*
*....................*
*...................e*
**********************
What I am trying to do:
If my text file has impossible coordinates for either the size or start/end coordinates, ignore those coordinates and continue processing input.
example:
10 0 => Invalid: Maze sizes must be greater than 0
15 7 => Maze becomes size 15 x 7
10 20 => Invalid: column 20 is outside range from 1 to 7
5 1 => Starting position is at position 5, 1
24 2 => Invalid: row 24 is outside of range from 1 to 15
3 3 => Ending position is at position 3, 3
1 10 => Invalid: column 10 is outside range from 1 to 7
2 9 => Invalid: column 9 is outside range from 1 to 7
3 8 => Invalid: column 8 is outside range from 1 to 7
4 7
5 6
5 1 => Invalid: attempting to block starting position
6 5
7 4
8 3
I know I'm supposed to use some fprintf or fscanf loop until the end of file is reached.
Can someone start me off in the right direction?
I want to print all coordinates in the file, with error messages further in the line, if necessary.

Is the problem you are trying to ask how to read all the points? If so you can do something like the following:
int n1, n2;
FILE * fp = fopen("myfile.txt", "r");
//...read first three lines and do what you need with them
//read rest of points
while( fscanf(fp, "%d %d", &n1, &n2) ) {
if (checkPoints(n1,n2)) // check points are valid
addPointsToBoard(n1,n2); // add to board
}
If you're asking how to implement something like checkPoints I'd say you haven't given enough information of how you plan to implement your code for someone to help
NOTE: This assumes you have a well formed input file. If you are concerned about invalid inputs you will need to do sanity checking
EDIT Based upon comment here is a way you can do a sanity check on the matrix size input (first line)
int valid_size = 0;
while(1) {
if ( fsanf(fp, "%d %d", &n1, &n2) )
valid_size = checkMatrixSizes(n1,n2);
else
exit(1); //never finding valid matrix size in file
if (valid_size)
break
}
The above loop will continuously loop until checkMatrixSizes finds valid sizes (I would also suspect it would create your board, etc. The above is pseudo code and far from complete). You could do similar loops for the second and third inputs. It should be noted that this code simply ignores any invalid input and moves on, which I think is the behavior you want based upon your question. Other behaviors might include adjusting the input to the closest acceptable value (i.e. if a column is out of range, set column to the highest possible value).

Related

How to process array data in chunks where last chunk may be a partial size

I have array of integer and I am trying to send this array as a sub block from esp32 to another one.
According this code I get on output like this:
output:
1 2 3 4 5
6 7 8 9 10
11 12 0 0 0
the expected output:
1 2 3 4 5
6 7 8 9 10
11 12
How can I update on esp_now_send to get like the expected output? how can I deal with the last sub block if it is less than 5 numbers?
The code needs to send up to only the available data. To do that the general approach would be to send full sub-blocks until the last sub-block which may be a partial one. That can be determined by simple maths logic to work out how much the current iteration should send based on how much data is left.
The code changes would be:
Change siz to be the real number of entries in the array: siz = sizeof(data)/sizeof(data[0]).
Change rang in the function call to `(ind + rang <= size ? rang : size - ind)``. That is, the size passed to the function call depends on how much data is left.

HeIp understanding Fibonacci Search

On the internet I only find code for the algorithm but I need understand in form of text first because I have trouble understand things from code only. And other description of the algorithm are very complicated for me (on Wikipedia and other sites).
Here is what I understand for far:
Let say we want search in array the element 10:
Index i 0 1 2 3 4
2 3 4 10 40
Some fibonacci number here:
Index j 0 1 2 3 4 5 6 7 8 9
0 1 1 2 3 5 8 13 21 34
First thing we do is find fibonacci number that is greater-equal to array length. Array length is 4 so we need take fibonacci number 5 that is in index position j=5.
But where we divide the array now and how continue? I really don't understand it.. Please help understand for exam...
The algorithm goes in the following way:
The length of the array is 5, so the fibonacci number which is greater than or equal to 5 is 5. The two numbers which are preceding in the Fibonacci sequence are 2 [n-2] and 3 [n-1] - (2, 3, 5).
So, arr[n-2] i.e. arr[2] is compared with the number to be searched which is 10.
If the element is smaller than the number, then the sequence is shifted 1 time to the left. Also, the previous index is saved for next iteration to give an offset for the index. In this case, since 4 is smaller, n-2 becomes 1 (1, 2, 3). arr[1 + 2(prev)] = arr[3] = 10. So, the index of the number is 3.
If the element is larger, the sequence is shifted 2 times to the left.
Always the min(n-2+offset,n)th element is compared with number to get the matching result.

Accesing two different rows simultaneously in C

Suppose I have a data set arranged in the following way
19 10 1 1
12 15 1 1
13 12 4 5
10 5 2 3
...
and so on, at a particular iteration in a for loop I have to read only the 1st and the 4th row and in the next iteration I have to access some other set of rows,for example
1st iteration:
1st row: 19 10 1 1
4th row: 10 5 2 3
i will access my data using the fscanf() function. But how will i ensure that I choose only the 1st and 4th rows or any two rows for that matter at a given iteration?
(I have not considered reading it into a 2D array since the size of data set is 10^8 )
Thank you.
As you read through your data (say, stored in a standard file), get byte offsets for rows by looking for row delimiters (a newline character). You can then read out rows based on the start and end byte offset with C pointer arithmetic on a FILE * and fseek(). Storing a few byte offsets (an eight byte long or equivalent, often) is cheap.

Matlab : Matrix indexing Logic

i am doing very simple Matrix indexing examples . where code is as give below
>> A=[ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ]
A =
1 2 3 4
5 6 7 8
9 10 11 12
>> A(end, end-2)
ans =
10
>> A(2:end, end:-2:1)
ans =
8 6
12 10
here i am bit confused . when i use A(end, end-2) it takes difference of two till first column and when there is just one column left there is no further processing , but when i use A(2:end, end:-2:1) it takes 6 10 but how does it print 8 12 while there is just one column left and we have to take difference of two from right to left , Pleas someone explain this simple point
The selection A(end, end-2) reads: take elements in last row of A that appear in column 4(end)-2=2.
The selection A(2:end, end:-2:1) similarly reads: take elements in rows 2 to 4(end) and starting from last column going backwards in jumps of two, i.e. 4 then 2.
To check the indexing, simply substitute the end with size(A,1) or size(A,2) if respectively found in the row and col position.
First the general stuff: end is just a placeholder for an index, namely the last position in a given array dimension. For instance, for an arbitrary array A(end,1) will pick the last element in column 1, and A(1,end) will pick the last element in the first row.
In your example, A(end, end-2) picks an element in the last row two columns before the last one.
To interpret a statement such as
A(2:end, end:-2:1)
it might help to substitute end with the actual index of the last row/column elements, so this is equivalent to
A(2:3, 4:-2:1)
Furthermore 4:-2:1 is equivalent to the list 4,2 since we are instructing to make the list starting at 4, decreasing in steps of 2, up to (minimum) 1. So this is equivalent to
A([2 3],[4 2])
Finally, the following combination of indices is implied by A([2 3],[4 2]):
A(2,4) A(2,2)
A(3,4) A(3,2)

C: how to store integers by reading line by line with a random layout?

I need to read a file and store each number (int) in a variable, when it sees \n or a "-" (the minus sign means that it should store the numbers from 1 to 5 (1-5)) it needs to store it into the next variable. How should I proceed?
I was thinking of using fgets() but I can't find a way to do what I want.
The input looks like this:
0
0
5 10
4
2 4
5-10 2 3 4 6 7-9
4 3
These are x y positions.
I'd use fscanf to read one int at a time, and when it's negative, it is obviously the second part of a range. Or is -4--2 a valid input?

Resources