Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 29th, 2005, 7:01 PM   #1
deathseeker25
Programmer
 
Join Date: May 2005
Posts: 48
Rep Power: 0 deathseeker25 is on a distinguished road
Some exercises

Hello I'm a n00b ate programming and I think I need some help solving one exercise that I saw in a Google search.It says exactly:

Quote:
1. To write a program that reads 3 corresponding whole number to one hour in hours, minutes and seconds, and converts into seconds, and prints the result. Assuming the following 0 variable for the hours, minutes and seconds, the reading could be:

int horas, minutos,segundos;
long int result_em_segundos;
.....
scanf("%d%d%d",&horas, &min, & seg);
Note: this was translated by Google....

Well this problem was solved almost successfully:

/*Horas. minutos e segundos*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int horas,min,seg;
    long int result_em_segundos;
    
    printf("\nDiga que horas, quantos minutos e quantos segundos são respectivamente: ");
    scanf("%d%d%d",&horas,&min,&seg);
    
    horas=60*60*horas;
    min=60*min;
    
    printf("O resultado em segundos e de %d ",horas+min+seg);
    
    
    system("PAUSE");

Some things are in portuguese btu I think the code is quite understandable...

Well, my big problem is the second exercise:

Quote:
2. write a program that makes the inverse operation in the previous exercice.
And I wrote this code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int horas,sec,min;
    
           
    printf(" Say how many seconds you intend to convert for hourly time....");
    scanf("%d",&sec);
    
    horas=sec/60/60;
    min=sec/60;
    
    printf("Sao %d horas %d minutos e %d segundos",horas,min,sec);
    
    system("PAUSE");
    
}
The problem is that after compiling and executing it appears the following one in relation the last printf, introducing as given 3600 seconds:

Quote:
It's 1 hour, 60 minutes and 3600 seconds
Well, this is quite bad, isn't it....help me please....
deathseeker25 is offline   Reply With Quote
Old Jun 29th, 2005, 7:47 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
When you use part (or all) of the seconds to make the appropriate number of hours, you have to only use the remainder for the minutes, and the remainder of that operation for the seconds. You may find the modulus operator (%) useful for that.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 30th, 2005, 4:10 AM   #3
deathseeker25
Programmer
 
Join Date: May 2005
Posts: 48
Rep Power: 0 deathseeker25 is on a distinguished road
Quote:
Originally Posted by DaWei
When you use part (or all) of the seconds to make the appropriate number of hours, you have to only use the remainder for the minutes, and the remainder of that operation for the seconds. You may find the modulus operator (%) useful for that.
Thanks pal I used this code:

horas = sec/3600;
min = (sec%3600)/60;
sec = sec%60;

And it functioned prety well....

Thanks for helping
deathseeker25 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC