Back once again. I'm doing practice problems for an upcoming programming competition and me and my friend are truly stumped.
Question:
Assume that the hands of the clock move continuously around the face. Given the hour and the minute and a desired angle in degrees (0-360) between the hands, determine the smallest time before that angle occurs between the hands. Note that hands do not always line up "on the minute". Inputs will be integer.
Examples
hour: 12
minute: 10
desired angle: 100
output in minutes : 7.4
hour: 5
minute: 5
desired angle: 30
output in minutes : 16.3
Thanks for any tips.
I'm using Visual Studio 2010.
Code:#include<iostream> #include<iomanip> #include<cmath> using namespace std; int main() { int hour = 0; int minute = 0; int desired = 0; double angle = 0; double time = 0; cout << "Enter hour: "; cin >> hour; cout << "Enter minute: "; cin >> minute; cout << "Enter a desired angle: "; cin >> desired; angle = (hour*30 + minute*.5)- minute*6; if (angle > 180) { angle = angle -360; } if(angle < 0) angle = angle/-1; angle = desired - angle; time = angle/6 + ((minute/60)); cout << "Time in minutes: " << time << endl; system("pause"); return 0; }
Okay...so what's the problem actually?
if (angle > 180)
{
angle = angle -360;
}
float hour;
float minute;
float desired;
double angle;
double time;
angle = (minute * 6) - ((hour * 30) + (minute * .5)) ;
// Java method
angle = Math.abs( angle );
if (angle > 180)
angle = 360 - angle;
if (angle > desired)
angle -= desired;
else
angle = desired - angle;
// The distance to move the hour hand
double ticks = angle / 6 * .5;
// The final position of the hour hand (in minutes)
double hours = (minute * hour) + (minute * .5) + ticks;
// The distance between the minutes hand and the hours hand
double tomove = desired / 6.0;
// The final position of the minutes hand
double minutes = hours - tomove;
// The minutes that it takes to get to these positions
time = (angle + (hours - minutes))/6;
#include <stdlib.h>
#include <stdio.h>
typedef struct Item {
void *data;
struct Item *next;
struct Item *prev;
} LinkedList;
struct Item *getnext(struct Item *current) {
return current->next;
}
struct Item *getprev(struct Item *current) {
return current->prev;
}
struct Item *newitem(void *data) {
struct Item *newmem = malloc(sizeof(struct Item));
newmem->data = data;
newmem->next = NULL;
newmem->prev = NULL;
return newmem;
}
void join(struct Item *currentlast,
struct Item *newlast) {
currentlast->next = newlast;
newlast->prev = currentlast;
}
int main() {
// Testing it works
int item1 = 64;
int item2 = 34;
LinkedList *newObj = newitem(&item1);
LinkedList *anotherObj = newitem(&item2);
join(newObj, anotherObj);
// make sure it works. Try accessing the
// 34 value from the item holding 64
printf("%d ", *(int*) newObj->data);
printf("%d ", *(int*) newObj->next->data);
printf("%d ", *(int*) newObj->next->prev->data);
puts("Seems right");
}
struct foobarbaz{
int one;
int two;
int three;
int(*exampleMethod)(int,int);
};
int addTwoNumbers(int a,int b){
return a+b;
}
int main()
{
// Now, define an instance of our struct
// and add some default values.
struct foobarbaz fbb;
fbb.one =1;
fbb.two =2;
fbb.three =3;
// Now add a "method"
fbb.exampleMethod = addTwoNumbers;
// Try calling the method
int test2 = fbb.exampleMethod(13,36);
printf ("test2: %u \n", test2);
printf("\nDone\n");
return0;
}
Better how? It looks functional to me. Technically you don't need a previous pointer (current->next = current->next->next) but there are reasons you might want to use a previous.
Is there a reason you are using C and faking object oriented with a bunch of structs? Usually you'd see function pointers like this (shamelessly stolen from SO )
Code:struct foobarbaz{ int one; int two; int three; int(*exampleMethod)(int,int); }; int addTwoNumbers(int a,int b){ return a+b; } int main() { // Now, define an instance of our struct // and add some default values. struct foobarbaz fbb; fbb.one =1; fbb.two =2; fbb.three =3; // Now add a "method" fbb.exampleMethod = addTwoNumbers; // Try calling the method int test2 = fbb.exampleMethod(13,36); printf ("test2: %u \n", test2); printf("\nDone\n"); return0; }
Other than how you specify what's in them they actually work almost exactly how you'd expect them to. Which is kind of niceSo THAT is how function pointers work! Nice one! Thanks