Sams Teach Yourself C in 21 Days

(singke) #1
Answers 849

F


int array[5][4];
int a, b;
int main( void )
{
for ( a = 0; a < 5; a++ )
{
for ( b = 0; b < 4; b++ )
{
array[a][b] = rand();
}
}

/* Now print the array elements */
for ( a = 0; a < 5; a++ )
{
for ( b = 0; b < 4; b++ )
{
printf( “%d\t”, array[a][b] );
}
printf( “\n” ); /* go to a new line */
}
return 0;
}


  1. The following is one of many possible answers:
    / random.c: using a single-dimensional array /
    #include <stdio.h>
    #include <stdlib.h>
    / Declare a single-dimensional array with 1000 elements /
    int random[1000];
    int a, b, c;
    long total = 0;


int main( void )
{
/* Fill the array with random numbers. The C library */
/* function rand() returns a random number. Use one */
/* for loop for each array subscript. */

for (a = 0; a < 1000; a++)
{
random[a] = rand();
total += random[a];
}
printf(“\n\nAverage is: %ld\n”,total/1000);
/* Now display the array elements 10 at a time */

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

Free download pdf