// Starter code for homework 8. You are free to use this code in your // programs. However you are responsible for knowing what is does and // for completing the documentation of it! #include #include #include #include #define NDIM 3 #define MAXNAME 80 struct planet { double absolute_magnitude; double true_anomaly; double angle_of_perihelion; double angle_of_ascending_node; double inclination; double eccentricity; double semimajor_axis; char name[MAXNAME]; // Add any other variables you want! }; #define MAXPLANETS 1000 int read_file(char *filename, struct planet P[]) { // Read the given file and put the information into the array of planets. // Return the number of planets in the list. FILE *fp; int nplanets; char line[200]; fp = fopen(filename,"r"); if (fp==NULL) { fprintf(stderr,"File %s not found.\n", filename); abort(); } nplanets = 0; while (fgets(line,200,fp)!=NULL) { if (line[0]=='#') continue; sscanf(line, "%lf %lf %lf %lf %lf %lf %lf", &(P[nplanets].absolute_magnitude), &(P[nplanets].true_anomaly), &(P[nplanets].angle_of_perihelion), &(P[nplanets].angle_of_ascending_node), &(P[nplanets].inclination), &(P[nplanets].eccentricity), &(P[nplanets].semimajor_axis)); strncpy(P[nplanets].name, line+74, MAXNAME); P[nplanets].name[MAXNAME-1] = '\0'; nplanets++; if (nplanets>=MAXPLANETS) break; } fclose(fp); printf("Read %d planets from file %s.\n", nplanets, filename); return nplanets; } int main() { struct planet Planets[MAXPLANETS]; int nplanets; nplanets = read_file("MPC_listing_of_TNO.v2.txt", Planets); // Elements Planets[0] to Planets[nplanets-1] are now filled with planet information. return 0; }