Program is running in loop while reading input - c

I got a problem while reading my input. I am getting input with fgetc(stdin). Before the equations I get number of equations. My code looks like this
char c;
for (int i = 0; i < rows; i++)
{
do
{
c=fgetc(stdin);
if(c!='+' && c!='-' && c!='\n' && !isspace(c) && c!='=' && c!=EOF)
{
l++;
equation[l]=c;
}
if(/* other conditions*/) eqToGauss(i,l,s,gaussMatrix,equation,sign);
} while (c!= '\n');
}
Problem is that, at the end of the penultimate lane(lane 8, after number 584), it gets '\n' and after that it won't stop the program but acts like it is running in loop. Xcode debug goes into assembler mode and the next character it reads since that moment is '\xff'. That algorithm works fine with short/ less equations.
This input is without empty lines after equations.
299 x2 + 529 x3 + 194 x5 + 282 x3 + 364 x5 + 135 x4 + 388 x1 + 492 x2 + 236 x4 + 165 x2 + 557 x1 = 135 x3 + 26 x2 + 566
482 x2 + 582 x5 = 530 x3 + 417 x1 + 424 x3 + 147 x5 + 81 x4 + 143 x3 + 195 x4 + 8 x2 + 479
532 x5 + 61 x4 + 318 x4 + 288 x5 + 573 x5 + 162 x4 = 99 x4 + 44 x3 + 190 x1 + 60
224 x1 + 145 x4 + 29 x4 + 1 x1 + 549 x5 + 186 x5 + 509 x4 + 178 x4 + 321 x1 + 320 x1 = 172 x4 + 537 x5 + 15
398 x5 + 79 x5 + 357 x2 + 153 x5 + 37 x5 + 301 x1 + 174 x5 + 111 x2 = 170 x4 + 208 x1 + 368 x3 + 14 x4 + 100 x2 + 531 x2 + 578 x3 + 510 x4 + 583 x1 + 281
301 x5 + 394 x5 + 272 x3 + 514 x5 = 412 x2 + 288 x4 + 182 x5 + 218 x1 + 210
331 x5 + 333 x4 + 376 x2 + 490 x5 + 275 x5 + 105 x4 + 498 x2 + 152 x3 + 51 x2 = 328 x4 + 135 x5 + 452 x4 + 62 x5 + 533 x1 + 261 x3 + 527 x1 + 124 x5 + 295 x4 + 532 x2 + 4
388 x2 + 415 x4 + 172 x4 + 403 x5 + 30 x1 = 511 x3 + 231 x5 + 104 x2 + 516 x2 + 380 x2 + 274 x5 + 522 x2 + 584
554 x1 + 434 x4 + 288 x5 + 98 x2 + 85 x5 + 413 x2 + 40 x1 = 584 x3 + 113 x2 + 262 x5 + 348 x5 + 285 x5 + 328 x4 + 351 x4 + 77 x5 + 149 x5 + 170 x1 + 203

You read the EOF(end-of-file) character with fgetc and can't exit the loop as the exit statement is c != '\n', but as long the EOF is in the stream the fgetc won't consume it and won't change variable c, thus causing infinite loop.

You need to test for EOF on fgetc() -- the 0xFF implies end of file, but I don't see anything in your code fragment that stops reading input on EOF.
if( (c = fgetc(stdin)) == EOF )
{
printf( "end of input\n" );
exit( 0 );
}

Related

Get first smaller timestamp in SQL Server query

I have one table from this type:
Equipment TimestampEqmt
truck1 timetruck1
truck2 timetruck2
truck3 timetruck3
truck1 timetruck4
truck2 timetruck5
truck3 timetruck6
and another with GPS coordinates for the trucks
Equipment TimestamGPS X Y Z
truck1 TimestamGPS1 X1 Y1 Z1
truck2 TimestamGPS2 X2 Y2 Z2
truck3 TimestamGPS3 X3 Y3 Z3
truck1 TimestamGPS4 X4 Y4 Z4
truck2 TimestamGPS5 X5 Y5 Z5
truck3 TimestamGPS6 X6 Y6 Z6
truck1 TimestamGPS1 X7 Y7 Z7
truck2 TimestamGPS2 X8 Y8 Z8
truck3 TimestamGPS3 X9 Y9 Z9
truck1 TimestamGPS1 X10 Y10 Z10
truck2 TimestamGPS2 X11 Y11 Z11
truck3 TimestamGPS3 X12 Y12 Z12
How to get for every TimestampEqmt and Equipment from first table, first GPS coordinates where TimestamGPS < TimestampEqmt?
The result should be like this:
Equipment TimestampEqmt TimestamGPS X Y Z
truck1 timetruck1 TimestamGPS7 X7 Y7 Z7
truck2 timetruck2 TimestamGPS11 X11 Y11 Z11
truck3 timetruck3 TimestamGPS12 X12 Y12 Z12
Which means that TimestamGPS7 is the first smaller timestamp depending on timetruck1 for truck1.

