Initial commit
This commit is contained in:
48
examples/arrays.cm
Normal file
48
examples/arrays.cm
Normal file
@@ -0,0 +1,48 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// arrays.cm - Array operations
|
||||
// Demonstrates: arrays, loops, array initialization
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
int32 sum_array(int32 *arr, int32 len) {
|
||||
int32 total = 0;
|
||||
for (int32 i = 0; i < len; i = i + 1) {
|
||||
total = total + arr[i];
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
void reverse_array(int32 *arr, int32 len) {
|
||||
int32 i = 0;
|
||||
int32 j = len - 1;
|
||||
while (i < j) {
|
||||
int32 temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
i = i + 1;
|
||||
j = j - 1;
|
||||
}
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
int32 numbers[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
|
||||
printf("Original array: ");
|
||||
for (int32 i = 0; i < 10; i = i + 1) {
|
||||
printf("%d ", numbers[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
int32 total = sum_array(numbers, 10);
|
||||
printf("Sum: %d\n", total);
|
||||
|
||||
reverse_array(numbers, 10);
|
||||
|
||||
printf("Reversed array: ");
|
||||
for (int32 i = 0; i < 10; i = i + 1) {
|
||||
printf("%d ", numbers[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user