This question already has answers here:
How to make a function execute at the desired periods using c++ 11
(2 answers)
How to create timer events using C++ 11?
(6 answers)
Closed 3 years ago.
I've got the similar problem as python - loop at exact time intervals - the only difference is that I want to use C++.
So: how is it possible in C++ to make a loop in which every iteration would take a set amount of time, independently of the time of executing code in a loop?
// Example:
for (int i = 0; i < 100; i++)
{
cout << "foo" << endl; // prints foo every, let's say, 1s
bar(); // may take, let's say 1ms or 325ms (of course less than 1s)
}
Related
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?
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Integer division always zero [duplicate]
(1 answer)
Random number c++ in some range [duplicate]
(6 answers)
Closed 3 years ago.
I need to produces numbers between 0 and a max (seen in code as assetMax). In the code, the rand()/RAND_MAX always produces 0 and I cannot seem to figure out why. I use the rand() function immediately before it to produce values in a range and it works completely fine. However, here it does not.
I have tried to switch the order of the variables, create the random number in a separate double before multiplying the two, and the header.
void cPortfolio::randomize(cProblem &portfolioProblem) {
int assetCount = 6 * rand() / RAND_MAX + (portfolioProblem.assetMax-8); //this line works as expected
int test;
for (int i = 0; i < assetCount; i++) {
double num = rand() / RAND_MAX; //this always produces 0.0000
int test = num * (portfolioProblem.assetNum); } `} //cannot format these correctly please ignore the brackets
This question already has answers here:
Generate random numbers uniformly over an entire range
(20 answers)
rand() returns same values when called within a single function
(5 answers)
Closed 4 years ago.
We will modify part of the existing menu logic to generate an ID for the user. This user ID will be of the type int, and will be 5 digits in length. The values are 00001 – 99999, and should appear with all 5 digits (leading zeroes) on the display upon generating them. Do not change the data type from that of an int. Check out the setfill manipulator as a possible helper for this. Setfill allows you to set a fill character to a value using an input parameter, such as ‘0’ for leading zeroes. Researching it, you’ll see that you will need to reset the fill character after use too, or you will be printing a lot of zeroes on the display! Your program also should guarantee an absolutely unique user ID has been generated for each new activity that is added. Think about how this works...
Currently I've been trying to get the following code to work
srand(time(0));
cout << setfill('0') << setw(5) << rand() %99999 << endl;
Problem is that this doesn't seem random at all (it's just slowly counting up based on the computers internal clock right?) and the first digit is always zero. Like the instructions say it should be between 00001 and 99999.
EDIT: I appreciate the different solutions, but most of them are more advanced than what I'm supposed to be using for this assignment. I'm fairly sure srand() and rand() is what I should be using.
Okay, so it seems you must use the rand() function to generate a value between 1-99999. So with that in mind, the following code should generate random values in the required range:
#include <iomanip>
#include <iostream>
const int randID()
{
return 1 + (std::rand() % (99999));
}
int main()
{
for(int i = 0; i < 1000; ++i)
std::cout << std::setfill('0') << std::setw(5) << randID() << '\n';
return 0;
}
For me it prints, for example:
01886
21975
01072
11334
22868
26154
14296
32169
20826
09677
15630
28651
Which should satisfy your requirement of 0-padded values between 1-99999. Also, as mentioned in the comments. Do look into the <random> for your random number needs outside this assignment as it generates far superior random numbers, and offers way more generators, distributions and better seeding.
This question already has answers here:
Making a square() function without x*x in C++
(7 answers)
Closed 4 years ago.
I'm a beginner in programming and trying to learn C++ by the book Programming principles and practice using C++. In some parts of the book there are little exercises that you can try to do, one of this exercises is about calculating the square of a number, here is what my book says :
Implement square() without using the multiply operator, that is, do the x * x by repetead addition (start a variable result to 0 and add x to it x times).
I've already found a solution for this program but my first tentative was something like this :
#include <iostream>
int main()
{
int a = 0;
std::cout << "Enter an integer value : ";
std::cin >> a;
while (a < a * a)
{
a += a;
std::cout << a << "\n";
}
}
I know this code is wrong but I can't understand the output of the progam, if I enter 5 the program prints 10 20 30 40 50 until 8000, why the for loop doesn't stop when a is greater than its square ? I'm just curious to undersant why
Using multiplication when trying to avoid multiplication seems broken. What about this:
int r = 0;
for (int n = 0; n < a; ++n) {
r += a;
}
why the for loop doesn't stop when a is greater than its square ?
Because it never is. If you compare the graph of y=x^2 against the graph of y=x, you will see that the only time y=x is above, is when 0 < x < 1. That's never the case for integers1. Now, since we're talking about computers with limited storage here, there is a thing called overflow, which will cause a very large number to become a very small number. However, signed integer overflow is undefined behavior in C++. So once your loop gets to the point where overflow would happen, you cannot rely on the results.
1. Note that your loop is not set to stop just when a is greater than its square, but when it is greater than or equal to its square. So, your loop will actually stop if a is 0 or 1.
This question already has answers here:
Finding the length of an integer in C
(29 answers)
Closed 9 years ago.
I know I can do:
n = floor(log10(i)) + 1;
Or I can do a quick loop:
while(i) {
n++;
i/=10;
}
Is there any better way than a complicated math operation, or a loop to achieve the goal? For example: if i = 1234, then n = 4.
The shortest way I know of (not computationally, just in terms of typing) is to call snprintf(3):
int n = snprintf(NULL, 0, "%d", i);
convert it to a string (itoa) and count the number of characters? (might be not the best performance-wise though)