简体   繁体   中英

printf not working (tried other solutions, still doesn't work)

Ok. So basically, I'm trying to make an app to check whether or not there is a number containing all digits (0-9) that is divisible by all digits. I'm using the rand() function to get combinations of numbers, in case that might matter. This is my code (still incomplete, I just wanted to see if my code runs properly):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int randLessThanTen(int randNum);

int main()
{
    srand(time(0));
    int taken[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int num = 0;
    for(int i = 0; i < 9; i++)
    {
        int randNum = rand();
        randLessThanTen(randNum);
        while(taken[randNum] == 1)
        {
            randNum = rand();
            randLessThanTen(randNum);
        }
        num+=randNum*(pow(10, i));
        taken[randNum] = 1;
    }
    //wtf
    printf("the number is %d/n", num);
    return 0;
}
int randLessThanTen(int randNum)
{
    while(randNum>9)
    {
        randNum = rand();
    }
    return randNum;
}

And as mentioned above, the printf() statement doesn't work. It's not that the number isn't printed, but rather that nothing is printed at all, not even the string. I'm running Code::Blocks 20.03, maybe somebody here can help me?

taken is initialized with a size of 10.
rand() can produce numbers way larger than that.
Since the return value of randLessThanTen() is never used you are trying to access taken[] way beyond its scope which causes your program to crash before it even reaches printf() .

Also, your approach by generating random numbers until one of them is in your desired range is very time-consuming.
I would also tend to modulo 10 , like user3386109 suggested:

#include <stdio.h>
#include <time.h>
#include <math.h>

int main()
{
    srand(time(0));
    int taken[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int num = 0;
    for(int i = 0; i < 9; i++)
    {
        int randNum = rand() % 10;
        while(taken[randNum] == 1)
        {
            randNum = rand() % 10;
        }
        num+=randNum*(pow(10, i));
        taken[randNum] = 1;
    }
    printf("the number is %d\n", num);
    return 0;
}

You call the function RandlessThanTen in for loop but you are not storing its returning value in a variable. So Try to replace in for loop:

        RandNum = randlessThanTen(RandNum);
      

Now your RandNum will get affected by the function you call.

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include <time.h>

int randLessThanTen(int randNum);

int main() {
    srand(time(0));

    int taken[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    int num = 0;

    for (int i = 0; i < 9; i++) {   
        int randNum = rand();
        randNum = randLessThanTen(randNum);

        while(taken[randNum] == 1) {
            randNum = rand();
            randNum = randLessThanTen(randNum);
        }

        num += (int) randNum * (pow (10.0,(double) i));
        taken[randNum] = 1;
    }   
    //wtf
    printf("the number is %d\n", num);
    return 0;
}

int randLessThanTen(int randNum) {
   return (randNum) % 10; 
}

You need to take the return from the "randLessThanTen" function and use "#include<math.h>" for the pow() function. If you understand pointer, try to use pionter in the function.

void randLessThanTen(int *randNum) {
    *randNum = *randNum % 10;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM