No write to file - gnucobol

I'm trying to make a simple COBOL program that takes user inputs and writes them to a .txt file. My program successfully creates a .txt file, however, it fails to record any of the input, and for the life of me I can't figure out why. I want it to repeatedly take user input until specified not to and write that input to a text file.
This is what I have:
IDENTIFICATION DIVISION.
PROGRAM-ID. LIFE4.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OPTIONAL OUT-FILE ASSIGN "life.txt"
ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD OUT-FILE.
01 F-EKRAN.
02 F-TEXT PIC X(73).
02 F-LINE PIC X(73).
WORKING-STORAGE SECTION.
01 E-WIDTH CONSTANT 73.
01 E-HEIGHT CONSTANT 31.
01 E-SIZE CONSTANT 300.
01 Y-POS PICTURE 99.
01 X-POS PICTURE 99.
01 Y-TEMP PICTURE 99.
01 X-TEMP PICTURE 99.
01 TI PICTURE 9(8).
01 RND PICTURE 9(8).
01 E-TEMP PICTURE X.
01 E-COUNT PICTURE 9.
01 E-STEP PICTURE 9(8).
01 S-STEP PICTURE +9(8).
01 WS-TEMP PICTURE X(E-WIDTH).
01 EKRAN.
02 E-LINE OCCURS E-HEIGHT.
03 E-ITEM OCCURS E-WIDTH PICTURE X.
01 I PIC S9.
01 J PIC S9.
PROCEDURE DIVISION.
MAIN.
OPEN OUTPUT OUT-FILE.
ACCEPT TI FROM TIME.
MOVE FUNCTION RANDOM(TI) TO RND.
MOVE 0 TO E-STEP.
MOVE 1 TO Y-POS.
PERFORM INIT-EKRAN E-HEIGHT TIMES.
PERFORM RANDOM-ITEM E-SIZE TIMES.
LOOP.
MOVE 1 TO Y-POS.
MOVE E-STEP TO S-STEP.
DISPLAY FUNCTION CONCATENATE("STEP: ", S-STEP).
PERFORM DRAW-EKRAN E-HEIGHT TIMES.
MOVE 1 TO Y-POS.
MOVE FUNCTION CONCATENATE("STEP: ", S-STEP) TO F-TEXT.
WRITE F-TEXT.
PERFORM E-HEIGHT TIMES
MOVE E-LINE(Y-POS) TO F-LINE
WRITE F-LINE
ADD 1 TO Y-POS
END-PERFORM.
PERFORM NEW-EKRAN.
DISPLAY "CONTYNUE? (Y/N)".
ACCEPT E-TEMP.
IF NOT ( E-TEMP = "N") MOVE "Y" TO E-TEMP.
ADD 1 TO E-STEP.
IF E-TEMP = "Y" GO TO LOOP.
WRITE F-TEXT FROM "END".
DISPLAY "END".
CLOSE OUT-FILE.
STOP RUN.
INIT-EKRAN.
MOVE 1 TO X-POS
PERFORM E-WIDTH TIMES
MOVE "-" TO E-ITEM(Y-POS,X-POS)
ADD 1 TO X-POS
END-PERFORM
ADD 1 TO Y-POS
.
DRAW-EKRAN.
DISPLAY E-LINE(Y-POS)
ADD 1 TO Y-POS
.
RANDOM-ITEM.
COMPUTE Y-POS = FUNCTION RANDOM() * E-HEIGHT + 1
COMPUTE X-POS = FUNCTION RANDOM() * E-WIDTH + 1
MOVE "X" TO E-ITEM(Y-POS,X-POS)
.
NEW-EKRAN.
MOVE 1 TO Y-POS
PERFORM E-HEIGHT TIMES
MOVE 1 TO X-POS
PERFORM E-WIDTH TIMES
MOVE 0 TO E-COUNT
MOVE -1 TO I
PERFORM 3 TIMES
MOVE -1 TO J
PERFORM 3 TIMES
IF NOT( I = 0 AND J = 0 )
COMPUTE Y-TEMP = Y-POS + I
COMPUTE X-TEMP = X-POS + J
IF Y-TEMP > 0 AND Y-TEMP < E-HEIGHT + 1 AND
X-TEMP > 0 AND X-TEMP < E-WIDTH + 1
IF E-ITEM(Y-TEMP,X-TEMP) = "X" OR
E-ITEM(Y-TEMP,X-TEMP) = "D"
ADD 1 TO E-COUNT
END-IF
END-IF
END-IF
ADD 1 TO J
END-PERFORM
ADD 1 TO I
END-PERFORM
IF E-ITEM(Y-POS,X-POS) = "-" AND E-COUNT = 3
MOVE "S" TO E-ITEM(Y-POS,X-POS) END-IF
IF E-ITEM(Y-POS,X-POS) = "X" AND
( E-COUNT < 2 OR E-COUNT > 3 )
MOVE "D" TO E-ITEM(Y-POS,X-POS) END-IF
ADD 1 TO X-POS
END-PERFORM
ADD 1 TO Y-POS
END-PERFORM
* DISPLAY " "
* DISPLAY " "
* PERFORM EKRAN-TEMP
* DISPLAY " "
* DISPLAY " "
MOVE 1 TO Y-POS
PERFORM E-HEIGHT TIMES
MOVE 1 TO X-POS
PERFORM E-WIDTH TIMES
IF E-ITEM(Y-POS,X-POS) = "S"
MOVE "X" TO E-ITEM(Y-POS,X-POS) END-IF
IF E-ITEM(Y-POS,X-POS) = "D"
MOVE "-" TO E-ITEM(Y-POS,X-POS) END-IF
ADD 1 TO X-POS
END-PERFORM
ADD 1 TO Y-POS
END-PERFORM
.
EKRAN-TEMP.
MOVE 1 TO Y-POS
PERFORM DRAW-EKRAN E-HEIGHT TIMES
.
END PROGRAM LIFE4.
When the program is executed, the file is created but it is empty. What is the problem?

Solution found.
IDENTIFICATION DIVISION.
PROGRAM-ID. LIFE4.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OPTIONAL OUT-FILE ASSIGN "life.txt"
ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD OUT-FILE.
01 FLIN PIC X(73).
WORKING-STORAGE SECTION.
01 E-WIDTH CONSTANT 73.
01 E-HEIGHT CONSTANT 31.
01 E-SIZE CONSTANT 300.
01 Y-POS PICTURE 99.
01 X-POS PICTURE 99.
01 Y-TEMP PICTURE 99.
01 X-TEMP PICTURE 99.
01 TI PICTURE 9(8).
01 RND PICTURE 9(8).
01 E-TEMP PICTURE X.
01 E-COUNT PICTURE 9.
01 E-STEP PICTURE 9(8).
01 S-STEP PICTURE +9(8).
01 WS-TEMP PICTURE X(E-WIDTH).
01 EKRAN.
02 E-LINE OCCURS E-HEIGHT.
03 E-ITEM OCCURS E-WIDTH PICTURE X.
01 I PIC S9.
01 J PIC S9.
PROCEDURE DIVISION.
MAIN.
OPEN OUTPUT OUT-FILE.
ACCEPT TI FROM TIME.
MOVE FUNCTION RANDOM(TI) TO RND.
MOVE 0 TO E-STEP.
MOVE 1 TO Y-POS.
PERFORM INIT-EKRAN E-HEIGHT TIMES.
PERFORM RANDOM-ITEM E-SIZE TIMES.
LOOP.
MOVE 1 TO Y-POS.
MOVE E-STEP TO S-STEP.
DISPLAY FUNCTION CONCATENATE("STEP: ", S-STEP).
PERFORM DRAW-EKRAN E-HEIGHT TIMES.
MOVE 1 TO Y-POS.
MOVE FUNCTION CONCATENATE("STEP: ", S-STEP) TO FLIN.
WRITE FLIN.
PERFORM E-HEIGHT TIMES
MOVE E-LINE(Y-POS) TO FLIN
WRITE FLIN
ADD 1 TO Y-POS
END-PERFORM.
PERFORM NEW-EKRAN.
DISPLAY "CONTYNUE? (Y/N)".
ACCEPT E-TEMP.
IF NOT ( E-TEMP = "N") MOVE "Y" TO E-TEMP.
ADD 1 TO E-STEP.
IF E-TEMP = "Y" GO TO LOOP.
MOVE "END" TO FLIN.
WRITE FLIN.
DISPLAY "END".
CLOSE OUT-FILE.
STOP RUN.
INIT-EKRAN.
MOVE 1 TO X-POS
PERFORM E-WIDTH TIMES
MOVE "-" TO E-ITEM(Y-POS,X-POS)
ADD 1 TO X-POS
END-PERFORM
ADD 1 TO Y-POS
.
DRAW-EKRAN.
DISPLAY E-LINE(Y-POS)
ADD 1 TO Y-POS
.
RANDOM-ITEM.
COMPUTE Y-POS = FUNCTION RANDOM() * E-HEIGHT + 1
COMPUTE X-POS = FUNCTION RANDOM() * E-WIDTH + 1
MOVE "X" TO E-ITEM(Y-POS,X-POS)
.
NEW-EKRAN.
MOVE 1 TO Y-POS
PERFORM E-HEIGHT TIMES
MOVE 1 TO X-POS
PERFORM E-WIDTH TIMES
MOVE 0 TO E-COUNT
MOVE -1 TO I
PERFORM 3 TIMES
MOVE -1 TO J
PERFORM 3 TIMES
IF NOT( I = 0 AND J = 0 )
COMPUTE Y-TEMP = Y-POS + I
COMPUTE X-TEMP = X-POS + J
IF Y-TEMP > 0 AND Y-TEMP < E-HEIGHT + 1 AND
X-TEMP > 0 AND X-TEMP < E-WIDTH + 1
IF E-ITEM(Y-TEMP,X-TEMP) = "X" OR
E-ITEM(Y-TEMP,X-TEMP) = "D"
ADD 1 TO E-COUNT
END-IF
END-IF
END-IF
ADD 1 TO J
END-PERFORM
ADD 1 TO I
END-PERFORM
IF E-ITEM(Y-POS,X-POS) = "-" AND E-COUNT = 3
MOVE "S" TO E-ITEM(Y-POS,X-POS) END-IF
IF E-ITEM(Y-POS,X-POS) = "X" AND
( E-COUNT < 2 OR E-COUNT > 3 )
MOVE "D" TO E-ITEM(Y-POS,X-POS) END-IF
ADD 1 TO X-POS
END-PERFORM
ADD 1 TO Y-POS
END-PERFORM
* DISPLAY " "
* DISPLAY " "
* PERFORM EKRAN-TEMP
* DISPLAY " "
* DISPLAY " "
MOVE 1 TO Y-POS
PERFORM E-HEIGHT TIMES
MOVE 1 TO X-POS
PERFORM E-WIDTH TIMES
IF E-ITEM(Y-POS,X-POS) = "S"
MOVE "X" TO E-ITEM(Y-POS,X-POS) END-IF
IF E-ITEM(Y-POS,X-POS) = "D"
MOVE "-" TO E-ITEM(Y-POS,X-POS) END-IF
ADD 1 TO X-POS
END-PERFORM
ADD 1 TO Y-POS
END-PERFORM
.
EKRAN-TEMP.
MOVE 1 TO Y-POS
PERFORM DRAW-EKRAN E-HEIGHT TIMES
.
END PROGRAM LIFE4.

Related

Cumulative Count in Power BI/DAX

I have seen many different questions that are similar but nothing that I can find that will work.
I am trying to calculate a "running" total for the amount of support tickets that I had on any given day prior to today. I have a current (today) total queue size, and know for each day whether I added to or removed from that queue.
For example:
Date
Created < Known
Completed < Known
Growth < Known
Total Size < Unknown
10-Jan
100
09-Jan
79
77
+2
102
08-Jan
97
92
+5
107
07-Jan
64
67
-3
104
06-Jan
70
66
-4
100
05-Jan
78
80
+2
102
04-Jan
90
82
-8
94
03-Jan
74
68
+6
100
02-Jan
83
87
-4
106
01-Jan
80
70
+10
116
10-Jan is the only known Total value. The remainder total values are being calculated.
In Excel, this would be a simple formula D3 = D2 + C3.
(Calculated column on 'Table' table)
RecursionWithoutIFAndNoFilter_AlsoThisIsWhatIcouldUnderstandFromYourPost_Sorry =
--RunningGrowth
VAR CurrentDate = 'Table'[Date]
VAR RunningGrowth = CALCULATE(SUM('Table'[Growth < Known]), REMOVEFILTERS('Table'), 'Table'[Date]>=CurrentDate)
--MAXDateInTable (I suppose this means TODAY)
--A change in level (because of SELECTEDVALUE) would mean there are more than one row with 01/10
VAR MaxDate = CALCULATE(MAX('Table'[Date]),REMOVEFILTERS('Table'))
VAR TotalSizeInMaxDate = CALCULATE(SELECTEDVALUE('Table'[Total Size < Unknown]),REMOVEFILTERS('Table'),'Table'[Date] = MaxDate)
--Result
VAR Result = TotalSizeInMaxDate + RunningGrowth
RETURN Result

