Question

In: Computer Science

In C++ Make changes to List.h and List.cpp, but not City.h or City.cpp. Project includes Cities01.txt...

In C++

Make changes to List.h and List.cpp, but not City.h or City.cpp.

Project includes Cities01.txt , City.h ,City.cpp , List.h , List.cpp , and Trial.cpp

Cities01.txt

Lansing 42.73 -84.55
Detroit 42.33 -83.04
Flint 43.01 -83.68
Grand-Rapids 42.96 -85.66
Jackson 42.27 -84.47
Kalamazoo 42.23 -85.55
Ann-Arbor 42.22 -83.75
Mt-Pleasant 43.60 -84.78
Clare 43.82 -84.77
Saginaw 43.42 -83.95

City.h

// City class definition

class city
{ friend class list;
public:
city(); // Constructor
bool get(istream& in); // Input name & location
void put(ostream& out); // Output data
private:
string name; // Name
float latitude,longitude; // Location
};
List.h

#define LIST_SIZE 20

#include "City.h"

class list
{ public:
list(); // Constructor - empty list
bool insert(city arg); // Add a city
void display(ostream& out); // Output data
void sort(string arg); // Sort by distance from arg
int size(); // Return number of cities
private:
int len; // Number of used cities
city map[LIST_SIZE]; // Data set
};
City.cpp

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

#include "City.h"

/**************************************
* Constructor - no parameters
**************************************/
city::city()
{ name = "";
latitude = longitude = 0.0F;
}

/**************************************
* Get
**************************************/
bool city::get(istream &in)
{ in >> name;
in >> latitude;
in >> longitude;
return in.good();
}

/**************************************
* Put
**************************************/
void city::put(ostream &out)
{ out << left;
out << setw(14) << name;
out << right << fixed << setprecision(2);
out << setw(8) << latitude;
out << setw(8) << longitude;
}
List.cpp

#include <iostream>
#include <iomanip>
using namespace std;

#include "List.h"

/*************************************
* list()
*************************************/
list::list()
{ len = 0;
}

/*************************************
* insert()
*************************************/
bool list::insert(city arg)
{

// Check to see if there is room

if(len>=LIST_SIZE) return false;

// Add to array
  
map[len++] = arg;

// Return success

return true;
}

/*************************************
* display()
*************************************/
void list::display(ostream &out)
{
}

/*************************************
* size()
************************************/
int list::size()
{ return len;
}
Trial.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "List.h"

/*************************************
* main()
*************************************/
void main()
{ string name;
fstream infile;
city c;
list state;

// Load from file

cout << "Enter file name: ";
cin >> name;
cout << endl;

infile.open(name,ios::in);
if(!infile.is_open()) return;

while(c.get(infile)) state.insert(c);

infile.close();

// Display

cout << state.size() << " Cities" << endl << endl;
state.display(cout);

/* Steps 10.6-7
// Sort by closest to here and display

cout << "Enter home city: ";
cin >> name;
cout << endl;

state.sort(name);

state.display(cout);
*/
}

Could use some help with implementing the list::display() function so the output looks like this:

Enter file name: Cities01.txt

10 Cities

City               Lat    Long

-------------- ------ ------

Lansing         42.73 -84.55

Detroit          42.33 -83.04

Flint            43.01 -83.68

Grand-Rapids     42.96 -85.66

Jackson          42.27 -84.47

Kalamazoo        42.23 -85.55

Ann-Arbor        42.22 -83.75

Mt-Pleasant     43.60 -84.78

Clare            43.82 -84.77

Saginaw          43.42 -83.95

Create a private function list::dist() that takes two integers src and dst, and calculates the distance between the cities at index src and dst. Hint:

Distance = Sqrt((latsrc-latdist)^2+(longsrc-longdist)^2)

Update the display() function so it calls the distance() function to display the distance between each city and the first city in the list. Uncomment the section in main() for steps 10.6-7. Implement the list::sort() function in two parts:

  • Search for the matching city name in the list. Swap that city with the first element in the list.
  • Use a bubble or selection sort to order the cities in ascending order based on the distance from the home city (map index 0).

Output should look like this:

Enter file name: Cities01.txt

10 Cities

City               Lat    Long    Dist

-------------- ------ ------ ------

Lansing         42.73 -84.55    0.00

Detroit         42.33 -83.04    1.56

Flint            43.01 -83.68    0.91

Grand-Rapids     42.96 -85.66    1.13

Jackson          42.27 -84.47    0.47

Kalamazoo       42.23 -85.55    1.12

Ann-Arbor       42.22 -83.75    0.95

Mt-Pleasant     43.60 -84.78    0.90

Clare            43.82 -84.77    1.11

Saginaw          43.42 -83.95    0.91

Enter home city: Lansing

City              Lat    Long    Dist

-------------- ------ ------ ------

Lansing         42.73 -84.55    0.00

Jackson         42.27 -84.47    0.47

Mt-Pleasant     43.60 -84.78    0.90

Flint            43.01 -83.68    0.91

Saginaw          43.42 -83.95    0.91

Ann-Arbor       42.22 -83.75    0.95

Clare            43.82 -84.77    1.11

Kalamazoo        42.23 -85.55    1.12

Grand-Rapids     42.96 -85.66    1.13

Detroit          42.33 -83.04    1.56

Solutions

Expert Solution

SOLUTION -

The code written in bold is edited code.

In this code setter and getter methods are also added to set and get the value of fields as they are private

also swap function and toRadians functions are used which does the same thing as the name suggests .And also selection sort is used to sort the cities