MATLAB loop through certain integers and in the order that I specify v2

The normal loop is
for i=1:50
end
but I want to execute the loop through certain integers in 4 different array simultaneously and in the order that I specify. So in the first loop, Matlab would take the first integer from r1, r2, c1, c2. Then for the second loop, Matlab would take the second integer from r1, r2, c1, c2.
For r1 = [1128, 1128, 1128, 1128, 1128, 1128, 1128, 1128];
r2 = [1148, 1148, 1148, 1148, 1148, 1148, 1148, 1148];
c1 = [776, 776 + 1280, 776 + 1280*2, 776 + 1280*3, 776 + 1280*3 + 994, 776 + 1280*4 + 994, 776 + 1280*5 + 994, 776 + 1280*6 + 994, 776 + 1280*7 + 994];
c2 = [792, 792 + 1280, 792 + 1280*2, 792 + 1280*3, 792 + 1280*3 + 994, 792 + 1280*4 + 994, 792 + 1280*5 + 994, 792 + 1280*6 + 994, 792 + 1280*7 + 994];
a1_raw = imread('T:\SST\...\pinhole_1-8_6s', 'PixelRegion', {[r1, r2], [c1, c2]});
End
Also, I'd like a1_raw to change every time the loop loops so that at the end of the loop I should have 8 different "a1_raws". How can I do that in MATLAB?
use a single index for all arrays
n = numel( r1 ); %// assuming r2, c1 and c2 all have same number of elements
a1_raw = cell( 1, n ); %//pre-allocate
for ii = 1:n
r1i = r1(ii);
r2i = r2(ii);
c1i = c1(ii);
c2i = c2(ii);
a1_raw{ii} = imread('T:\SST\...\pinhole_1-8_6s', 'PixelRegion', {[r1i, r2i], [c1i, c2i]});
end

Karatsuba Algorithm: splitting strings

I am trying to implement the Karatsuba algorithm in C.
I work with char strings (which are digits in a certain base), and although I think I have understood most of the Karatsuba algorithm, I do not get where I should split the strings to multiply.
For example, where should I cut 123 * 123, and where should I cut 123 * 12?
I can't get to a solution that works with both these calculations.
I tried to cut it in half and flooring the result when the number if odd, but it did not work, and ceiling does not work too.
Any clue?
Let a, b, c, and d be the parts of the strings.
Let's try with 123 * 12
First try (a = 1, b = 23, c = 1, d = 2) (fail)
z0 = a * c = 1
z1 = b * d = 46
z2 = (a + b) * (c + d) - z0 - z1 = 24 * 3 - 1 - 46 = 72 - 1 - 46 = 25
z0_padded = 100
z2_padded = 250
z0_padded + z1 + z2_padded = 100 + 46 + 250 = 396 != 123 * 12
Second try (a = 12, b = 3, c = 12, d = 0) (fail)
z0 = 144
z1 = 0
z2 = 15 * 12 - z1 - z0 = 180 - 144 = 36
z0_padded = 14400
z2_padded = 360
z0_padded + z1 + z2_padded = 14760 != 1476
Third try (a = 12, b = 3, c = 0, d = 12) (success)
z0 = 0
z1 = 36
z2 = 15 * 12 - z0 - z1 = 144
z0_padded = 0
z2_padded = 1440
z0_padded + z1 + z2_padded = 1476 == 1476
Let's try with 123 * 123
First try (a = 1, b = 23, c = 1, d = 23) (fail)
z0 = 1
z1 = 23 * 23 = 529
z2 = 24 * 24 - z0 - z1 = 46
z0_padded = 100
z2_padded = 460
z0_padded + z1 + z2_padded = 561 != 15129
Second try (a = 12, b = 3, c = 12, d = 3) (success)
z0 = 12 * 12 = 144
z1 = 3 * 3 = 9
z2 = 15 * 15 - z0 - z1 = 72
z0_padded = 14400
z2_padded = 720
z0_padded + z1 + z2_padded = 15129 == 15129
Third try (a = 12, b = 3, c = 1, d = 23) (fail)
z0 = 12
z1 = 3 * 23 = 69
z2 = 15 * 24 - z0 - z1 = 279
z0_padded = 1200
z2_padded = 2799
z0_padded + z1 = z2_padded = 4068 != 15129
Here, I do not get where I messed this up. Note that my padding method adds n zeroes at the end of a number where n = m * 2 and m equals the size of the longest string divided by two.
EDIT
Now that I have understood that b and d must be of the same length, it works almost everytime, but there are still exceptions: for example 1234*12
a = 123
b = 4
c = 1
d = 2
z0 = 123
z1 = 8
z2 = 127 * 3 - 123 - 8 = 250
z0_padded = 1230000
z2_padded = 25000
z0_padded + z1 + z2_padded = 1255008 != 14808
Here, assuming I split the strings correctly, the problem is the padding, but I do not get how I should pad. I read on Wikipedia that I should pad depending on the size of the biggest string (see a few lines up), there should be another solution.
The Karatsuba algorithm is a nice way to perform multiplications.
If you want it to work, b and d must be of the same length.
Here are two possibilities to compute 123x12 :
a= 1;b=23;c=0;d=12;
a=12;b= 3;c=1;d= 2;
Let's explain how it works for the second case :
123=12×10+3
12= 1×10+2
123×12=(12×10+3)×(1×10+2)
123×12=12×1×100+ (12×2+3×1)×10+3×2
123×12=12×1×100+((12+3)×(1+2)-12×1-3×2)×10+3×2
Let's explain how it works for the first case :
123=1×100+23
12=0×100+12
123×12=(1×100+23)×(0×100+12)
123×12=1×0×10000+ (1×12+23×0)×100+23×12
123×12=1×0×10000+((1+23)×(0+12)-1×0-23×12)×100+23×12
It also works with 10^k, 2^k or n instead of 10 or 100.

