Changing an int value into time using casting and setfill [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
For a programming assignment, I need to display the time a hypothetical cafe is open. On the weekends the cafe opens an hour and a half later than during the week, but closes at the same time.
The user inputs the opening time, closing time, and how much later it opens on the weekend. The program is supposed to calculate and display the schedule for the cafe. So far my program can do all of this, but I just don't know how to convert a regular integer, such as 845, into the time format, such as 8:45.
My teacher suggesting using casting and setfill. I have tried researching both but I'm just not sure where it applies in the program.
So, how can I convert an integer (e.g. 845) into the time format (e.g. 8:45)?

I guess we can assume that the integer number is always between 0 and 2400, so we're interested in four of it's digits, at most.
One way is to access parts of the integer by division and modulo. Let's take 1245 (12:45) as an example:
1245 / 100 = 12 (always rounded down)
1245 % 100 = 45 (<= modulo operator)
So by using division and modulo, we can extract the first two numbers (= hours) and second two numbers (= minutes) separately.
The rest is easy:
int time = 1245; //your time
int hours = time / 100;
int minutes = time % 100;
//print it
std::cout << hours << ":" << minutes << std::endl;
Result: 12:45

Related

Uniswap V3 sqrtPrice for ETH/DAI and ETH/USDT, ETH/USDC vastly different [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 9 months ago.
Improve this question
I use IUniswapV3PoolState.slot0 to return sqrtPriceX96 and tick for different pairs in Uniswap V3. The results are very reasonable for ETH/DAI, but it's quite different for ETH/USDT and ETH/USDC.
This is not because of the order of the tokens, but rather the final result of price (after taking care of square root and Q96) differ by 10^(-12). So I would get ETH/DAI around $3200, while ETH/USDT and ETH/USDC will give $3200*10^(-12). Is there anything I'm missing? Thank you!
EVM compatible blockchains use fixed-point math. The floating point values that you see in the UI are abstractions, technically everything is an integer; a specific number of digits reserved to represent the decimals. Different ERC-20 tokens reserve a different number of decimals.
WETH and DAI have 18 decimals
USDT and USDC have 6 decimals.
If you have asset X that has 6 and an asset Y that has 18 decimals, then the price of Y in terms of X has to be corrected for this fact.
Lets use price = y/x, then the price adjusted for the amount of decimals is going to be price_adjusted = y/x * 10^(-12). To see why refer to Section 3.3.2 here:

Equivalence Partition [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 4 years ago.
Improve this question
If the temperature falls below 18 degrees, the heating is switched on.when the temperature reaches 21 degrees, the heating is switched off.what are the boundries solution for this problem?
How to solve this problem?
Equivalence partitioning is a testing technique. The idea is to divide the input data into equivalent ("similar") classes and have a test case covering each partition at least one time. This technique is used to reduce the amount of test cases.
In this given example, there are 2 main points where the state changes. So the line segments in between will represent the equivalent classes.
Class 1: less than 18 (the heating should be on)
Class 2: from 18 to 21
Class 3: greater than 21 (the heating should be off)
So the minimum set of input test values to cover all valid equivalence partitions would be one value from each of those classes. Let's say: 16, 20, 25
Any one from min limit-18-20-21-Anyone from Max limit...
The minimum is 3 values:2 outside the boundaries a<18, b>21 and one inside the range
http://istqbexamcertification.com/what-is-equivalence-partitioning-in-software-testing/
If the temperature falls below 18 degrees, the heating is switched on.when the temperature reaches 21 degrees, the heating is switched off.what is the minimum set of test input values to cover all valid equivalence partitions?
Ans:-
Your temperature is 12,15,17 is heating.
your Temperature is 21,23,26 is no heating.
answer is 16,21,24.

C++: Decimal to Roman Numeral conversion [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I know how to do this and have made a program that does so in the past, however it never incorporated the special cases of roman numerals (e.g. IV = 4). Integrating that into a functional program in a minimalistic way is my main problem. Is there a good way to do this?
A very simple solution, in pseudo-code, could look something like this:
value = get_the_value_to_convert();
divider = 1000; // Start at 1000 (M)
while (value > 0)
{
count = value / divider;
value = value % divider;
for (i = 0; i < count; ++i)
print(roman_numeral_from_number(divider));
divider /= 10;
}
The roman_numeral_from_numbe function does a one-to-one mapping between a number and a roman numeral.
For e.g. 5432 as input it would print MMMMMCCCCXXXII.
For better results, divide the divider variable with 2 and 5 every second time.
For even better results, add check for certain numbers, like 90 being equal to "XC".
Note that even with the modifications, the above algorithm can't handle very large numbers, but on the other hand Wikipedia states that historically it was only used for small numbers anyway.

Calculation for a variation of hours [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am a beginner to programming and I am learning to program in C++, I am having some trouble trying to work out what would be the best way to attack this solution for a console game I am making, so basically I have 3 games that go for 5 hours each, and any game after that will go for 4 hours, once I get the amount of the games the user wants to play, basically I want to work out the total amount of hours the games will take to play.
How do i go about calculating the first 3 games multiply 5 hours and anything after that by 4 hours if there are more than 3 games being played?
Any help would be appreciated thanks.
unsigned int n_hours(unsigned int n_games) {
if (n_games < 4) {
return n_games*5;
} else {
return (n_games-3)*4 + 15;
//return n_games*4+3; //less straightforward to understand
}
}

Generate prime factors of a number [closed]

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'm trying to write a function that given an Int greater than one gives a non decreasing list made of the prime factors (with repetition) of that number
Example: n = 12, the output should be [2,2,3]
I don't know where to start.
There are of course well know algorithms for what you want to do, so simple google search would really solve that.
However, I'd like to show you a simple thinking process that might be helpful in the future.
Since the factors have to appear in the ascending order, you might:
Start with the lowest prime (2).
Check if the number can be divided by it. If it can, do it and go back to 1.
If not, replace 2 with a next prime and go back to 2.
Now, it's obvious that the biggest prime you will ever check is the number you've started with. However, the basic multiplication axiom states that if a number can be divided by a:
n / a = b
Then it can also be divided by b! You can use that fact to further narrow the checking range, but I'll leave it to you to figure (or google) the upper bound.
Actual implementation is of course a part of your homework and thus supplying code wouldn't be a wise idea here. However, I don't think that stuff such as next_prime will be hard for you.