City.h

// City class definition

class city
{ friend class list;
public:
city(); // Constructor
bool get(istream& in); // Input name & location
void put(ostream& out); // Output data

float getlat(); // get latitude

float getlong();// get longitude

string getname(); // get name

void setlat(float lat); // set latitude

void setlong(float long); // set longitude

void setname(string name); //set name


private:
string name; // Name
float latitude,longitude; // Location
};
List.h

#define LIST_SIZE 20

#include "City.h"

class list
{ public:
list(); // Constructor - empty list
bool insert(city arg); // Add a city

void dist(city src,city  desc);//distance between two cities

float toRadians(float degree); //convert degree to radians

void swap(city a,city b); // swap to cities values

void display(ostream& out); // Output data
void sort(string arg); // Sort by distance from arg
int size(); // Return number of cities
private:
int len; // Number of used cities
city map[LIST_SIZE]; // Data set
};
City.cpp

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

#include "City.h"

/**************************************
* Constructor - no parameters
**************************************/
city::city()
{ name = "";
latitude = longitude = 0.0F;
}

/**************************************
* Get
**************************************/
bool city::get(istream &in)
{ in >> name;
in >> latitude;
in >> longitude;
return in.good();
}

void city::setlat(float lat) //setter method for latitude

{

latitude=lat;

}

void city::setlong(float long ) //setter method for longitude

{

longitude=long;

}

void city:: setname(string n) //setter method for name

{

name=n;

}

float city::getlat() // getter method for latitude

{

return lat;

}

float city:: getlong() //getter method for longitude

{

return long;

}

srtring city:: getname() //getter method for name

{

return name;

}

/**************************************
* Put
**************************************/
void city::put(ostream &out)
{ out << left;
out << setw(14) << name;
out << right << fixed << setprecision(2);
out << setw(8) << latitude;
out << setw(8) << longitude;
}
List.cpp

#include <iostream>
#include <iomanip>
using namespace std;

#include "List.h"

/*************************************
* list()
*************************************/
list::list()
{ len = 0;
}

/*************************************
* insert()
*************************************/
bool list::insert(city arg)
{

// Check to see if there is room

if(len>=LIST_SIZE) return false;

// Add to array
  
map[len++] = arg;

// Return success

return true;

}

void list:: swap(city a,city b)

{

// swapping the latitude values

float temp=a.getlat();

a.setlat(b.getlat());

b.setlat(temp);

// swapping the longitude values

temp=a.getlong();

a.setlong(b.getlong());

b.setlong(temp);

// swapping the name values

string t=a.getname();

a.setname(b.getname());

b.setname(t);

}

/**********************

*toRadians()

***************/

float toRadians(float degree)

{

float one_deg = (M_PI) / 180;

    return (one_deg * degree);

}

/**********************

*dist()

***************/

void list::dist(city a ,city b)

{

float lat1=a.getlat();

float long1=a.getlong();

float lat2=b.getlat();

float long2=b.getlong();

    lat1 = toRadians(lat1);

    long1 = toRadians(long1);

    lat2 = toRadians(lat2);

    long2 = toRadians(long2);

     

    float dlong = long2 - long1;

float dlat = lat2 - lat1;

float ans = pow(sin(dlat / 2), 2) +

                          cos(lat1) * cos(lat2) *

                          pow(sin(dlong / 2), 2);

    ans = 2 * asin(sqrt(ans));

    

float R = 6371;

     

    ans = ans * R;

    return ans;

}

/*************************************
* display()
*************************************/
void list::display(ostream &out)
{

for(int i=1;i<len;i++)

{
cout<<"distance between the city first and city "<<i <<"is"<<distance(map[0],map[1])<<"\n";

}


}

void list ::sort(string arg)

{

for(int i=0;i<len;i++)

{

if(map[i].getname()==arg)

{ swap(map[i],map[0]);

break;

}

}

for(int i=1;i<n;i++)

{  

float dis=dist(map[i],map[0]);

int min=i;

for(int j=i+1;j<n;j++)

{

  float D=dist(map[j],map[0]);

if(D<dis)

{

dis=D;

min=j;
}

swap(map[i],map[min]);

}

}

/*************************************
* size()
************************************/
int list::size()
{ return len;
}
Tria.cpp


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "List.h"

/*************************************
* main()
*************************************/
void main()
{ string name;
fstream infile;
city c;
list state;

// Load from file

cout << "Enter file name: ";
cin >> name;
cout << endl;

infile.open(name,ios::in);
if(!infile.is_open()) return;

while(c.get(infile)) state.insert(c);

infile.close();

// Display

cout << state.size() << " Cities" << endl << endl;
state.display(cout);

/* Steps 10.6-7
// Sort by closest to here and display

cout << "Enter home city: ";
cin >> name;
cout << endl;

state.sort(name);

state.display(cout);
*/
}


Related Solutions

Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
C++ make a rational class that includes these members for rational -private member variables to hold...
C++ make a rational class that includes these members for rational -private member variables to hold the numerator and denominator values -a default constructor -an overloaded constructor that accepts 2 values for an initial fraction -member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument) -a member function that accepts an argument of type of ostream that writes the fraction to that open output stream do not let...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Also, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students...
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students to generate a linked list of playing card based on data read from a file and to write out the end result to a file. linkedList.h Create a header file name linkedList Include the following C header files: stdio.h stdlib.h string.h Create the following macros: TRUE 1 FACES 13 SUITS 4 Add the following function prototypes: addCard displayCards readDataFile writeDataFile Add a typedef struct...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT