School themes from USSR and EurAsia
#20
Universal program for learning other languages
for example C# & Python
translating every line and paragraphs qb64 qbasic

output to screen
keyboard input array size
1-dimensional array
2-dimensional array
random
mathematics
for loop
condition
while loop
output to a file
read from file
and it can be useful to students and teachers

Script:

Start random
Set or enter size of array n from keyboard
Start arrays 1-dimensional and 2-dimensional given n
Start arrays of results 1-dimensional and 2-dimensional

Array 1 random from 0 to 9: filling and output

If element is even: calculate square
otherwise, leave value as it is
and put results in another array
and print both arrays in a column

Bubble sorting of final array
nested loops and exchanges

Output sorted final array via while

Fill 2-dimensional array with sums of column and row numbers
If value is odd: square
it, otherwise leave value and create another 2-dimensional array
and display original matrix and result

Create a file name as 2 random digits and ".txt"

Output result matrix to file in reverse order
where both rows and columns are inverted

Close file for writing and open file for reading

Read from file to variable w
and form a matrix on screen inverse of matrix 2

Everyone can + must = must
translate entire program or part of program independently
and I translated into Python & C# as brothers qb64 qbasic

Universal Yabasic: https://jdoodle.com/ia/CUo
Universal C#: https://rextester.com/RYGR5556
Universal C++: https://rextester.com/DHHKPJ12975
Universal Python https://rextester.com/YJZQQ73725

Code: (Select All)
Randomize Timer: t=timer: n = 3: ' Print "n= ": input n
Dim d(n), a(n, n), x(n), y(n, n) ' universe.bas

For i = 1 To n: d(i) = Int(Rnd * 9)
Print d(i);: Next: Print: Print

For i = 1 To n
If d(i) Mod 2 = 0 Then x(i)=d(i)^2 Else x(i)=d(i)
Print d(i), x(i): Next: Print

For i = 1 To n - 1: For j = i + 1 To n
    If x(i) > x(j) Then Swap x(i),x(j)
Next: Next

i = 1: While (i <= n)
    Print x(i);: i = i + 1
Wend: Print: Print

For i = 1 To n: For j = 1 To n
    a(i,j) = i + j
    If (a(i,j) Mod 2 <> 0) Then
        y(i,j) = a(i,j)^2
    Else
        y(i,j) = a(i,j)
        End If
Print a(i,j);: Next: Print: Next: Print

For i = 1 To n: For j = 1 To n
Print y(i,j);: Next: Print: Next: Print

a$ = Mid$(Str$(Int(Rnd * 88) + 10) + ".txt", 2, 6)

Open a$ For Output As #1
For i = n To 1 Step -1: For j = n To 1 Step -1
Print #1, y(i,j);: Next: Print #1,: Next: Print #1,

Close: Open a$ For Input As #2

For i = 1 To n: For j = 1 To n
Input #2, w: Print w;: Next: Print: Next: Print
Close

min = d(1): max = d(1):  nmin=1: nmax=1
for i=2 to n
if d(i)< min then min=d(i): nmin=i
if d(i)> max then max=d(i): nmax=i
next
for i=1 to n: Print d(i);: next: Print: Print 
Print min; nmin, max; nmax

s=0: for i=1 to n: s=s+d(i): next: ? s, s/n
_delay .25: Print Timer - t: End

Results for n=5 from Python

Code: (Select All)
Array d in a string
6 0 7 8 5

Array d even x=d^2
6 36
0 0
7 7
8 64
5 5

Array x sorting
and output x via while
0 5 7 36 64

Array y sum or odd y=d^2
0 1 2 9 4
1 2 9 4 25
2 9 4 25 6
9 4 25 6 49
4 25 6 49 8

Array d minimax from:
6 0 7 8 5

0 1 8 3

Sum d and average
26 5,20
Write name of program in 1st line to copy & paste & save filename.bas
Insert program pictures: press print-screen-shot button
Open paint & Paste & Save as PNG
Add picture file to program topic

Russia looks world from future. Big data is peace data.
I never recommend anything & always write only about myself
Reply


Messages In This Thread
School themes from USSR and EurAsia - by DANILIN - 05-02-2022, 12:31 PM
RE: School themes from USSR and EurAsia - by Pete - 11-07-2022, 10:48 PM
RE: School themes from USSR and EurAsia - by Jack - 11-08-2022, 03:07 AM
RE: School themes from USSR and EurAsia - by Jack - 11-08-2022, 03:19 PM
RE: School themes from USSR and EurAsia - by Pete - 11-08-2022, 04:39 PM
RE: School themes from USSR and EurAsia - by Pete - 11-08-2022, 03:34 PM
RE: School themes from USSR and EurAsia - by DANILIN - 01-27-2023, 08:45 AM



Users browsing this thread: 12 Guest(s)