You are on page 1of 1

Answers: Practise Exercise 7: Arrays

1. Declare a character based array called letters of ten elements


char letters[10];
2. Assign the character value 'Z' to the fourth element of the letters array
letters[3] = 'Z';
3. Use a for loop to total the contents of an integer array called numbers which
has five elements. Store the result in an integer called total.
for( loop = 0, total = 0; loop < 5; loop++ )
total = total + numbers[loop];
4. Declare a multidimensioned array of floats called balances having three rows
and five columns.
float balances[3][5];
5. Write a for loop to total the contents of the multidimensioned float array ba
lances.
for( row = 0, total = 0; row < 3; row++ )
for( column = 0; column < 5; column++ )
total = total + balances[row][column];
6. Assign the text string "Hello" to the character based array words at declarat
ion time.
static char words[] = "Hello";
7. Assign the text string "Welcome" to the character based array stuff (not at d
eclaration time)
char stuff[50];
strcpy( stuff, "Welcome" );
8. Use a printf statement to print out the third element of an integer array cal
led totals
printf("%d\n", totals[2] );
9. Use a printf statement to print out the contents of the character array calle
d words
printf("%s\n", words);
10. Use a scanf statement to read a string of characters into the array words.
scanf("%s", words);
11. Write a for loop which will read five characters (use scanf) and deposit the
m into the character based array words, beginning at element 0.
for( loop = 0; loop < 5; loop++ )
scanf("%c", &words[loop] );

You might also like