Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program to list trees in nursery catalog according to average height
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // Constants:
- #define MAX_TREES 5 /* maximum number of trees in main program array */
- #define MAX_NAME 40 /* 1 + maximum length of a tree's common name */
- // Tree description
- typedef struct tree_type
- {
- int cat_no; // catalog number; 1000-1999 for trees
- char variety[MAX_NAME]; // common name of this tree
- int avg_height; // average height at full growth, in feet
- } tree_type;
- int main(void)
- {
- // First version of program uses fixed data
- tree_type trees[MAX_TREES] = {
- { 1001, "Aspen", 48 },
- { 1002, "Blue Spruce", 64 },
- { 1003, "Dogwood", 21 },
- { 1004, "Ponderosa Pine", 95 },
- { 1005, "Walnut", 80 },
- };
- // List all trees in array:
- printf("Catalog Expected\n");
- printf("Number Height Common Name\n");
- for (int i=0; i<MAX_TREES; ++i)
- {
- printf(" %4d %3d ft. %s\n",
- trees[i].cat_no,
- trees[i].avg_height,
- trees[i].variety);
- }
- // Selectively list array entries matching a condition:
- printf("\nTrees under 60 foot height limit:\n");
- int nprinted = 0; // count number of lines printed
- for (int i=0; i<MAX_TREES; ++i)
- {
- if (trees[i].avg_height <= 60)
- {
- ++nprinted; // add to count before printing to use as line #
- printf("%2d. %s (%d)\n",
- nprinted,
- trees[i].variety,
- trees[i].avg_height);
- }
- }
- if (nprinted == 0) // test for no lines printed in loop
- printf(" ***None***\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement