• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help for a simple C++ programming problem!

SU27

Junior Member
I am quite a newbie in C++.

I encountered a following problem and do not know how to solve it. Please help me write the code or pseudocode.

Angles are often measured in degrees (°), minutes ('), and seconds (&quot😉. There are 360 degrees in a circle, 60 minutes in one degree, and 60 seconds in one minute. Write a program that reads two angular measurements given in degrees, minutes, and seconds, and then calculates and displays their sum. Use the program to verify the following:

74°29'13'' + 105°8'16" = 179°37'29"
122°17'48" + 237°42'12" = 0°0'0"


Thank very much for your kind help and direction!!
 
At the very least you'll have to implement some sort of add function which knows to wrap around the number when it goes over 59 or 359.
 
You'll need to use the mod function.

There's a few ways to go about this. Here's one:

add the seconds together, take that result and subtract (result mod 60)
that is your total seconds, result mod 60 is leftover minutes

add the minutes together, take that result and subtract (result mod 60)
that is your total minutes (after you add the leftover from the seconds) result mod 60 is leftover degrees.

add the degrees together, mod by 360. That is total degrees.

I think that will work, I did it really fast in my head.
 
Heres something that may help you wrap it around:
You could probably do it alot easier with a class implementation, but this should suffice

1.Add up seconds, minutes, and degrees seaprately(seconds1+seconds2, etc)
2. if the total seconds and minutes are <60, then you can just simply add them
3. if >60 then you use the mod operator. so total_minutes%60 will equal minutes. HOwever, then you must add one to the degree, or one to the minute(if youre using seconds)

//Temp variable
int total=degres1+degrees2;
int total1=minutes1+minutes2;
int total2=seconds1+seconds2;


if (total2<60)
{total_seconds=total2;}
else
{
total_seconds=total2%60;
total1+;
}

if (total1<60)
{total_minutes=total1;}
else
{
total_minutes=total1%60;
total+
}

total_degrees=total



imhotepMP





 
Thanks a lot to all for your kind help! I finally work that out! Actually the program can go without the mod function.

 
Actually the program can go without the mod function.

Well of course you can use simple subtraction to take the place of the modulus operator.
 
Well my solution would be something like

double angle1 = degress + minutes / 60.0 + seconds / 3600.0;
...

double angle = angle1 + angle2;

cout << int (angle) << &quot;degrees &quot; << int (angle * 60) % 60 << &quot; minutes &quot; << int (angle * 3600 + 0.5) % 60 << &quot; seconds&quot;;

Although it is not exact. But close enough, and just with seconds, you shouldn't see any errors unless your degress were up in the 10^8 range. In which case, normalise you angle and bring it back to 0-360

Moohoo
 
Back
Top