Program to convert seconds to format of DD/MM/SS [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know you're supposed to post code of what you have started when asking questions, but honestly I am completely lost. I am reading a book on C++ to learn (I'm self taught), the book is Sams C++ Primer Plus. I have recently just finished the chapter on type conversions and type casts. Well, I was interested in trying to make the program that converts seconds to days/minutes/seconds. It's a very simple application. I could code the application perfectly, but for some reason the math stumps me. I need help with this, or at least starting the variables and such. Thanks.
Also, I'm sure I am supposed to use the modulo (in C++ modulus) somewhere within the program.
The output of the program is supposed to be similar to this:
"Enter the number of seconds: SECONDS"
"SECONDS seconds = 364 days, 46 minutes, 40 seconds.
The actual values in the output don't matter as long as the conversion is correct.

const int SECSPERDAY=84600;
const int SECSPERHOUR=3600;
const int SECSPERMIN=60;
int days=SECONDS/SECSPERDAY;
int hours=(SECONDS-(days*SECSPERDAY))/SECSPERHOUR;
int mins=(SECONDS-(days*SECSPERDAY)-(hours*SECSPERHOUR))/SECSPERMIN;
int secs=SECONDS%SECSPERMIN;

As your task is connected to type conversions and -casts I would suggest the learning outcome is that dividing with integers drops the remainder and you can get the remainder by modulu operation.
therefor you can use (given SECONDS is the input)
int sec = SECONDS % 60;
int min = (SECONDS / 60) % 60;
int hours = (SECONDS / (60 * 60)) % 24;
int days = (SECONDS / (60 * 60 * 24) % 365;
int years = (SECONDS / (60 * 60 * 24 * 365);
I did not use constants to show the direct depedencies even if this is considered bad practice.

Related

Is there a way for C++ to detect that when a value ends with a certain number it'll do an if-statement? [duplicate]

This question already has answers here:
How does the modulus operator work?
(5 answers)
Closed 2 years ago.
it's my first time posting in here! I know that my question doesn't sound really correct, but I'm really new to programming. In my program, I need that if a user enters a number (1 <= 365) then the program will tell which day is it. Then I came up with a solution but I don't know if C++ can even do it. For example, if A/7= 3.6 and B/7= 2.1 or C/7=2.9 then it'll read the last decimal and do a different if function every time (for example if it ends with .6 program then says it's Thursday and .9 it's Sunday and so on and so on). I hope it kind of makes sense.
Thanks
#include <cstdio>
main()
{
float n, D;
printf("Enter the number of the day n:\n");
scanf("%f", &n);
D = n/7
if (D = )/*here I'm trying that if D ends with some number it'll do this*/
{
printf("It's Monday")
}
}
How about multiply 10 then '%' to get the remainder?

Calculation of value of Pi up to 'n' decimal places [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I was trying to make a C++ program to calculate value of Pi up to 'n' decimal places using the Leibniz formula.
The number 'n' would be entered by the user.
I was successful in creating the Leibniz formula but I am having trouble with the latter part i.e value precise up to 'n' places.
The trouble :-
As more and more terms will continue to add to it, it's digits will keep on changing so how to tell if a particular digit has stopped changing despite the addition of more terms.
The code written so far :-
#include<iostream>
using namespace std;
int main()
{
float s=0;
int w=-1;
for(float i=1;;i=i+2)
{
w=w*(-1);
s=s+w*(1/i);
cout<<s<<endl;
}
return 0;
}
It would be great if things would be kept simple since I am just a beginner at C++.
Thank you very much :)
Since you want to compute Pi up to arbitrary nth digit, you want a library for working with big float numbers; see
C++ library for big float numbers
the real trouble is that Leibniz formula is not useful in the context. The actual precision achieved can be estimated as the last term in the formula
Pi / 4 = 1/1 - 1/3 + 1/5 - 1/7 + ... + (-1)**(n + 1) * 1 / (2 * n - 1) + ...
If, for intance, you want Pi up to 100th digit it means that the residual (and the last term) should be less than 1e-100:
1 / (2 * n - 1) < 1e-100
2 * n - 1 > 1e100
n > 5e99
And as you can see 5e99 loops is by far too much for the modern (super-)computers

What is this boolean operation? `expression * boolean` [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I saw a problem where we had to find the wage. If the hours are less than 40 we pay regular wage ($100/hr) if there is overtime we give 1.5 times the original pay ($150/hr).
The challenge was to solve it without if-else/loops/or ternary operations.
It was solved like this
int hours = /*some_number*/;
int wage = (100*hours) + (50*(hours-40))*(hours>40);
This code works.
(hours>40) returns 1 if hours is greater than 40 and returns 0 if it is less.
I understand that it is some kind of boolean operation, but how does it work and what is this called exactly.
The right way to do it is straight-forward:
int hours = /*some_number*/;
int wage = 100*hours;
if (hours > 40) wage += 50 * (hours-40);
To squeeze it to a single expression, the example takes advantage of the fact that a boolean is either 1 or 0. So x*some_bool evaluates to either x or 0.
In your case, if (hours > 40) then
(50*(hours-40))*(hours>40) == (50*(hours-40)) * 1 == 50*(hours-40)
otherwise it is 0.
(50*(hours-40))*(hours>40) == (50*(hours-40)) * 0 == 0
In general it is less readable to write code this way. The only valid uses IMO are in advanced algebraic transformations used in cryptography or complexity theory.
Well you basically answered your question. The (hours > 40) returns 1 if hours is greater than 40 and returns 0 otherwise.
Maybe more tricky part is the fact that this boolean result is then converted to int implicitly before being multiplied by overtime payment, but that is out of the scope of the question.
So the whole code could be indeed expanded in this way
int hours = /* something */
int wage = 0;
int overtime = hours - 40;
if (hours > 40) {
wage = 100*hours + 50*overtime;
}
else {
wage = 100*hours
}

C++, Getting, adding, and dividing percentiles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm very new to c++. I need help on how to:
get percentiles (which I think I'm doing right..?, and
how to add them to another number.
Here is part of my code, I'm not even sure if I'm including the right headers. Thanks.
#include <iostream>
#include <iomanip>
int main() {
double total, tipTotal, taxTotal, cost, tax, tip;
cost = 44, 50;
tax = 100 - 93.25;
tip = 100 - 85;
total = cost + tax + tip;
tipTotal = cost + tax + tip;
taxTotal = cost + tax;
// other code
return 0;
}
First, I think you have a typo.
I think you meant to say 44.50 (with a decimal point).
cost = 44.50;
Because the comma is a valid operator, the compiler does not catch this as an error.
Second, C++ does not handle percentages, so you have to specify percentages as a decimal value.
So 15% is not 15.0 but 0.15.
So your code should say
tax = 1.00 - 0.9325;
tip = 1.00 - 0.85;

How to improve my code about the growing of a bacterial colony (C++) [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm coding a program to calculate the growth of a bacterial colony until certain point.
Given a "X", that will represent the initial number of bacteria. And given a "Y", that will represent the number limit desired of bacteria in the bacterial colony. Return the number of days and hours that the bacterial colony needs for reaching the limit.
The bacterial colony doubles each hour.
Example.1:
Input: 1, 8
Output: 0, 3
Example.2:
Input: 1000 , 1024000
Output:0, 10
Example.3:
Input: 123, 3453546624536
Output: 1, 10
If the hour calculated returns a fractional number, it must be rounded down.
So far I have written this code:
#include <iostream>
using namespace std;
int main(){
long int binitial, blimit, day, counter=0;
float hour;
cin >> binitial;
cin >> blimit;
while(binitial <= blimit){
binitial = binitial * 2;
counter++;
}
day = counter / 24;
cout << day << " ";
hour = (counter % 24) - 0.5;
cout << (int)hour;
return 0;
}
You can remove the loop by observing that the number of hours is Log2(Y/X). To calculate Log2(A) using the standard functions, calculate log(A)/log(2).
You may need to address precision issues when going from doubles to ints, because the calculations will be approximate. The final expression for the hours may look like this:
int hours = (log(Y/X) / log(2)) + 1E-8; // Add a small delta
Going from hours to days/hours is very simple, too:
cout << hours/24 << " " << hours % 24 << endl;
You can use a long int for hour if you do the following:
hour = counter - (day*24); // The total number of hours minus the number of hours that are in each day.
I don't have a compiler in front of me but you can probably also do something like this:
hour = counter % 24; // this will return the remainder when counter is divided by 24.
If blimit is always a multiple of binitial, the solution is simple:
counter%24 will be always an integer, so you don't have to round it.
In case of day days and hour hours, you only have to do is:
hour = counter%24
A note on the method of calculation: you don't need to iterate if you're only doubling each time. You're just looking for a value of n such that 2n gives the right result.
So, note that ngenerations = log2 blimit - log2 binitial
Once you have the number of generations (as a floating-point number) you can just truncate that to an integer number of hours.