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

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?

Related

Taking x number integers and putting them into an array

I need to take x amount of input lines (x is specified by the user) then put them into a 2d array. Each line contains x amount of integers separated by spaces.
For example;
Input:
3
4 3 1
6 5 2
9 7 3
I need to take that input and put them into a 2d array, how do I do this?
Assuming your numbers are separated exactly by one space:
n = int(input('enter size'))
print([[int(i) for i in input().split(' ')]
for __ in range(n)])
Suppose you have your input stored in a file named 'input.txt'
n=2 #number specified by user
with open('input.txt', 'r') as file:
result = [[int(char) for char in lines.split(' ')]for lines in file.read().splitlines()[:2*n:2] ]

Integer compression method

How can I compress a row of integers into something shorter ?
Like:
Input: '1 2 4 5 3 5 2 3 1 2 3 4' -> Algorithm -> Output: 'X Y Z'
and can get it back the other way around? ('X Y Z' -> '1 2 4 5 3 5 2 3 1 2 3 4')
Note:Input will only contain numbers between 1-5 and the total string of number will be 10-16
Is there any way I can compress it to 3-5 numbers?
Here is one way. First, subtract one from each of your little numbers. For your example input that results in
0 1 3 4 2 4 1 2 0 1 2 3
Now treat that as the base-5 representation of an integer. (You can choose either most significant digit first or last.) Calculate the number in binary that means the same thing. Now you have a single integer that "compressed" your string of little numbers. Since you have shown no code of your own, I'll just stop here. You should be able to implement this easily.
Since you will have at most 16 little numbers, the maximum resulting value from that algorithm will be 5^16 which is 152,587,890,625. This fits into 38 bits. If you need to store smaller numbers than that, convert your resulting value into another, larger number base, such as 2^16 or 2^32. The former would result in 3 numbers, the latter in 2.
#SergGr points out in a comment that this method does not show the number of integers encoded. If that is not stored separately, that can be a problem, since the method does not distinguish between leading zeros and coded zeros. There are several ways to handle that, if you need the number of integers included in the compression. You could require the most significant digit to be 1 (first or last depends on where the most significant number is.) This increases the number of bits by one, so you now may need 39 bits.
Here is a toy example of variable length encoding. Assume we want to encode two strings: 1 2 3 and 1 2 3 0 0. How the results will be different? Let's consider two base-5 numbers 321 and 00321. They represent the same value but still let's convert them into base-2 preserving the padding.
1 + 2*5 + 3*5^2 = 86 dec = 1010110 bin
1 + 2*5 + 3*5^2 + 0*5^3 + 0*5^4 = 000001010110 bin
Those additional 0 in the second line mean that the biggest 5-digit base-5 number 44444 has a base-2 representation of 110000110100 so the binary representation of the number is padded to the same size.
Note that there is no need to pad the first line because the biggest 3-digit base-5 number 444 has a base-2 representation of 1111100 i.e. of the same length. For an initial string 3 2 1 some padding will be required in this case as well, so padding might be required even if the top digits are not 0.
Now lets add the most significant 1 to the binary representations and that will be our encoded values
1 2 3 => 11010110 binary = 214 dec
1 2 3 0 0 => 1000001010110 binary = 4182 dec
There are many ways to decode those values back. One of the simplest (but not the most efficient) is to first calculate the number of base-5 digits by calculating floor(log5(encoded)) and then remove the top bit and fill the digits one by one using mod 5 and divide by 5 operations.
Obviously such encoding of variable-length always adds exactly 1 bit of overhead.
Its call : polidatacompressor.js but license will be cost you, you have to ask author about prices LOL
https://github.com/polidatacompressor/polidatacompressor
Ncomp(65535) will output: 255, 255 and when you store this in database as bytes you got 2 char
another way is to use "Hexadecimal aka base16" in javascript (1231).toString(16) give you '4cf' in 60% situation it compress char by -1
Or use base10 to base64 https://github.com/base62/base62.js/
4131 --> 14D
413131 --> 1Jtp

What input operator can we use in C that ignores 'space' and accepts 'Enter' while taking array inputs

Consider an example:
5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1
Here, in the first line, 5 denotes the size of the array.
I'm entering five sequences one by one.
I want the first sequence ie. 1 0 5 to be stored in arr[0].
Note: 1, 0 and 5 are seperated by spaces.
However, arr[0] should contain 105 without any space.
I want to accept the next sequence into arr[1] only after pressing 'Enter'.
So that arr[1] should contain 117, arr[2] should contain 103 and so on up to arr[4].
Is there any operator that I can use for this?
There are no operators that do I/O in C at all, so no.
I also don't think there's any standard function with those semantics, they tend to view all whitespace as equal.
You should write your own, probably using fgets() to read in whole lines and then extracting the digits to convert to integers.

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.

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

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).

Resources