C Implementation of the iota Perform (Growing Sequence)

Advertisements

[ad_1]

The “iota” perform just isn’t part of the usual C library. Nevertheless, it’s obtainable within the C++ Normal Library, particularly within the <numeric> header file, the place it generates an rising sequence.

The equal performance may be applied in C. The next perform, named iota, fills an array with an rising sequence beginning with a given worth.

1
2
3
4
5
6
7
#embody <stdlib.h>
 
void iota(int* arr, size_t measurement, int begin) {
    for (size_t i = 0; i < measurement; i++) {
        arr[i] = begin + i;
    }
}
#embody <stdlib.h>

void iota(int* arr, size_t measurement, int begin) {
    for (size_t i = 0; i < measurement; i++) {
        arr[i] = begin + i;
    }
}

This perform takes three parameters:

  • arr is the array to be full of an rising sequence
  • measurement is the scale of the array
  • begin is the beginning variety of the sequence

The perform then iterates over the array, assigning every ingredient a worth which is the sum of the beginning quantity and the present index.

The perform modifies the array in-place and doesn’t return a worth. This design selection was made to keep away from dynamic reminiscence allocation contained in the perform, which might require the caller to free the reminiscence afterwards to forestall reminiscence leaks. This fashion, the caller has full management over the reminiscence used for the array.

–EOF (The Final Computing & Know-how Weblog) —

GD Star Ranking
loading…


314 phrases
Final Put up: C Perform of Fisher-Yates Implementation (Random Shuffling Algorithm)
Subsequent Put up: C Implementation of the itoa Perform (Integer to String Conversion)



[ad_2]