Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

help in C++

hello guys ..i am studying C++ to the first time and i need i help in doing my assignment :

i will include the whole question and the sample output run wanted and also i will include my solution to help me finding the mstakes i did.

You have been asked to write simple Global Positioning System (GPS) program. Your application should read and process a sequence of waypoints stored in a GPS file. Each waypoint is stored in a single line and consists of the waypoints coordinates (latitude, longitude), as well as altitude (elevation wrt to the sea level), and name.

Example:

latitude longitude altitude waypoints name
23.076 57.499 745.622 CarParking

Your program should calculate the following:

1. Total distance of the route i.e; sum of all distances between waypoints.
2. The highest altitude.
3. The altitude profile.

How to calculate the distance between two waypoints?
If the latitude and longitude of two waypoints p1 and p2, are (a1, b1) and (a2, b2) respectively, then the distance D between waypoints p1, and p2 is given by the following formula:

D = Arccos[Cos[a1] Cos[b1] Cos[a2] Cos[b2] + Cos[a1] Sin[b1] Cos[a2] Sin[b2] + Sin[a1] Sin[a2]]/360 * 2*PI * r

Where,
r is the radius of the earth (6378136 m)
PI= 3.14159

Altitude profile
To have a visual altitude profile, use the character star (*). The highest altitude should be represented with 20 stars (See the sample run).

Input file:
Each waypoint record is stored in a single line in a file named gps.dat
=======================================================
LATITUDE LONGITUDE ALTITUDE NAME
23.076058114 57.499308921 745.622 CARPARKING
23.088451345 57.502853293 1522.843 VIEWPOINT
23.094749339 57.510688277 1639.642 CLIFFS_VILLAGE
23.098353054 57.513631247 1666.559 ROCKTABLE
23.101017829 57.527312189 1722.315 GREY_RAMP
23.098258339 57.541948417 1926.593 ABAND_VILLAGE
23.098221123 57.552689323 1899.917 JARIR
23.089191634 57.551676035 1865.550 MIBINT
23.084712345 57.540917695 1607.438 CAVE_VILLAGE
23.076595981 57.536980044 1638.681 MAAQUL
23.081151880 57.525282009 1529.091 BALCONY
23.084797254 57.515532514 1065.739 DESCENT
23.083009645 57.509667529 773.981 AL_FARA

OUTPUT sample run:
GPS PROGRAM

DATA
LATITUDE LONGITUDE ALTITUDE NAME
23.076 57.499 745.622 CARPARKING
23.088 57.503 1522.843 VIEWPOINT
23.095 57.511 1639.642 CLIFFS_VILLAGE
23.098 57.514 1666.559 ROCKTABLE
23.101 57.527 1722.315 GREY_RAMP
23.098 57.542 1926.593 ABAND_VILLAGE
23.098 57.553 1899.917 JARIR
23.089 57.552 1865.550 MIBINT
23.085 57.541 1607.438 CAVE_VILLAGE
23.077 57.537 1638.681 MAAQUL
23.081 57.525 1529.091 BALCONY
23.085 57.516 1065.739 DESCENT
23.083 57.510 773.981 AL_FARA

ALTITUDE PROFILE

745.622********
1522.840****************
1639.640******************
1666.560******************
1722.320******************
1926.590********************
1899.920********************
1865.550********************
1607.440*****************
1638.680******************
1529.090****************
1065.740************
773.981*********

HIGHEST ALTITUDE
1926.593 METER.

TOTAL DISTANCE
9155.413 METER.

BYE!
PRESS ANY KEY TO CONTINUE
=====================================================

my program is:

====================================================
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<iomanip>

using namespace std;
double arccos (double);
int main()
{
double lat, longt, alt,h_alt(0), t_dist(0), D(0);
double a1(0), b1(0), a2(0), b2(0);
string name;
int n(0);
const double PI=3.14159;
const double r=6378136;
ifstream infile;
ifstream ins;
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(3);

infile.open("gps.dat");
ins.open("gps.dat");
if(!infile || !ins)
{
cout<<"files opening error"<<endl;
return 1;
}

cout<<"GPS PROGRAM"<<endl<<endl;
cout<<"DATA"<<endl;

cout<<setw(10)<<"LATITUDE"<<setw(15)<<"LONGITUDE"<<setw(10)<<"ALTITUDE"<<setw(15)<<"NAME"<<endl;
infile>>lat>>longt>>alt>>name;

while(!infile.eof())
{
a1=lat;
b1=longt;
cout<<setw(8)<<lat<<setw(17)<<longt<<setw(12)<<alt<<setw(17)<<name<<endl;



infile>>lat>>longt>>alt>>name;

a2=lat;
b2=longt;
D = arccos((cos(a1)*cos(b1)*cos(a2)*cos(b2))+(cos(a1)*sin(b1)*cos(a2)*sin(b2))) +(sin(a1)*sin(a2)/(360*2*r))+D;


t_dist = t_dist+D;

if(alt>=h_alt)
h_alt = alt;

}

cout<<"ALTITUDE PROFILE"<<endl;

infile>>lat>>longt>>alt>>name;

while (!ins.eof())
{
n=(int)alt/100;

for (int i=0;i<=n;i++)
{
cout<<"*";

}

infile>>lat>>longt>>alt>>name;

}

cout<<"HIGHEST ALTITUDE"<<endl;
cout<<h_alt<<" METER."<<endl<<endl;

cout<<"TOTAL DISTANCE"<<endl;
cout<<t_dist<<" METER."<<endl<<endl;

cout<<"BYE!"<<endl<<endl;

infile.close();
ins.close();
return 0;
}