How to write a program in gwbasic for adding the natural numbers for 1 to 100?

I am trying to write a program for adding the natural numbers from 1 to n (1 + 2 + 3 + ... + n). However, the sum appears 1 when I use if statement. And when I use for-next statement there is a syntax error that I don't understand.
if:
30 let s = 0
40 let i = 1
50 s = s + i
60 i = i + 1
70 if i<=n, then goto 50
80 print s
for-next:
30 let i, s
40 s = 0
50 for i = 1 to n
60 s = s + i
70 next i
80 print n
When I take n = 10, the if statement code gives a result of 1, but it should be 55.
When I try to use the for-next statement, it gives no result saying that there is a syntax error in 30.
Why is this happening?
The following code works in this online Basic interpreter.
10 let n = 100
30 let s = 0
40 let i = 1
50 s = s + i
60 i = i + 1
70 if i <= n then goto 50 endif
80 print s
I initialised n on the line labelled 10, removed the comma on the line labelled 70 and added an endif on the same line.
This is the for-next version:
30 let n = 100
40 let s = 0
50 for i = 1 to n
60 s = s + i
70 next i
80 print s
(btw, the sum of the first n natural numbers is n(n+1)/2:
10 let n = 100
20 let s = n * (n + 1) / 2
30 print s
)
Why is this happening? Where am I mistaking?
30 let s = 0
40 let i = 1
50 s = s + i
60 i = i + 1
70 if i<=n, then goto 50
80 print s
Fix #1: Initialize variable 'n':
20 let n = 10
Fix #2: Remove comma from line 70:
70 if i<=n then goto 50
30 let i, s
40 s = 0
50 for i = 1 to n
60 s = s + i
70 next i
80 print n
Fix #1: Initialize variable 'n':
30 let n = 10
Fix #2: Print 's' instead of 'n':
80 print s
10 cls
20 let x=1
30 for x=1 to 100
40 print x
50 next x
60 end

how to find xor key/algorithm, for a given hex?

So i have this hex: B0 32 B6 B4 37
I know this hex is obfuscated with some key/algorithm.
I also know this hex is equal to: 61 64 6d 69 6e (admin)
How can i calculate the XOR key for this?
If you write out the binary representation, you can see the pattern:
encoded decoded
10110000 -> 01100001
00110010 -> 01100100
Notice that the bit patterns have the same number of bits before and after. To decode, you just bitwise rotate one bit left. So the value shifts left one place and the most significant bit wraps around to the least significant place. To encode, just do the opposite.
int value, encoded_value;
encoded_value = 0xB0;
value = ((encoded_value << 1) | (encoded_value >> 7)) & 255;
// value will be 0x61;
encoded_value = ((value >> 1) | (value << 7)) & 255;

Logic: Applying gravity to a vector

