C/C++ Thread

S.T.A.R.S.

banned
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?
 

Troncoso

VIP Member
It looks like your code doesn't let the actual angle be greater than 180, which would mean you could never achieve an obtuse angle such as 270.
That's what this code suggests anyway:

Code:
if (angle > 180)
    {
        angle  = angle -360;
    }

The question doesn't specify if the angle can be in either direction (one direction would normally be +/- or the angle would be < or > 180).
 
Last edited:

Darren

Moderator
Staff member
Yeah I noticed that too ^^. I removed that bit of code and all it did was make my first value return as like -35ish. I think you're right about that. But that doesn't change my problem with the two examples being wrong.
 

Troncoso

VIP Member
Well, my last point makes a big difference on how you'd do this. For instance, the second example:

hour: 5
minute: 5
desired angle: 30
output in minutes : 16.3

On the clock face, the minute hand is behind the hour hand by 122.5 degrees. So, for the purpose of that question, is this -122.5 or is it 237.5 degrees?

Edit: Regardless of all that, I think your error is due to not accounting for the fact that as the minute hand moves forward, so does the hour hand. You account for that in your initial angle calculation, but you leave that out when try to find the angle to move to get to the desired result.
 
Last edited:

Troncoso

VIP Member
This got me pretty close:

Code:
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;

The positions seem relatively right for the first example, but I get like 10.2. For the second example I get 16.25, so I'm assuming the example was rounded.
 

0x47

New Member
Hello, How can I incorporate functions written in other languages when I am writing a program ?
example: I am writing using C and I want to write a function that will work fast so I wrote it in assembler, and now I need to somehow to load it in to my project.
Is there some file format I can compile it to that is recognised by all languages ?
 

0x47

New Member
didn't know that , but I meant something else.
bad example probably, If I want to use function I wrote in C while I am programming In C#?
 

0x47

New Member
I see, I thought its a different process.
I don't have any specific issue , just being curious.
thanks for your help.
 

SpriteMidr

Active Member
Does anyone know if there is a better way to implement a linked list than this in C?

Code:
#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");
}
 

Cromewell

Administrator
Staff member
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 :p)
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;
}
 

SpriteMidr

Active Member
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 :p)
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;
}

So THAT is how function pointers work! Nice one! Thanks :)
 
Top