double arccos (double x)
{

double result(0);

result=1/cos(x);

return result ;

}
=====================================================

i need just to know how to print those stars and also how to calculate the total distance. :confused:
[6794 byte] By [dani1122] at [2007-11-11 8:25:43]
# 1 Re: help in C++
In order to print the stars properly you need to have looked at all of the altitudes, determined the highest, determine the weight value of each star (since each star is 1/20th of the highest altitude). Then, when you print out a given altitude, you follow that value with the number of stars equal to the floor of altitude divided by 1/20th of the highest altitude.

You have the formula for total distance - and it appears you are set to calculate each distance - so is it working?

To produce this print out properly, I would use some container which would allow me to store altitude, distance, and name of the reference point. Do you know about arrays? do you know about structs? do you know about classes?

if you are fairly early in your study of C++, you probably are using arrays. Therefore, use "parallel arrays" - where all information about point 0 is stored at the combination of latitudes[0]. longitudes[0]. altitudes[0], names[0], distances[0]. As you process each point from the data in your infile, you move to the next index of your arrays and store the data. Print out your table by referring to the index of each of the relevant arrays. Then you can obtain the highest altitude, determine the weight of each star, and then print out your contour map. Finally, you can calculate your distances, enter the result into the relevant distances[] element, and then total the values.
nspils at 2007-11-11 21:01:20 >
# 2 Re: help in C++
mmm How accurate is that formula I wonder? I am doing this same stuff on the job and we used the formula that 1 min of lattitude is 1 nautical mile and the longitude has a cos(lat - in - decimal - degrees) term to correct for the width changing. It seems to be pretty good. On that note arc-cos instead of atan2 seems risky with the quadrant issues, and on top of that remember that sin and cos take radians so 360 is probably a mistake.

Also, while 4 or so digits of PI is fine for most things, in the gps world your going to be way off without using more. Try 3.1415926535897932384626433832795
jonnin at 2007-11-11 21:02:27 >
# 3 Re: help in C++
The only problem with 3.1415926535897932384626433832795 is that the built-in float and double can't support this precision level. long double might be closer, but still not precise enough. I believe that 15 digits after the decimal point should be sufficient.
Danny at 2007-11-11 21:03:19 >
# 4 Re: help in C++
I thought that was the 80-bit version? Or is it still too long? I cant remember ...
jonnin at 2007-11-11 21:04:25 >
# 5 Re: help in C++
The 80 bit version (i.e., long double) I believe has up to 18 decimal numbers.
Danny at 2007-11-11 21:05:30 >
# 6 Re: help in C++
Ah ok. I used windows calc, which I thought was 80-bit. I didnt count the digits =)
jonnin at 2007-11-11 21:06:29 >
# 7 Re: help in C++
Thanx all....actually i dont know anything about arrays or strct..i am sorry the formula misses PI=3.14159 multiplied by 360*2*r

i still have the problem..
dani1122 at 2007-11-11 21:07:33 >
# 8 Re: help in C++
Your exercise cries out to me for the need of a method to store the data being used from the input file, to allow manipulation of the data. Otherwise, it involves many wasteful processings of the input file. If you're doing this exercise as a part of a class, I'd suggest you ask your instructor for guidance on what he/she wants in the program. If this is self-study, I'd suggest it is time to start learning about arrays.
nspils at 2007-11-11 21:08:31 >
# 9 Re: help in C++
The total distance is the sum of the distances.
To do the stars, find the smallest height, and the largest. the smallest is zero stars and the largest is (max) whatever you want that to be (say 20 for example). Then you scale it so print stars.

so if you had 100 is smallest, and 1000 is largest, then 1000 = 20 and 100 = 0 so you set up a ratio of how many to print using that, of course since its ascii and you have to print a full star (no partial star printings) so you round up or down when you print.
jonnin at 2007-11-11 21:09:29 >