diff --git a/dana/programs/N_Queens.dana b/dana/programs/N_Queens.dana index 72fc27c..57382ca 100644 --- a/dana/programs/N_Queens.dana +++ b/dana/programs/N_Queens.dana @@ -1,101 +1,110 @@ def main - def printBoard: board as byte[][], n as int - var i j is int - i := 0 + var size_main is int + + def printBoard: board_param as byte[][], n_param as int + var i_pb j_pb is int + i_pb := 0 loop: - if i >= n : + if i_pb >= n_param : break - j := 0 + j_pb := 0 loop: - if j >= n : + if j_pb >= n_param : break - if board[i][j] = 1 : + if board_param[i_pb][j_pb] = true : writeString: "Q " else : writeString: ". " - j := j + 1 + j_pb := j_pb + 1 writeString: "\n" - i := i + 1 + i_pb := i_pb + 1 writeString: "\n" - def isSafe is byte: board as byte[][], row col n as int - var i j is int + def isSafe is byte: board_param as byte[][], row_param col_param n_param as int + var i_is j_is is int - # check columns - i := 0 + i_is := 0 loop: - if i >= row : + if i_is >= row_param : break - if board[i][col] = 1 : - return : 0 - i := i + 1 + if board_param[i_is][col_param] = true : + return : false + i_is := i_is + 1 - # check left diagonal - i := row - 1 - j := col - 1 + i_is := row_param - 1 + j_is := col_param - 1 loop: - if i < 0 or j < 0 : + if i_is < 0 or j_is < 0 : break - if board[i][j] = 1 : - return : 0 - i := i - 1 - j := j - 1 - - # check right diagonal - i := row - 1 - j := col + 1 + if board_param[i_is][j_is] = true : + return : false + i_is := i_is - 1 + j_is := j_is - 1 + + i_is := row_param - 1 + j_is := col_param + 1 loop: - if i < 0 or j >= n : + if i_is < 0 or j_is >= n_param : break - if board[i][j] = 1 : - return : 0 - i := i - 1 - j := j + 1 + if board_param[i_is][j_is] = true : + return : false + i_is := i_is - 1 + j_is := j_is + 1 - return : 1 + return : true - def solveNQueensUtil is byte: board as byte[][], row n as int - if row = n : - printBoard: board, n - return : 1 + def solveNQueensUtil is byte: board_param as byte[][], row_param n_param as int + var col_util is int - var col is int - col := 0 + if row_param = n_param : + printBoard: board_param, n_param + return : true + + col_util := 0 loop: - if col >= n : + if col_util >= n_param : break - if isSafe: board, row, col, n : - board[row][col] := 1 - if solveNQueensUtil: board, row + 1, n : - return : 1 - board[row][col] := 0 - col := col + 1 + if isSafe(board_param, row_param, col_util, n_param) : + if row_param < 20 and col_util < 20 : board_param[row_param][col_util] := true + + if solveNQueensUtil(board_param, row_param + 1, n_param) : + return : true + + if row_param < 20 and col_util < 20 : board_param[row_param][col_util] := false + col_util := col_util + 1 - return : 0 + return : false - def solveNQueens: n as int - var board is byte[n][n] - var i j is int + def solveNQueens: n_param as int + var board_snq is byte[20][20] + var i_snq j_snq is int - i := 0 + i_snq := 0 loop: - if i >= n : + if i_snq >= n_param : break - j := 0 + j_snq := 0 loop: - if j >= n : + if j_snq >= n_param : break - board[i][j] := 0 - j := j + 1 - i := i + 1 + if i_snq < 20 and j_snq < 20 : board_snq[i_snq][j_snq] := false + j_snq := j_snq + 1 + i_snq := i_snq + 1 - if solveNQueensUtil: board, 0, n : - writeString: "Solution found.\n" + if solveNQueensUtil(board_snq, 0, n_param) : + skip else : writeString: "No solution exists.\n" - var size is int writeString: "Enter board size: " - size := readInteger() - solveNQueens: size + size_main := readInteger() + if size_main > 20 : + writeString: "Warning: Board size capped at 20 due to program limits.\n" + size_main := 20 + if size_main < 1 : + writeString: "Info: Board size must be at least 1. Setting to 1.\n" + size_main := 1 + + if size_main > 0 : solveNQueens: size_main + else: writeString: "No solution exists for non-positive size.\n" diff --git a/dana/programs/bsort.dana b/dana/programs/bsort.dana index 98e008a..149b424 100644 --- a/dana/programs/bsort.dana +++ b/dana/programs/bsort.dana @@ -1,27 +1,28 @@ def main def bsort: n as int, x as int [] - - def swap: x y as ref int - var t is int - t := x - x := y - y := t - var changed is byte var i is int - loop: + loop outer_loop_label: changed := false i := 0 - loop: + loop inner_loop_label: if i < n-1: if x[i] > x[i+1]: swap: x[i], x[i+1] changed := true i := i+1 - else: break - if not changed: break + else: + break : inner_loop_label + if not changed: + break : outer_loop_label + + def swap: x y as ref int + var t is int + t := x + x := y + y := t def writeArray: msg as byte [], n as int, x as int [] var i is int @@ -37,16 +38,17 @@ def main writeString: "\n" var seed i is int - var x is int [16] + var x is int [16] seed := 65 i := 0 - loop: + loop init_loop: if i < 16: seed := (seed * 137 + 220 + i) % 101 x[i] := seed i := i+1 - else: break + else: + break : init_loop writeArray: "Initial array: ", 16, x bsort: 16, x writeArray: "Sorted array: ", 16, x diff --git a/dana/programs/calculator.dana b/dana/programs/calculator.dana index a1bcb74..65482f6 100644 --- a/dana/programs/calculator.dana +++ b/dana/programs/calculator.dana @@ -1,3 +1,4 @@ +(* calculator.dana *) def main # Helper Function @@ -11,10 +12,8 @@ def main else: return: false - (* - Conversion from part of a string to an int - [from, to) - *) + # Conversion from part of a string to an int + # [from, to) def string_to_int is int: s as byte[], from to as int var i res pow is int @@ -30,18 +29,16 @@ def main return: res - (* - Simple Calculator - - Input: lines of the following form: - - where is a positive integer - and is one of +, -, *, / - Any other characters among them get ignored. - - Output: lines with the results - or a message "Invalid" in case of failure - *) + # Simple Calculator + # + # Input: lines of the following form: + # + # where is a positive integer + # and is one of +, -, *, / + # Any other characters among them get ignored. + # + # Output: lines with the results + # or a message "Invalid" in case of failure var buf is byte[256] var idx len start p_end a b is int @@ -60,50 +57,71 @@ def main if idx = len: writeString: "Invalid\n" continue: lines - elif is_number(buf[idx]): + elif is_digit(buf[idx]): break: a_start idx := idx + 1 start := idx loop a_end: if idx = len: - writeString: "Invalid\n" - continue: lines - elif not is_number(buf[idx]): + p_end := idx + break: a_end + elif not is_digit(buf[idx]): + p_end := idx break: a_end idx := idx + 1 - p_end := idx + if idx = len: + p_end := idx + break: a_end + + if start = p_end: + writeString: "Invalid\n" + continue: lines - a := string_to_int(buff, start, p_end) + a := string_to_int(buf, start, p_end) loop op_detect: if idx = len: writeString: "Invalid\n" continue: lines elif is_operator(buf[idx]): + op := buf[idx] + idx := idx + 1 break: op_detect idx := idx + 1 - op := buf[idx] + + if idx = start: + writeString: "Invalid\n" + continue: lines loop b_start: if idx = len: writeString: "Invalid\n" continue: lines - elif is_number(buf[idx]): + elif is_digit(buf[idx]): break: b_start idx := idx + 1 start := idx loop b_end: - if idx = len or not is_number(buf[idx]): + if idx = len: + p_end := idx + break: b_end + elif not is_digit(buf[idx]): + p_end := idx break: b_end idx := idx + 1 - p_end := idx + if idx = len: + p_end := idx + break: b_end - b := string_to_int(buff, start, p_end) + if start = p_end: # No digits were found for the second number + writeString: "Invalid\n" + continue: lines - # We have finished parsing now we can calculate + b := string_to_int(buf, start, p_end) # Corrected buff to buf + # We have finished parsing now we can calculate if op = '+': writeInteger: a + b elif op = '-': @@ -111,9 +129,12 @@ def main elif op = '*': writeInteger: a * b elif op = '/': - writeInteger: a / b + if b = 0: + writeString: "Invalid\n" # Division by zero + else: + writeInteger: a / b else: writeString: "Invalid\n" + writeString: "\n" exit - diff --git a/dana/programs/linemarket.dana b/dana/programs/linemarket.dana index b659880..6099498 100644 --- a/dana/programs/linemarket.dana +++ b/dana/programs/linemarket.dana @@ -1,136 +1,123 @@ -(* - This is an implementation of my solution for the first exercise in - the first coding exercises set for the Algorithms course during the - 2024-2025 winter Semester @ ECE/NTUA. The full description of the - problem can be found in the web page of the course in Helios. - - Our goal is to place N stores in M non-overlapping integer intervals[s_i, x_i]. - A store can be placed in any integer number that belongs to an interval. - We want our program to calculate the smallest possible distance between - two consecutive stores such as that distance is maximized. - - Input: The program reads from the standard input two positive integers, N amount of stores and M amount of spaces(intervals) to place them. - After that for the next M lines it reads from the standard input two non-negative integers defining the beginning and the end of each space(interval). - We assume the spaces are given in a random order and their total length is not shorter than N. - - Output: Our program prints in the standard output a positive integer, - the minimum required distance between two consecutive stores in a - placement of all stores such as that minimum distance is maximized. - - Example Input: 6 3 - 10 12 - 0 4 - 5 8 - - Example Output: 2 - -*) - -def main +(* linemarket.dana + * + * Input: + * 6 <-- stores + * 3 <-- N (number of intervals) + * 10 <-- s (start of interval 1) + * 12 <-- f (end of interval 1) + * 0 <-- s (start of interval 2) + * 4 <-- f (end of interval 2) + * 5 <-- s (start of interval 3) + * 8 <-- f (end of interval 3) + * + * Output: + * 2 + *) +def main + var stores size s f N tail is int + var MAX_STORES MAX_PLACES is int + var i is int + var arr is int[200000] + + def swap: x y as ref int + var t is int + t := x + x := y + y := t + def bsort: n as int, x as int[] - def swap: x y as ref int - var t is int - t := x - x := y - y := t - var changed is byte - var i is int - + var i_bsort is int + loop: changed := false - i := 0 + i_bsort := 0 loop: - if i < n-1: - if x[i] > x[i+1]: - swap x[i], x[i+1] + if i_bsort < n-1: + if x[i_bsort] > x[i_bsort+1]: + swap: x[i_bsort], x[i_bsort+1] changed := true - i := i + 1 + i_bsort := i_bsort + 1 else: break if not changed: break - - def possible_store is byte: arr as int[], mid spaces stores as int: - var last_position placed_stores i is int + + def possible_store is byte: current_arr as int[], mid spaces stores_to_place as int + var last_position placed_stores i_ps is int + var j is int + var position is int last_position := -1000000000 placed_stores := 0 - i := 0 + i_ps := 0 loop: - if i >= (spaces/2): break + if i_ps >= (spaces/2): break else: - var j is int - j := 2*i - var position is int - if (last_position + mid) > arr[j]: position := last_position + mid - else: position := arr[j] + j := 2*i_ps + if (last_position + mid) > current_arr[j]: position := last_position + mid + else: position := current_arr[j] loop: - if position > arr[j + 1]: break + if position > current_arr[j + 1]: break else: placed_stores := placed_stores + 1 last_position := position position := position + mid - if placed_stores >= stores: return true - i := i + 1 - return false + if placed_stores >= stores_to_place: return: true + i_ps := i_ps + 1 + return: false - def largest_minimum_distance is int: arr as int[], spaces stores as int: - bsort(arr, spaces) + def largest_minimum_distance is int: lmd_arr as int[], spaces num_stores as int + var distance is int var result left right is int + + bsort: spaces, lmd_arr result := 0 left := 0 - right := arr[spaces - 1] - arr[0] - - #Simple Binary Search for finding the sweet spot + if spaces > 0 : right := lmd_arr[spaces - 1] - lmd_arr[0] + else: right := 0 + + loop: if left > right: break else: - var distance is int - distance := (left + right) / 2 - if possible_store(arr, distance, spaces, stores): + if left > right : distance := left + else: distance := (left + right) / 2 + + if distance <= 0 : distance := 1 + + if possible_store(lmd_arr, distance, spaces, num_stores): result := distance left := distance + 1 else: - right = distance + 1 - return result + right := distance - 1 + return: result - var stores size s f N tail is int tail := 0 + MAX_STORES := 1000000 + MAX_PLACES := 100000 + stores := readInteger() N := readInteger() - var MAX_SIZE MAX_PLACES is int - - MAX_STORES := 1000000 #Change accordingly for larger sizes - MAX_PLACES := 100000 #Change accordingly for larger sizes - - (* - The program handles out of bound inputs by - asking the user again for a correct one. - Maybe the two "if" statements can be combined into one. - *) if stores > MAX_STORES or stores < 1: - writeString "Wrong starting input, let's try again!" + writeString: "Wrong starting input, let's try again!\n" stores := readInteger() if N > MAX_PLACES or N < 1: - writeString "Wrong starting input, let's try again!" + writeString: "Wrong starting input, let's try again!\n" N := readInteger() - var arr is int[2*N] - - var i is int i := 0 loop: if i >= N: break else: - s := readInteger() #We assume correct values for the limits of the spaces + s := readInteger() f := readInteger() arr[i*2] := s arr[i*2 + 1] := f tail := tail + 2 i := i + 1 - + writeInteger: largest_minimum_distance(arr, tail, stores) writeString: "\n" diff --git a/dana/programs/lis.dana b/dana/programs/lis.dana index d70873a..35be0c8 100644 --- a/dana/programs/lis.dana +++ b/dana/programs/lis.dana @@ -2,62 +2,68 @@ Finds the length of the Longest Increasing Subsequence. Dynamic Programming solution in O(N^2) *) - def main - - def lis is int: n as int, arr as int[] - - var dp is int [n] - var i j maxLen is int - - if n <= 0: - return: 0 - - (* Initialize dp array *) - i := 0 - loop: - if i >= n : break - dp[i] := 1 - i := i + 1 - - (* Begin computing values *) - i := 1 - loop: - if i >= n: break - j := 0 - loop: - if j >= i: break - if arr[j] < arr[i] and dp[j] + 1 > dp[i]: - dp[i] := dp[j] + 1 - j := j + 1 - i := i + 1 - - maxLen := dp[0] - i := 1 - loop: - if i >= n: break - if dp[i] > maxLen: - maxLen := dp[i] - i := i + 1 - - return: maxLen - - var n is int - n := readInteger() - - var arr is int [n] - - (* Populate array *) - var i is int - i := 0 - loop: - if i >= n: break - arr[i] := readInteger() - i := i + 1 - - var len is int - len := lis: n, arr - - writeString: "longest inreasing subsequence has length " - writeInteger: len - writeString: "\n" + var n_main is int + var arr_main is int [1000] + var i_main is int + var len is int + var dummy_val is int + + def lis is int: n_param as int, arr_param as int[] + + var dp is int [1000] + var i j maxLen is int + + if n_param <= 0: + return: 0 + + i := 0 + loop: + if i >= n_param : break + dp[i] := 1 + i := i + 1 + + i := 1 + loop: + if i >= n_param: break + j := 0 + loop: + if j >= i: break + if arr_param[j] < arr_param[i] and dp[j] + 1 > dp[i]: + dp[i] := dp[j] + 1 + j := j + 1 + i := i + 1 + + if n_param > 0: + maxLen := dp[0] + else: + maxLen := 0 + + i := 1 + loop: + if i >= n_param: break + if dp[i] > maxLen: + maxLen := dp[i] + i := i + 1 + + return: maxLen + + n_main := readInteger() + + i_main := 0 + loop: + if i_main >= n_main: break + if i_main < 1000: + arr_main[i_main] := readInteger() + else: + dummy_val := readInteger() + i_main := i_main + 1 + + if n_main > 1000: + writeString: "Warning: Input size exceeds program capacity, processing up to 1000 elements.\n" + n_main := 1000 + + len := lis( n_main, arr_main ) + writeString: "longest increasing subsequence has length " + writeInteger: len + writeString: "\n" diff --git a/dana/programs/mergesort.dana b/dana/programs/mergesort.dana index 7c924f0..f803457 100644 --- a/dana/programs/mergesort.dana +++ b/dana/programs/mergesort.dana @@ -1,83 +1,93 @@ -def merge: arr as int [], left as int, mid as int, right as int - var n1 n2 i j k is int - n1 := mid - left + 1 - n2 := right - mid +def main + var n_main is int + var arr_main is int [1000] + var i_main is int + var dummy_val is int - var L is int [n1] - var R is int [n2] + def merge: arr_param as int [], left_param as int, mid_param as int, right_param as int + var n1_local n2_local i_local j_local k_local is int + var L_temp is int [1000] + var R_temp is int [1000] - i := 0 - loop: - if i < n1: - L[i] := arr[left + i] - i := i + 1 - else: break + n1_local := mid_param - left_param + 1 + n2_local := right_param - mid_param - j := 0 - loop: - if j < n2: - R[j] := arr[mid + 1 + j] - j := j + 1 - else: break + i_local := 0 + loop: + if i_local < n1_local and left_param + i_local < 1000 and i_local < 1000: + L_temp[i_local] := arr_param[left_param + i_local] + i_local := i_local + 1 + else: break - i := 0 - j := 0 - k := left + j_local := 0 + loop: + if j_local < n2_local and mid_param + 1 + j_local < 1000 and j_local < 1000: + R_temp[j_local] := arr_param[mid_param + 1 + j_local] + j_local := j_local + 1 + else: break - loop: - if i < n1 and j < n2: - if L[i] <= R[j]: - arr[k] := L[i] - i := i + 1 - else: - arr[k] := R[j] - j := j + 1 - k := k + 1 - else: break + i_local := 0 + j_local := 0 + k_local := left_param - loop: - if i < n1: - arr[k] := L[i] - i := i + 1 - k := k + 1 - else: break + loop: + if i_local < n1_local and j_local < n2_local: + if L_temp[i_local] <= R_temp[j_local]: + if k_local < 1000: arr_param[k_local] := L_temp[i_local] + i_local := i_local + 1 + else: + if k_local < 1000: arr_param[k_local] := R_temp[j_local] + j_local := j_local + 1 + k_local := k_local + 1 + else: break - loop: - if j < n2: - arr[k] := R[j] - j := j + 1 - k := k + 1 - else: break + loop: + if i_local < n1_local: + if k_local < 1000: arr_param[k_local] := L_temp[i_local] + i_local := i_local + 1 + k_local := k_local + 1 + else: break -def merge_sort: arr as int [], left as int, right as int - if left < right: - var mid is int - mid := (left + right) / 2 - merge_sort: arr, left, mid - merge_sort: arr, mid + 1, right - merge: arr, left, mid, right + loop: + if j_local < n2_local: + if k_local < 1000: arr_param[k_local] := R_temp[j_local] + j_local := j_local + 1 + k_local := k_local + 1 + else: break -def main - var n is int - n := readInteger() + def merge_sort: arr_param as int [], left_param as int, right_param as int + var mid_local is int - var arr is int [n] - var i is int + if left_param < right_param: + mid_local := (left_param + right_param) / 2 + merge_sort: arr_param, left_param, mid_local + merge_sort: arr_param, mid_local + 1, right_param + merge: arr_param, left_param, mid_local, right_param - i := 0 + n_main := readInteger() + + i_main := 0 loop: - if i < n: - arr[i] := readInteger() - i := i + 1 - else: break + if i_main >= n_main: break + if i_main < 1000: + arr_main[i_main] := readInteger() + else: + dummy_val := readInteger() + i_main := i_main + 1 + + if n_main > 1000: + writeString: "Warning: Input size exceeds program capacity (1000 elements). Processing first 1000.\n" + n_main := 1000 + - merge_sort: arr, 0, n-1 + if n_main > 0 : + merge_sort: arr_main, 0, n_main - 1 - i := 0 + i_main := 0 loop: - if i < n: - writeInteger: arr[i] - writeString: " " - i := i + 1 - else: break + if i_main >= n_main: break + if i_main < 1000: + writeInteger: arr_main[i_main] + writeString: " " + i_main := i_main + 1 writeString: "\n" diff --git a/dana/programs/palindrome.dana b/dana/programs/palindrome.dana index 29efc9e..8203a85 100644 --- a/dana/programs/palindrome.dana +++ b/dana/programs/palindrome.dana @@ -1,35 +1,35 @@ def main - - def check_pal is byte: s as byte[] + def check_pal is byte: s as byte[] + var res is byte + var i l is int - var res is byte - var i l is int - - l:=strlen(s) - i:=0 - res:=true - - loop: - if i = l/2: break - elif not s[i] = s[l-i-1]: - res:=false - break - else: - i:=i+1 + l := strlen(s) + i := 0 + res := true - return: res + loop: + if i = l / 2: break + elif not s[i] = s[l-i-1]: + res := false + break + else: + i := i + 1 + return: res + var is_pal is byte + var input is byte [31] - val is_pal is byte - val input is byte [] - - writeString: "Give a string with maximum length 30: " - readString: 30, input - writeString: input + writeString: "Give a string with maximum length 30: " + readString: 31, input + writeString: "Input was: " + writeString: input + writeString: "\n" - is_pal := check_pal: input - - if is_pal: - writeString: " ... is palindrome\n" - else: - writeString: " ... is not palindrome\n" + is_pal := check_pal(input) + + if is_pal = true: + writeString: input + writeString: " ... is palindrome\n" + else: + writeString: input + writeString: " ... is not palindrome\n" diff --git a/dana/programs/quicksort.dana b/dana/programs/quicksort.dana index 11bf5b9..f1bf905 100644 --- a/dana/programs/quicksort.dana +++ b/dana/programs/quicksort.dana @@ -1,72 +1,59 @@ def main -begin def partition is int: low high as int, arr as int [] - begin - var pivot i temp j is int + var pivot i temp j is int pivot := arr[high] i := low - 1 j := low - loop: - begin - if j <= high-1: begin break end + if j >= high: break if arr[j] < pivot: - begin i := i + 1 temp := arr[i] arr[i] := arr[j] arr[j] := temp - end j := j + 1 - end temp := arr[i+1] arr[i+1] := arr[high] arr[high] := temp - return: i+1 - end + return: i+1 def quicksort: low high as int, arr as int [] - begin - if low <= high: - begin - var pivot is int - pivot := partition(low, high, arr) - # recursion - quicksort: low, pivot-1, arr - quicksort: pivot+1, high, arr - end - end + var pi is int + if low < high: + pi := partition(low, high, arr) + quicksort: low, pi-1, arr + quicksort: pi+1, high, arr - # Read the length of array from input var N is int - N := readInteger() - # Define the array of given length - var nums is int [N] + var nums is int [100] var k is int - k := 0 + writeString: "Enter number of elements (max 100):\n" + N := readInteger() + + if N > 100: + writeString: "Error: Number of elements exceeds maximum allowed size (100).\n" + exit + if N <= 0: + writeString: "Number of elements must be positive.\n" + exit + + k := 0 loop: - begin - if k < N: begin break end + if k >= N: break + writeString: "Enter element " nums[k] := readInteger() k := k + 1 - end - quicksort: 0, N, nums + quicksort: 0, N-1, nums k := 0 + writeString: "Sorted array: \n" loop: - begin - if i < n: - begin - if k > 0: begin writeString: ā€, ā€ end - writeInteger: x[k] + if k < N: + if k > 0: writeString: ", " + writeInteger: nums[k] k := k+1 - end - else: - begin + else: break - end - end - writeString: ā€\nā€ -end + writeString: "\n" diff --git a/dana/programs/rotatefun.dana b/dana/programs/rotatefun.dana index 459045d..ce054c2 100644 --- a/dana/programs/rotatefun.dana +++ b/dana/programs/rotatefun.dana @@ -1,50 +1,57 @@ # LeetCode problem: 396. Rotate Function (https://leetcode.com/problems/rotate-function/description/) def main - - def maxRotateFunction is int: n as int, nums as int [] - var sum prevF maxF i is int + def maxRotateFunction is int: n_param as int, nums_param as int [] + var sum prevF maxF i F is int sum := 0 prevF := 0 - maxF := 0 i := 0 loop: - if i < n: - sum := sum + nums[i] - prevF := prevF + i * nums[i] + if i < n_param: + sum := sum + nums_param[i] + prevF := prevF + i * nums_param[i] i := i + 1 else: break - maxF := prevF - i := 1 + maxF := prevF + i := 1 loop: - if i < n: - var F is int - F := prevF + sum - n * nums[n-i] - if F > maxF: maxF := F + if i < n_param: + F := prevF + sum - n_param * nums_param[n_param-i] + if F > maxF: + maxF := F prevF := F i := i + 1 else: break return: maxF - var n is int - n := readInteger() - - var nums is int [n] + var n_actual is int + var nums_main is int [100] + var i_main is int + var result is int - var i is int - i := 0 + writeString: "Enter number of elements (max 100):\n" + n_actual := readInteger() + + if n_actual <= 0: + writeString: "Number of elements must be positive.\n" + exit + if n_actual > 100: + writeString: "Number of elements exceeds array capacity (100).\n" + exit + + i_main := 0 loop: - if i < n: - nums[i] := readInteger() - i := i + 1 + if i_main < n_actual: + writeString: "Enter element " + nums_main[i_main] := readInteger() + i_main := i_main + 1 else: break - var result is int - result := maxRotateFunction(n, nums) + result := maxRotateFunction(n_actual, nums_main) writeInteger: result writeString: "\n"