There is a method called gravity(Vector[] vector) The vector contains sequence of numbers. The gravity function should return a new vector after applying gravity which is explained below.
Assume 0's are air and 1's are brick. When gravity is applied the bricks should fall down to the lowest level.
Let vector = [3, 7, 8]
Converting this to binary we get:
0 0 1 1 for 3
0 1 1 1 for 7
1 0 0 0 for 8
Applying gravity:
0 0 0 0 which is 0
0 0 1 1 which is 3
1 1 1 1 which is 15
So the gravity function should return [0, 3, 15].
Hope you people understood the explanation. I tried a lot but I couldn't figure out the logic for this. One thing I observed was the sum of the numbers in the vector before and after applying gravity remains same.
That is,
3 + 7 + 8 = 18 = 0 + 3 + 15 for the above case.
I think it is as simple as counting the total '1' bit of each position...
Let N be the input vector size, b be the longest binary length of the input elements
Pre-compute the total # of '1' bit of each position, stored in count[], O(N*b)
Run Gravity Function, that is, to regenerate N numbers from the count[], O(N*b)
Total run time is O(N*b)
Below is the sample code in C++
#include<bits/stdc++.h>
using namespace std;
int v[5] = {3,9,7,8,5};
int cnt[5] = {0};
vector<int> ans;
vector<int> gravity(){
vector<int> ret;
for(int i=0; i<5;i++){
int s = 0;
for(int j=0; j<5;j++)
if(cnt[j]){
s += (1<<j); cnt[j]--;
}
ret.push_back(s);
}
return ret;
}
int main(){
// precompute sum of 1 of each bit
for(int i=0, j=0, tmp=v[i]; i<5; i++, j=0, tmp=v[i]){
while(tmp){
if(tmp&1) cnt[j]++;
tmp >>= 1; j++;
}
}
ans = gravity();
for(int i=ans.size()-1; i>=0; i--) printf("%d ", ans[i]);
return 0;
}
The output is as follows:
Success time: 0 memory: 3272 signal:0
0 1 1 15 15
Start at the bottom. Any bricks in the row on top of that one will fall down except where there is already a brick on the bottom. So, the new bottom row is:
bottom_new = bottom_old OR top_old
The new top is:
top_new = bottom_old AND top_old
That is, there will be a brick in the new bottom row if there was a brick in either row, but there's only going to be a brick in the new top row if there was a brick in both rows.
Then you just work your way up the stack, with the new top row becoming the old bottom row for the next step.
The only solution I can think of so far uses nested for loops:
v is the input vector of N integers
D is the number of digits in each integer
c keeps track of the bottom-most free space where a brick can fall
The algorithm checks if the ith bit in the number n is set using (n & (1<<i)), which works in most C-like languages.
The algorithm in C:
for (int j=0; j<D; ++j)
int bit = 1<<j;
int c = N-1;
for (int i=N-1; i>=0; --i)
if (v[i] & bit) { // if bit j of number v[i] is set...
v[i] ^= bit; // set bit j in the number i to 0 using XOR
v[c] ^= bit; // set bottom-most bit in the number i to 1 using XOR
c -= 1; //increment by bottom row 1
}
If N is small and known it advance, you could work out the truth tables for the values of each digit and get the correct result using only bitwise operations and no loops.
Solution:
So I found a solution which needs recursion I guess. Though I don't know the condition to stop the recursion.
The vector v = [3, 7, 8] is very simple that its not possible to explain why recursion is required so am considering a new vector v = [3, 9, 7, 8, 5]
In binary form :
0 0 1 1 - a4
1 0 0 1 - a3
0 1 1 1 - a2
1 0 0 0 - a1
0 1 0 1 - a0
Iteration 1 :
0 0 0 0 - b7 (b7 = a4 AND b5)
0 0 1 1 - b6 (b6 = a4 OR b5)
0 0 0 0 - b5 (b5 = a3 AND b3) ignore this
1 0 0 1 - b4 (b4 = a3 OR b3)
0 0 0 0 - b3 (b3 = a2 AND b1) ignore this
0 1 1 1 - b2 (b2 = a2 OR b1)
0 0 0 0 - b1 (b1 = a0 AND a1) ignore this
1 1 0 1 - b0 (b0 = a0 OR a1)
Intermediate vector = [b7, b6, b4, b2, b0] = [0, 3, 9, 7, 13]
Iteration 2 :
0 0 0 0 - c7 (c7 = b4 AND c5)
0 0 0 1 - c6 (c6 = b4 OR c5)
0 0 0 1 - c5 (c5 = b3 AND c3) ignore this
0 0 1 1 - c4 (c4 = b3 OR c3)
0 0 0 1 - c3 (c3 = b2 AND c1) ignore this
1 1 0 1 - c2 (c2 = b2 OR c1)
0 1 0 1 - c1 (c1 = b0 AND b1) ignore this
1 1 1 1 - c0 (c0 = b0 OR b1)
Intermediate vector = [c7, c6, c4, c2, c0] = [0, 1, 3, 13, 15]
Iteration 3 :
0 0 0 0 - d7 (d7 = c4 AND d5)
0 0 0 1 - d6 (d6 = c4 OR d5)
0 0 0 1 - d5 (d5 = c3 AND d3) ignore this
0 0 0 1 - d4 (d4 = c3 OR d3)
0 0 0 1 - d3 (d3 = c2 AND d1) ignore this
1 1 1 1 - d2 (d2 = c2 OR d1)
1 1 0 1 - d1 (d1 = c0 AND c1) ignore this
1 1 1 1 - d0 (d0 = c0 OR c1)
Resultant vector = [d7, d6, d4, d2, d0] = [0, 1, 1, 15, 15]
I got this solution by going backwards through the vector.
Another solution:
Construct a multidimensional array with all the bits of all the elements in the vector (i.e) if v = [3,7,8] then construct a 3x4 array and store all the bits.
Count the number of 1's in each column and store the count.
Fill each column with count number of 1's starting from the bottom bit.
This approach is simple but requires construction of large matrices.

