C++ Primer Plus Chapter 3 Exercise 3

c plus plusThis solution actually took me a second to figure out as I had issues deciphering what was really being said here. Here is what I came up with:

3. Write a program that asks the user to enter a latitude in degrees, minutes, and seconds
and that then displays the latitude in decimal format. There are 60 seconds of arc to a
minute and 60 minutes of arc to a degree; represent these values with symbolic constants.
You should use a separate variable for each input value. A sample run should
look like this:
Enter a latitude in degrees, minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees

#include <iostream>

using namespace std;

int main()
{

int degrees;
int minutes;
int seconds;

// Gather input
cout << "Enter your latitude in degrees, minutes, and seconds: " << endl;
cout << "Enter your degrees: ";
cin >> degrees;
cout << "Enter your minutes of arc: ";
cin >> minutes;
cout << "Enter your seconds of arc: ";
cin >> seconds;
cout << degrees << " degrees, " << minutes << " minutes " << seconds << " seconds = ";

// Calculate
float latitude = float (degrees) + ( float (minutes) +  float (seconds)/60)/60;
cout << latitude << " degrees." << endl;

cin.get();
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s