I'm learning c++
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.
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...
Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment 1 ID: 1. True / False 2. True / False 3. True / False 4. True / False 5. True / False 6. True / False 7. True / False 8. True / False 9. True / False 10. True / False Variable and functions identifiers can only begin with alphabet and digit. Compile time array sizes can be non-constant variables. Compile time array...
Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...