0

this is my function: i need that every number that get random will be: one bigger than 50, one even, and one not even. i complied just with gcc and i'm using c99. It compiled well, but it when it print three random numbers it's print 0,0,and real random number. I want it to print for me three real numbers. thanks for who trying to help!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define HIGH_NUMBER 100


int isValidNumbers(int num1,int num2, int num3);


int main(void)
{
    srand (time(NULL));
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    num1,num2,num3 =isValidNumbers(num1,num2,num3);
    printf("%d %d %d\n",num1,num2,num3);    
    system("PAUSE");
    return 0;
}

int isValidNumbers(int num1,int num2, int num3)
{
    int i=1,ans = 0;
    do 
    {
        srand (time(NULL));
        num1 = rand()%HIGH_NUMBER;
        num2 = rand()%HIGH_NUMBER;
        num3 = rand()%HIGH_NUMBER;
        if ((num1%2==0||num2%2||num3%2==0)&&(num1%2==1||num2%2==1||num3%2==1)&&(num1>50||num2>50||num3>50))
        {
            return num1,num2,num3;
            i--;
            printf("%d %d %d",num1,num2,num3);
        }
    }
    while (i);

}
HelloWorld
  • 33
  • 2

3 Answers3

2

Your function does not set the calling variables as you hoped for. You can't return more than one function value - and it makes no difference that you gave the same names to the variables in main and in the function - they are different variables, and as you wrote it, main just passes copies of those variable values.

Instead I pass pointers to those variables in this example. I also removed the return value of the function, since it now always returns valid values as your spec.

I removed srand within the function. It should be called once only, especially as I call the function 3 times in this example to show different results. If left in the function, all 3 calls would probably give the same result (unless a one-second timer boundary is bridged).

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

#define HIGH_NUMBER 100

void ValidNumbers(int *num1, int *num2, int *num3);     // pointer arguments

int main(void)
{
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    int tries;
    srand ((unsigned)time(NULL));           // call once
    for(tries=0; tries<3; tries++) {
        ValidNumbers(&num1, &num2, &num3);
        printf("%-2d %-2d %-2d\n", num1, num2, num3);    
    }
    return 0;
}

void ValidNumbers(int *num1, int *num2, int *num3)
{
    do {
        *num1 = rand() % HIGH_NUMBER;       // write through pointer of passed var
    } while (*num1 <= 50);                  // number > 50

    do {
        *num2 = rand() % HIGH_NUMBER;
    } while (*num2 % 2 != 0);               // even number

    do {
        *num3 = rand() % HIGH_NUMBER;
    } while (*num3 % 2 == 0);               // odd number
}

Program output:

79 16 79
95 2  37
73 28 93
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1

Returning multiple values is not allowed in C. So

return num1,num2,num3;

this will not work.


You can use a struct with 3 numbers and return that instead. You can go through this for an example of how do go about it.


Also note that the statement inside if after the return statement will never get executed, and are thus useless..

return num1,num2,num3;
i--;                                // <-- this will never get executed
printf("%d %d %d",num1,num2,num3);  // <-- this will never get executed
Community
  • 1
  • 1
Haris
  • 12,120
  • 6
  • 43
  • 70
0

There are two ways in C to return multiple parameters One is mentioned above using 1 struct to hold all the return values inside struct.

Another is using pointer/address to hold the values.

I revised the script to show both ways. I will add a positive comment for HelloWorld, you guys are too harsh, it is an important concepts in c:

#include <stdlib.h>
#include <stdio.h>
#define HIGH_NUMBER 100
typedef struct Random Random;
Random *isValidNumbers(int *num1,int *num2, int *num3);
struct Random{
    int a;
    int b;
    int c;
};

int main(void)
{
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    /* pr to use as parameter, rr used as a return struct , just for demoo purpuse*/
    Random *p;
    p = isValidNumbers(&num1,&num2,&num3);
    printf("%d %d %d main return random numbers\n",num1,num2,num3);
    printf("%d %d %d main return from struct   \n",p->a,p->b,p->c);
    return 0;
}

Random *isValidNumbers(int *num1,int *num2, int *num3)
{
    struct Random *r = malloc(sizeof(Random));
    int i=1,ans = 0;
    do
    {
        srand (time(NULL));
        *num1 = rand()%HIGH_NUMBER;
        *num2 = rand()%HIGH_NUMBER;
        *num3 = rand()%HIGH_NUMBER;
        r->a = *num1;
        r->b = *num2;
        r->c = *num3;

        if ((*num1%2==0||*num2%2||*num3%2==0)&&(*num1%2==1||*num2%2==1||*num3%2==1)&&(*num1>50||*num2>50||*num3>50))
        {
            i--;
            printf("%d %d %d inside isValidNumber\n",*num1,*num2,*num3);
            break;
        }
    }
    while (i);
    return r;
}

Results:

cmake28 ..
make
[gliang@www build]$ ./src/random
99 99 44 inside isValidNumber
99 99 44 main return random numbers
99 99 44 main return from struct
Gang
  • 2,658
  • 3
  • 17
  • 38