Question

I'm learning c++

CS 1320 Spring 2019 Lab Assignment #4 Objectives: . Learn to define an use an array of structures Learn to define a function

exoplanets- Notepad File Edit Format View Help TRAPPİST-1 e 39 0.87 GJ 667 C f 0.87 GJ 667 Ce 0.71 Proxima Cen b 4.2 0.87 Kep

Untitled Notepad File Edit Format View Help The exoplanet.txt has information from 5 exo planet first the name, the distance

CS 1320 Spring 2019 Lab Assignment #4 Objectives: . Learn to define an use an array of structures Learn to define a function to process the array based on one of the properties Instructions: Part 1. Exoplanets are planets outside our solar system, orbiting another star. Identification of the properties that may possibly indicate an "Earth-like" (potentially habitable) exoplanet is an ongoing and experimental task. Observations using NASA's Kepler, Hubble, Spitzer and other space telescopes, as well as data from ground stations, have been compiled into lists of potential candidates, along with their properties such as size, composition, and distance from their sun. This programming assignment requires you to define an array of structures with typedef exoplanet (alias exop) which will be used to group together properties for instances of exoplanets. The properties for the exoplanets are recorded in an input data file name exoplanets.txt. This file contains data compiled by The University of Puerto Rico at Arecibo's Planetary Habitability Laboratory, including NASA and other data. Properties of interest to us are: exoplanet name (string), distance from Earth (in light years, integer), and PHL's numeric estimate of similarity to Earth (ESI, a floating point value) Use an eof-controlled while-loop to read in and save the data for each exoplanet listed in the file. By using structures, you will be able to group and more easily process the various types of data for exoplanets without having to use multiple arrays. Display all the data for each exoplanet in the file before going to Part 2 Part 2. Define an additional function in your program, as described below. If you define the function above main, no prototype statement is necessary. I f you define the function below main, include a prototype statement for the function, placed above main. Let us assume that we would like to focus further scientific investigation on those exoplanets that are closest to Earth. We would like to display the name, distance, and ESI value for the closest exoplanet to earth. findClosest: This function takes the array of exoplanet structures and the length of the array (use a variable for the length) and returns the index position of the closest exoplanet in the array. After the function has finished execution, all data about the closest exoplanet should be displayed in main Add another function name sortDistances, which sorts the array of exoplanets in order of closest to farther distance away from Earth. Print out all data for each exoplanet to verify correct sorting
exoplanets- Notepad File Edit Format View Help TRAPPİST-1 e 39 0.87 GJ 667 C f 0.87 GJ 667 Ce 0.71 Proxima Cen b 4.2 0.87 Kepler-62 f 1200 0.69
Untitled Notepad File Edit Format View Help The exoplanet.txt has information from 5 exo planet first the name, the distance measure in light years, and the exo number
0 0
Add a comment Improve this question Transcribed image text
Answer #1

You should have provided the files instead of their screenshots

Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

#include <bits/stdc++.h>
using namespace std;
struct exoplanet
{
string name;
int composition;
double distance;
};
int findClosest(vector <exoplanet>v, int n)
{
int result = 0;
int minTillNow = v[0].distance;
for(int i=1;i<n;i++)
{
if(minTillNow>v[i].distance)
{
minTillNow = v[i].distance;
result = i;
}
}
return result;
}
int main()
{
ifstream file;
string line,filename,word;
stringstream ss;
vector <exoplanet>v; //To store detail of every employee
double temp;
exoplanet e;

filename = "exoplanet.txt"; //File name
file.open(filename); // open file
while (getline(file,line)) //line will now contain entire row of information
{
e.name = line;
getline(file,line); //line will now contain composition in string format
ss<<line;
ss>>e.composition;
ss.clear();
getline(file,line); //line will now contain distance in string format
ss<<line;
ss>>e.distance;
ss.clear();
v.push_back(e); //Add this planet to the vector
}
//Display details
for(int i=0;i<v.size();i++)
{
cout<<" "<<v[i].name<<"\t "<<v[i].composition<<"\t "<<v[i].distance<<endl;
}
int ind = findClosest(v,v.size());
cout<<"Planet closest to earth is "<<v[ind].name;
return 0;
}

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.

Add a comment
Know the answer?
Add Answer to:
I'm learning c++ CS 1320 Spring 2019 Lab Assignment #4 Objectives: . Learn to define an use an array of structures Learn to define a function to process the array based on one of the properties...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT