Sams Teach Yourself C in 21 Days

(singke) #1
for (a = 0; a < 1000; a++)
{
printf(“\nrandom[%d] = “, a);
printf(“%d”, random[a]);
if ( a % 10 == 0 && a > 0 )
{
printf(“\nPress Enter to continue, CTRL-C to quit.”);
getchar();
}
}
return 0;
} /* end of main() */


  1. The following are two solutions. The first initializes the array at the time it is
    declared, and the second initializes it in a forloop.
    Answer 1:
    #include <stdio.h>
    / Declare a single-dimensional array /
    int elements[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int idx;
    int main( void )
    {
    for (idx = 0; idx < 10; idx++)
    {
    printf( “\nelements[%d] = %d “, idx, elements[idx] );
    }
    return 0;
    } / end of main() /
    Answer 2:
    #include <stdio.h>
    / Declare a single-dimensional array /
    int elements[10];
    int idx;
    int main( void )
    {
    for (idx = 0; idx < 10; idx++)
    elements[idx] = idx ;


for (idx = 0; idx < 10; idx++)
printf( “\nelements[%d] = %d “, idx, elements[idx] );

return 0;
}

850 Appendix F

49 448201x-APP F 8/13/02 11:22 AM Page 850

Free download pdf