You are here:

C/i need a help in c++

Advertisement


Question
QUESTION: 1)Write a program to express a given number in terms of hours ,minutes, and seconds and output the result.
2)Repeat The q1 by reading the number of days from a file and write the output to another file.

ANSWER: Hello Sheikha

I am not sure if you need help with the programming or the algorithm. The first step is to develop the algorithm. I'll help you with problem 1. Maybe you can do problem 2 from my example.

You must break the problem down into smaller steps. What are the steps for problem 1 ? Here is my list.

1) Input number as an integer

2) Calculate hours = number / 3600
We can assume that the number is in seconds. The number of seconds in an hour is 3600. So the number of hours represented by the number is number / 3600. Here we are doing integer division, so the remainder is thrown away. For example, if the number is 3700, then number / 3600 is 1.

3) number = number - hours * 3600
Since we already have the hours, we take the number of seconds represented by the hours, and we subtract that from the original number.

4) minutes = number / 60
We calculate the minutes with the same method that we calculate hours.

5) number = number - minutes * 60
Since we already have minutes, we subtract the number of seconds represented by those minutes from the number.

6) seconds = number
What is left is the seconds.

7) print out the results.

Show me an attempt at writing the code, and I'll help you more.

Best regards
Zlatko


---------- FOLLOW-UP ----------

QUESTION: Thanks alot for your explanation,
I did it like the below but it give wrong result, can you help me in programming?i need it necessary,

#include<iostream.h>
void main()
{
int n;
cout<<"enter number of days:";
cin>>n;
hr=n*24;
min=hr*60;
sec=min*60*60;
cout<<"n"<<"hr"<<"min"<<"sec";
}

ANSWER: I see now that I did not understand your original question.



The problem is with calculating seconds,
sec = min * 60; // This is correct
not
sec = min * 60 * 60; // This is wrong

I don't know how you got your program to run, because it does not even compile for me. Anyway, here is the corrected program.

int main(void) // The c++ standard requires that main return an int
{
   int n;
   cout<<"enter number of days:";
   cin>>n;

   int hr=n*24;
   int min=hr*60;
   int sec=min*60;
   cout<<"n=" << n << " hr=" << hr << " min=" << min << " sec=" << sec << endl;

   return 0;
}

---------- FOLLOW-UP ----------

QUESTION: thanks alot,
how can i answer the second part of the question?
2)Repeat The q1 by reading the number of days from a file and write the output to another file.

Answer
You use the ifstream for input, and ofstream for output. Here is the program.

#include <iostream.h>
#include <fstream.h>
using namespace std; // you probably don't need this line

int main(void)
{
   ifstream fin("infile.txt");
   if (fin)
   {
       int n;
       //cout<<"enter number of days:";
       fin>>n;

       int hr=n*24;
       int min=hr*60;
       int sec=min*60;

       ofstream fout("outfile.txt");
       fout<<"n=" << n << " hr=" << hr << " min=" << min << " sec=" << sec << endl;
   }
   else
   {
       cout << "File is not found\n";
   }

}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.