Solution for Exercise 19-25
ex1925
#include <stdio.h> #include <stdlib.h> #include <time.h> void create(int *a); void show(int *a); int main() { int r[10]; int *pr; pr = r; create(pr); show(pr); return(0); } void create(int *a) { int x,r; srand((unsigned)time(NULL)); for(x=0;x<10;x++) { r = rand(); r%=10; *a = r; a++; } } void show(int *a) { int x; for(x=0;x<10;x++) printf("%d\n",*a++); }
Notes
* The stdlib.h
and time.h
header files are required for the srand() and rand() functions, found at Lines 24 and 27, respectively.
* The r%=10
operation at Line 28 ensures that random values in the range of 0 through 9 are stored in the array.
* The array-to-pointer notation isn't used in this code because variable a
walks through the array one unit at a time in both loops.
* Here is sample output, although the values are random and differ from run to run:
Copyright © 1997-2024 by QPBC.
All rights reserved