Jump to content

Algorithm to convert 24 hour format into 12 hour

- - - - -

  • Please log in to reply
12 replies to this topic

#1
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Basically it has been a while since I posted....
Just need to seek for some assistance as this is a task for C programming in linux - any hint, few sentences or cheat sheet would be most welcome to assist in this small task I am having difficulties with.....here is the scope of it in a quote box.

Quote

Design and implement an algorithm that will prompt for and receive the time expressed in 2400 format, (e.g 2305 hours), convert it to 12-hour format (e.g 11:05p.m) and display the new time to the screen. Your program is to repeat the processing until a sentinel time of 9999 is entered.

I attempted this in code, at some point - it seems that I am stuck


#include <stdio.h>


int main()

{

     int time;

    

     //read numbers

     printf("Enter the time in 2400 format>\n");

     scanf("%d\n",  &time);


     //converting

     if (time > 1200 )

     printf("The time is %d\n", time);

     else

     printf("The time is %d\n", time);


     //exit program

    return(0);

}



Few things I am attempting is to change 2400 to 1200 hours, which is I am not sure and the other is the ':' printed out in the output. Does anyone know how?

(pssttt.... what about the sentinel execution? Does this involve with a while loop or something relevant?)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
time / 100 appears to be your hours. If that is greater than 11, you are in PM. If it's greater than 12, you need to subtract 12 as well. If it's 0, you need to add 12 and it's still AM.

time%100 would be the minutes.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
This will convert a 2400 format integer to 1200 format:


int convert_format(int time)

{

return time >= 1300 ? time - 1200 : (time < 100 ? time + 1200 : time);

}


As for separating the hours and the minutes, WingedPanther is right. Perform an integer division by 100 to get the hours, and the modulus to get the minutes. Then, simply print them out separately with a colon between them:


printf("The time is %d:%d\n", hours, minutes);



#4
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts

WingedPanther said:

time / 100 appears to be your hours. If that is greater than 11, you are in PM. If it's greater than 12, you need to subtract 12 as well. If it's 0, you need to add 12 and it's still AM.

time%100 would be the minutes.

Can you well, say this in code which I understand better that way?

#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

jclarke said:

Can you well, say this in code which I understand better that way?

The code I provided in my previous post does exactly what WingedPanther said. (Adding and subtracting 12 depending on the range of the input.) If you need to determine AM/PM, that's just a simple if statement that you should be able to do quickly.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Basically, I'm just saying create three new variables: hours, minutes, and ampm. The first two are integers, the third is a two-character string. Assign values as indicated above.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
WingedPanther's suggestion in code:
int hours = time / 100;

int minutes = time % 100;

char *ampm = "am";


// The special case of 24:00 as input should be considered, as for most people it's

// valid input specifying 0:00 the next day. IF 24:00 is used with a minute value,

// then the input IS invalid.

if (minutes > 59 || hours > 24 || hours == 24 && minutes != 0)

{

    performFailureOp(); // You define it, this is in the case of bad input.

}


// Now perform all the modifications needed.

if (hours > 11 && hours != 24) ampm = "pm";

if (hours == 0) hours = 12;

if (hours == 24) hours = 0;

Wow I changed my sig!

#8
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
Um, do you really need help with this? Just subtract 1200 if time >= 1300. Perhaps I'm misunderstanding your question, but it seems fairly obvious.
Programming is a journey, not a destination.

#9
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts

DarkLordofthePenguins said:

Um, do you really need help with this? Just subtract 1200 if time >= 1300. Perhaps I'm misunderstanding your question, but it seems fairly obvious.

Im sorry if you find this fairly obvius however, I am deaf and my english isn't my first language apparently.

#10
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Just a little help - i am working on this one
Am I getting close?

include <stdio.h>


int covert_format(int a)


int main ()

{

	int time;

	int hours;

	int minutes;

	char *ampm = "am";

	

	

	//read numbers

	printf("Enter the time in 2400 format>\n");

	scanf("%d\n", &time);



	time(convert_format)

		


	//exit program

	return(0);

}


int covert_format(int time)

{

	return time >= 1300 ? time - 1200 : (time < 100 ? time + 1200 : time);

printf("The time is %d:%d'n", hours, minutes)

}



#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Once you return something from a function, it ends - meaning your printf will never execute. Also, you're missing # in front of include, missing a semicolon at end of function prototype and you're calling your function wrong:

convert_format(time); //you're missing semicolon here as well


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#12
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Hmmm - not sure where put my finger at


#include <stdio.h>


int covert_time(int a);


int main ()

{

	int time;

	//int hours;

	//int minutes;

	char *ampm = "am";

	

	

	//read numbers

	printf("Enter the hours in 2400 format>\n");

	scanf("%d\n", &hours);

	printf("Enter the minutes of the time>\n");

	scanf("%d\n", &minutes);



	convert_time(time);

		


	//exit program

	return(0);

}


int covert_time(int time)

{

	return time >= 1300 ? time - 1200 : (time < 100 ? time + 1200 : time);

printf("The time is %d:%d'n", hours, minutes);

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users