single line from multidimensional array, using awk

I have a file which contains:
x1 x2 x3 x4
x5 x6 x7 x8
x9
Which I want:
x1
x2
x3
.
.
How you do that using awk or in another ways?
With awk:
$ awk '1' RS=' ' ORS='\n' file
x1
x2
x3
x4
x5
x6
x7
x8
x9
However personally I would do:
$ xargs -n1 < file
x1
x2
x3
x4
x5
x6
x7
x8
x9
or:
$ grep -o '\S*' file
x1
x2
x3
x4
x5
x6
x7
x8
x9

How do you compute the XOR Remainder used in CRC?

I'm trying to remember how the math is worked out to compute the remainder of an XOR algorithm in Cyclical Redundancy Checks to verify the remainder bits of a network message.
I shouldn't have tossed that text book.
This is easily done in code, but how is it worked out by hand?
I know it looks something like a standard division algorithm, but I can't remember where to go from there to get the remainder.
___________
1010 | 101101000
Note: I did google it, but wasn't able to find a place where they mapped the steps in figuring the remainder.
1010 | 101101000
1010
0001 this result is 1011 XOR 1010 = 0001
1010
1010
0000 thus no remainder.
Thus 101101000 is perfect and no error has occurred in transmission/reception
In my experience it's easier to convert it to a polynomial when calculating by hand, especially when there're a lot of zeroes.
1010 = 1*x^3 + 0*x^2 + 1*x^1 + 0*x^0 = x^3 + x = x3 + x
101101000 = x8 + x6 + x5 + x3
-------------------
x3 + x ) x8 + x6 + x5 + x3
Then you divide the largest term in the dividend (x^8) with the first term in the divisor (x^3), resulting in x^5. You put that number on top and then multiply it with each term in the divisor. This yields the following for the first iteration:
x5
-------------------
x3 + x ) x8 + x6 + x5 + x3
x8 + x6
Doing XOR for each term then yields the new dividend: x5 + x3:
x5
-------------------
x3 + x ) x8 + x6 + x5 + x3
x8 + x6
-------------------
x5 + x3
Follow the same pattern until the dividend's largest term is smaller then the divisor's largest term. After the calculations are complete it will look like this:
x5 + x2
-------------------
x3 + x ) x8 + x6 + x5 + x3
x8 + x6
-------------------
x5 + x3
x5 + x3
-------------------
0
The reminder in this case is 0, which would indicate that most likely no errors has occurred during the transmission.
Note: I've shortened x^y as xy in the example above to reduce the clutter in the answer, since SO doesn't support math equation formatting.
Note2: Adding/subtracting a multiple of the divisor from the dividend will also give the reminder 0, since (P(x) + a*C(x)) / C(x) = P(x)/C(x) + a*C(x)/C(x) gives the same reminder as P(x)/C(x) since the reminder of a*C(x)/C(x) is 0.
It is long division by binary 11. There is an example on Wikipedia.
Let's assume that we want to divide 101110000 to 1001.
101110000
1001
--------- XOR the 1011 and 1001
0010
Now we will remove the zeros at the beginning of our XOR result which is 0010 and slide numbers from the top.
101110000
1001
---------
1010
Continue the XOR 1001 with the result.
101110000
1001
---------
1010
1001
---------
0011
--------- Remove zeros at the beginning
1100
1001
---------
0101
--------- Remove zeros at the beginning
1010
1001
---------
0011
Answer is 0011.

Resources