CPE 101

Laboratory 10

    To experience and understand records/structures in C.
    To learn about and create arrays of records.
    To practice using end-file loops.

    Your instructor, peers, texts, and your own innate capabilities and resourcefulness!

This lab will involve developing a small C program which handles data about planets.  You will be creating your own type, an array of structs, in order to store the planet information for a star system.  You will need to develop a variety of functions to process this data.  The assignment does not provide every detail needed to create a solution.  You will have to think through the requirements and fill in some details of the solution on your own.

Part 1: Creating the data type and reading in data.

1. Create a struct defining a planet based on this skeleton:
typedef struct
      /* A planet name (25 char maximum) (assume no embedded blanks). */
      /* The planet’s equatorial diameter (km.) (double) */
      /* The planet’s distance to the Earth (km.) (double) */
      /* The number of natural satellites (i.e. moons) the planet has (int). */
} planetData;

2. Create a function which reads an array of planetData.  Write the appropriate code to scan and parse the planet data until end of file and store it in the array of planetData. (Assume a maximum of 100 planets).

sample input file

Part 2: Displaying the data.

1. Write a function that is passed one planet record and displays that planet's data.  For example:
name: Earth
diameter: 6378.1 km.
distance to the Earth: 0 km.
number of moons: 1
(Tip: the "%e" formatter will display scientific notation.)

2. Write a function that is passed an array of planets and uses a loop (and the previous function) to print out the data for each planet data in the array:
Planet 1:
        name: Mercury
        diameter: 2439.7 km.
        distance to the Earth: 2.219e+08 km.
        number of moons: 0
Planet 2:
        name: Venus
        diameter: 6051.8 km.
        distance to the Earth: 2.61e+08 km.
        number of moons: 0
Part 3: Analyze the data.

Next, you will be adding functions in order to perform specific analyses on the data:

1.    Return the planet which is closest to the Earth.
2.    Return the planet with the most moons.
3.    Return the planet with the smallest diameter.
4.    Return the average diameter of all the planets
5.    Given a string, return the record for that planet if it exists.  Otherwise, return an "empty" planet.

Part 4: Test driver

Write a main function to call each of your analysis functions, then print out the result.

Note that when your program is graded it will be tested with a fictitious star system with different planets than our own.

Name your source file planets.c and submit it via handin to Lab10.