Solution for Exercise 13-16
ex1316
#include <stdio.h> int main() { char pres[4][2][11] = { "George", "Washington", "John", "Adams", "Thomas", "Jefferson", "James", "Monroe" }; int loop; for(loop=0;loop<4;loop++) printf("%-6s %-10s\n",pres[loop][0],pres[loop][1]); return(0); }
Notes
* That's a three dimensional array defined at Line 5: 4 rows, 2 columns, 10 characters maximum per name, plus 1 for the null character, \0
.
* Code::Blocks may balk at the array's assignment, as it crosses several lines. You may see a "missing braces" warning, which is okay to ignore.
* To display each string, the variables press[loop][0]
and pres[loop][1]
are used, representing the President's first and last names, respectively. Those are string variables, not characters, because pres
is a three-dimensional array.
* The conversion characters %-6s
and %-10s
left align the text with a space between them (look at the formatting string at Line 14). The longest first name is six letters long (two of them), and Washington
is the longest last name at 10 characters.
Copyright © 1997-2024 by QPBC.
All rights reserved