Program to show Macro to generate pseudo-random number from 0 to NumValues in C



#include
#include
#include
/* Macro to generate pseudo-random number from 0 to NumValues */
#define random (NumValues) ((int)(((double)(rand())*(NumValues))/(RAND_MAX+1.0)))
#define iterations 6
#define test /* Select testing output */
#define testf /* Select function call trace */
#define repeatable /* Select repeatable execution */
/* Function prototypes */
int sum(int, int);
int product(int, int);
int difference(int, int);
int main(void)
{
int funsel = 0; /* Index for function selection */
int a = 10, b = 5; /* Starting values */
int result = 0; /* Storage for results */

/* Function pointer array declaration */
int (*pfun[])(int, int) = {sum, product, difference};
/* Conditional code for repeatable execution */
#ifdef repeatable
srand(1);
#else
srand((unsigned int)time(NULL)); /* Seed random number generation */
#endif
/* Execute random function selections */
int element_count = sizeof(pfun)/sizeof(pfun[0]);
for(int i = 0 ; i < iterations ; i++)
{
/* Generate random index to pfun array */
funsel = random(element_count);
if( funsel>element_count-1 )
{
printf("\nInvalid array index = %d", funsel);
exit(1);
}
#ifdef test
printf("\nRandom index = %d", funsel);
#endif
result = pfun[funsel](a , b); /* Call random function */
printf("\nresult = %d", result );
}
return 0;
}

/* Definition of the function sum */
int sum(int x, int y)
{
#ifdef testf
printf("\nFunction sum called args %d and %d.", x, y);
#endif

return x + y;
}
/* Definition of the function product */
int product( int x, int y )
{
#ifdef testf
printf("\nFunction product called args %d and %d.", x, y);
#endif

return x * y;
}
/* Definition of the function difference */
int difference(int x, int y)
{
#ifdef testf
printf("\nFunction difference called args %d and %d.", x, y);
#endif
return x - y;
}


Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!