Thursday 20 March 2014

Program in c to find greatest among three numbers via function

/*… Hey guy’s this is a simple program with a twist  i have made it with function… try it…*/
#include <stdio.h>
 /* function prototype */
int maximum( int, int, int );
/*…main function starts..*/
void main()
{
int a, b, c;
printf( “Enter three integers: ” );
scanf( “%d%d%d”, &a, &b, &c );
printf( “Maximum is: %d\n”, maximum( a, b, c ) );
getch();
}
/*…Main Function End’s…*/
/* Function maximum definition */
int maximum( int x, int y, int z )
{
int max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max;
}

//The output is:::::::::::::::::::

Image

program in c to show the “randomization of die-roll”

/*…… hey guy’s it is a randomizing die rolling program…*/
#include <stdlib.h>
#include<conio.h>
#include <stdio.h>
/*…Main function start’s….*/
void main()
{
/*….Initialization…*/
int i;
unsigned seed;
printf( “Enter seed: ” );
scanf( “%u”, &seed );
srand( seed );        //here srand is used to randomise the seed…….
/*….Loop start’s…*/
for ( i = 1; i <= 10; i++ ) 
{
printf( “%10d”, 1 + ( rand() % 6 ) );
/*…Condition checking..*/
if ( i % 5 == 0 )
printf( “\n” );
}
/*….Loop end’s….*/
getch();
}
/*…Main function end’s..*/

Image

PROGRAM IN C TO ILLUETRATE THE USE OF “SRAND” WITH “SWITCH STATEMENT”

//IT IS JUST A GEME OF CHANCES AND I HAVE NAMED IT ” ROLL BABY ROLL…”
//rules are so simple listed below——–
–Roll two dice
•7 or 11 on first throw, player wins
•2, 3, or 12 on first throw, player loses
•4, 5, 6, 8, 9, 10 – value becomes player’s “point”
–Player must roll his point before rolling 7 to win

/*… Now programming begins…*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rollDice( void );
/*…Main function begin’s…*/
void main()
{
int gameStatus, sum, myPoint;
srand( time( NULL ) );
sum = rollDice();                                    /* first roll of the dice */
switch ( sum ) {
case 7: case 11:                                   /* win on first roll */
gameStatus = 1;
break;
case 2: case 3: case 12:                      /* lose on first roll */
gameStatus = 2;
break;
default:                                               /* remember point */
gameStatus = 0;
myPoint = sum;
printf( “Point is %d\n”, myPoint );
break;
}
while ( gameStatus == 0 )                    /* keep rolling */
{                                                     
sum = rollDice();
if ( sum == myPoint )                           /* win by making point */
gameStatus = 1;
else
if ( sum == 7 )                                       /* lose by rolling 7 */
gameStatus = 2;
}
if ( gameStatus == 1 )
printf( “Player wins\n” );
else
printf( “Player loses\n” );
getch();
}
/*…Main function end’s…*/
int rollDice( void )
{
int die1, die2, workSum;
die1 = 1 + ( rand() % 6 );
die2 = 1 + ( rand() % 6 );
workSum = die1 + die2;
printf( “Player rolled %d + %d = %d\n”, die1, die2, workSum );
return workSum;
}

Image

what is ENUMERATION IN C

  Enumeration
  Set of integers represented by identifiers
–Enumeration constants – like symbolic constants whose values automatically set
•Values start at 0 and are incremented by 1
•Values can be set explicitly with =
•Need unique constant names
–Declare variables as normal
•Enumeration variables can only assume their enumeration constant values (not the integer representations)

PROGRAM IN C TO SHOW THAT THE ADDRESS AND VALUE OF A POINTER VARIABLE ARE COMPLEMENTS OF EACH OTHER

/*hey guy’s this is just a simple program that illustrate you  the “&” and ”  * ”  operators used on pointers……. */
#include <stdio.h>
/*….Main function Starts…*/
void main()
{
int a;                                                            /* a is an integer */
int *aPtr;                                                       /* aPtr is a pointer to an integer */
a = 7;
aPtr = &a;                                                      /* aPtr set to address of a */
printf( “The address of a is %p” “\nThe value of aPtr is %p”, &a, aPtr );
printf( “\n\nThe value of a is %d” “\nThe value of *aPtr is %d”, a, *aPtr );
printf( “\n\nShowing that * and & are inverses of “ ”each other.\n&*aPtr = %p” “\n*&aPtr = %p\n”, &*aPtr, *&aPtr );
getch();
}
/*…Main function ends…*/

Image

PREDEFINED SYMBOLIC CONSTANTS

Five predefined symbolic constants
Cannot be used in #define or #undef——
given below………….


Symbolic constant Description
__LINE__ The line number of the current source code line (an inte¬ger constant).
__FILE__ The presumed name of the source file (a string).
__DATE__ The date the source file is compiled (a string of the form “Mmm dd yyyy” such as “Jan 19 2001″). 
__TIME__ The time the source file is compiled (a string literal of the form “hh:mm:ss”).

CALL BY REFERENCE

Call by reference with pointer arguments
–Pass address of argument using & operator
–Allows you to change actual location in memory
–Arrays are not passed with & because the array name is already a pointer
* operator 
–Used as alias/nickname for variable inside of function
void double(int *number)
{
*number = 2 * (*number);
}
*number used as nickname for the variable passed