How to resolve the bugs

Hi, the numbers seems to be jumper up after I call out the loops.
How to make sure that the random numbers run accordingly with the loops?
[code]

#include<iostream>
#include <ctime>
#include <iomanip>
using namespace std;

int main()
{
int i, D, count = 0, randvalue, NUMBERS;

cout << "Enter the number of times to repeat the loop : \n";
cin >> NUMBERS;
if (NUMBERS >= 0 && NUMBERS <= 100)
{
cout << "The total number of generated numbers:" << NUMBERS << endl;

srand(time(NULL));
for (i = 1; i <= NUMBERS; i++)
{
randvalue = rand() % 9999;
cout << randvalue << endl;

for (int i = 0; i <= randvalue; i++)
{
D = randvalue % 10;
if (D >=1 && D <= 3)
{
count = count++;
randvalue = randvalue / 10;
cout << count << endl;
}
else
randvalue = randvalue / 10;
}
}

cout << "Number of times the generated number contains digit 1, digit 2 & digit 3: " << count << endl;
}

else {
cout << "Invalid entry... Please re-enter number between 0 and 100 \n";
}
cin.ignore();
cin.ignore();
return 0;
}
Hi, the numbers seems to be jumper up after I call out the loops.
How to make sure that the random numbers run accordingly with the loops?

Pseudo-random numbers are supposed to look random... :-)
What’s exactly the problem you’d like to solve?

I would write your code in a totally different style, but with just a bit tuning it seems to run quite fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include <ctime>
#include <iomanip>

int main()
{
    srand(time(NULL));
    int i, D, count = 0, randvalue, NUMBERS;

    std::cout << "Enter the number of times to repeat the loop : \n";
    std::cin >> NUMBERS;
    std::cin.ignore(1);
    if (NUMBERS >= 0 && NUMBERS <= 100)
    {
        std::cout << "The total number of generated numbers:" << NUMBERS << '\n';

        for (i = 1; i <= NUMBERS; i++)
        {
            randvalue = rand() % 9999;
            std::cout << "randvalue: " << randvalue << '\n';

            for (int i = 0; i <= randvalue; i++)
            {
                D = randvalue % 10;
                if (D >=1 && D <= 3)
                {
                    count++;
                    randvalue = randvalue / 10;
                    std::cout << "count: " << count << '\n';
                }
                else
                randvalue = randvalue / 10;
            }
        }

        std::cout << "Number of times the generated number contains digit 1, digit 2 & digit 3: " << count << '\n';
    }

    else {
        std::cout << "Invalid entry... Please re-enter number between 0 and 100 \n";
    }
    return 0;
} 


Hi Enoizat,

Thanks for the responds. The question goes like this.

The program prompts the user to enter a number. This number determines the number of times the loop will repeat. The number ranges from 0 to 100. Validate this range. The program uses a random number to generate a number from 0 to 9999 and then checks if the generated number contains the digit 1, digit 2 or digit 3.

However, i realized that some of the random "4 digit numbers" missed count for the digit 1, 2, 3. I thought by default the program should detect all random "4 digit numbers" contains the digit 1, 2, 3 numbers?
Last edited on
Sorry, help me to understand.
Since every number can be divided by 1, what do you mean is you want to consider the digits as characters and count, for example
418 --> 1 between {1, 2, 3}
231 --> 3 between {1, 2, 3}
6 --> 0 between {1, 2, 3} even if it can be divided by 2 and 3.
Did I understood?

And do you want to do this by math?
In that case I’d prefer to use the logarithm to the base 10 to know the number of digits and to divide by 10 to access every digit in sequence.
But I can’t deny, since I’m terrible at math :-( , that my first choice would be to turn the number into a string.
Please, let me know if that’s what you want to achieve and if it must be done by math.
Last edited on
Hi,
I think adding a break statement after line 29 will do the trick...
¿do you want to know if there is a digit in range [1;3] or how many digits are in that range?

1
2
3
4
5
for (int i = 0; i <= randvalue; i++)
{
	//...
	randvalue = randvalue / 10;
}
¿how many times would that loop? (test with extreme cases, like 999 and 1000)
Enoizat: I think the command i used "For while" which actually uses "count ++" as what u mentioned it is done in math. I'm not sure how it ended up missed out some of the digit 1, digit 2, digit 3 during the counting process. You know how to use logarithm to the base 10 to draft out the source code? So far, my lecturer haven't cover till that topic.

Shadder: I tried adding break statement after line 29, it doesn't solve the problem.

ne555: the most extreme cases is 0 till 100 loop which it= tried. It still doesn't solve the problem.

I'm so thankful for everyone contributing their ideas. Good to learn new things in this forum! Keep it going :)
that's not what I asked you.
you wanted to traverse the digits, but your loop condition makes no sense. If you do a dry run setting `randvalue' to 999 and 1000 you may realise your mistake.

also, you still haven't answered if you want to count digits or count numbers
Hints:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <ctime>
#include <iomanip>
#include <iostream>

void waitForEnter();

int main()
{
    // First step: let's get a valid number from the user
    int NUMBER = 0;
    do {
        std::cout << "How many pseudo-random should I generate (1-100)? ";
        std::cin >> NUMBER;
        std::cin.ignore(1);
        if(NUMBER < 1 || 100 < NUMBER) {
            std::cout << "Invalid entry... Please re-enter number between 1 "
                         "and 100 \n";
        }
    } while(NUMBER < 1 || 100 < NUMBER);

    // Second step: loop as many time the user asked
    // Count the matches
    int count = 0;
    srand(time(NULL)); // this should happen just one time in the program
    for (int i = 0; i < NUMBER; i++)
    {
        int randvalue = rand() % 9999;
        std::cout << "Random number " << randvalue;
        int temp = 0;
        do {
            int D = randvalue % 10;
            if(D >=1 && D <= 3)
            {
                count++;
                temp++;
            }
            randvalue /= 10;
        } while(0 < randvalue);
        if(temp > 0) {
            std::cout << " contains " << temp << " digits between 1, 2 or 3.\n";
        } else {
            std::cout << " doesn't contain any 1 nor 2 nor 3.\n";
        }
    }
    std::cout << "In all, I've counted " << count << " amongst 1 or 2 or 3.\n";

    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Topic archived. No new replies allowed.