Generating random points to build a procedural line

I want to randomly generate points. Well at least there should be a limitation on the y-axis. Later I connect the points to a line which should proceed in a simple animation. You can imagine this as a random walk of a drunken person, going uphill and downhill.
This sounds very simple. I searched around the web and found that this could be accomplished using the markov chain. I think this idea is really interesting.
You can create the first state of your scene by yourself and pass this state as input to the markov chain algorithm. The algorithm randomly changes this state and creates a walk.
However I cannot find any example of that algorithm and no source code. I just found an applet that demonstrates the markov chain algorithm: http://www.probability.ca/jeff/java/unif.html
Please suggest some code. Any other ideas how to accomplish this are appreciated too.
I painted an example
So I want the line to proceed in a similar way. There are valleys, slopes ... they are random but the randomness still apply to the initial state of the line. This is why I found makrov chain so interesting here: http://www.suite101.com/content/implementing-markov-chains-a24146
Here's some code in Lua:
absstepmax = 25
ymin = -100
ymax = 100
x = 0
y = 5
for i = 1, 20 do
y = y + (math.random(2*absstepmax) - absstepmax - 1)
y = math.max(ymin, math.min(ymax, y))
x = x + 5
print (x,y)
end
absstepmax limits the size of a y step per iteration
ymin and ymax limit the extent of y
There is no bias in the example, i.e., y can change symmetrically up or down. If you want your "drunk" tending more "downhill" you can change the offset after the call to random from absstepmax - 1 to absstepmax - 5 or whatever bias you like.
In this example, the x step is fixed. You may make this random as well using the same mechanisms.
Here are some sample runs:
> absstepmax = 25
> ymin = -100
> ymax = 100
> x = 0
> y = 5
> for i = 1, 20 do
>> y = y + (math.random(2*absstepmax) - absstepmax - 1)
>> y = math.max(ymin, math.min(ymax, y))
>> x = x + 5
>> print (x,y)
>> end
5 4
10 22
15 37
20 39
25 50
30 40
35 21
40 22
45 12
50 16
55 16
60 12
65 -1
70 -8
75 -14
80 -17
85 -19
90 -25
95 -37
100 -59
> absstepmax = 25
> ymin = -100
> ymax = 100
> x = 0
> y = 5
> for i = 1, 20 do
>> y = y + (math.random(2*absstepmax) - absstepmax - 1)
>> y = math.max(ymin, math.min(ymax, y))
>> x = x + 5
>> print (x,y)
>> end
5 -2
10 -15
15 -7
20 1
25 1
30 12
35 23
40 45
45 43
50 65
55 56
60 54
65 54
70 62
75 57
80 62
85 86
90 68
95 76
100 68
>
Painted result added from OP:

Resources