I’m trying to write a function to fill an array with 40 random uppercase letters. I attempted to fill the array and print it but I’m not getting any output.
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
void fillS1(char y[]);
int main(int argc, const char * argv[])
{
char s1[40];
int n;
fillS1(&s1[40]);
for (n = 0; n < 41; n++) {
printf("%s", s1);
}
return 0;
}
void fillS1(char y[40]){
int x = 1;
while (x < 41) {
x = rand() % 26;
x = 2 + 'A';
x = y[x];
x++;
}
}
I would just code
and use it as (e.g. in your
main)Notice that you need an extra byte for the null terminating character. Ad we are assuming some ASCII compatible char encoding. A more portable way could have been
since literal strings are constant arrays of chars.
If you code in C++, see the standard
<random>header file. If you code for Linux, see random(4) and getrandom(2), perhaps use it with srandom(3). Read also about the Mersenne Twister