Hello Mahmoud et ALL
C-Style Arrays Implementation in Ring Language
- C_StyleArrays.ring
# Corrected to work with Ring's actual capabilities
# Using Ring's native list functions and avoiding conflicts
has Built in Tests;
=======================
OUTPUT
C-Style Arrays in Ring Language
====================================================
1. Basic Array Operations (C-style)
-----------------------------------
Array initialized: { 10, 20, 30, 40, 50 }
After arr[2] = 35: { 10, 20, 35, 40, 50 }
arr[0] = 10
arr[4] = 50
2. Dynamic Memory Allocation
----------------------------
Allocated array of size: 10
Calloc array (Zeros): { 0, 0, 0, 0, 0 }
Malloc array filled.: { 10, 20, 30, 40, 50, 0, 0, 0, 0, 0 }
After memset(ptr2, 99, 3): { 99, 99, 99, 0, 0 }
After memcpy(ptr2, ptr, 2): { 10, 20, 99, 0, 0 }
Memory freed
3. Array Algorithms (C-style)
-----------------------------
Original array...: { 64, 34, 25, 12, 22, 11, 90 }
After bubble sort: { 11, 12, 22, 25, 34, 64, 90 }
After quick sort.: { 11, 12, 22, 25, 34, 64, 90 }
Binary search: found 25 at index 3
Linear search: found 90 at index 6
4. 2D Arrays / Matrices (C-style)
---------------------------------
3x3 Matrix:
1 2 3
4 5 6
7 8 9
Matrix element[1][1] = 5
After matrix[1][1] = 99:
1 2 3
4 99 6
7 8 9
5. Pointer Arithmetic Simulation
--------------------------------
Original array........: { 100, 200, 300, 400, 500 }
After *(ptr + 2) = 350: { 100, 200, 350, 400, 500 }
Traversal using pointer-style:
*(ptr + 0) = 100
*(ptr + 1) = 200
*(ptr + 2) = 350
*(ptr + 3) = 400
*(ptr + 4) = 500
6. Array Operations (string-like)
---------------------------------
Source array.................: { 1, 2, 3, 4, 5 }
After array_copy (3 elements): { 1, 2, 3, 0, 0 }
Array comparison result: 0 (0=equal, -1=less, 1=greater)
C-style array operations completed!
=============================
Functions and Classes
func ring_malloc size
func ring_calloc count
func ring_free ptr
func ring_memset ptr,value,count
func ring_memcpy dest,src,count
func c_array_init values
func array_copy dest,src,count
func array_compare arr1,arr2,count
func array_search arr,size,target
func bubble_sort arr,n
func quick_sort arr,low,high
func partition arr,low,high
func binary_search arr,left,right,target
func min_val a,b
Class CArray
func init_array array_size
func get_value index
func set_value index,value
func get_size
func clear_memory
func deallocate
func print_array
Class CMatrix
func init_matrix num_rows,num_cols
func get_matrix row_index,col_index
func set_matrix row_index,col_index,value
func deallocate
func print_matrix