Saturday, April 5, 2014

C Control Statements

Oracle Certification Program Candidate Guide



A program cannot always be a sequential set of statements consisting of only assignment and input/output statements. While developing programs to solve any problem, it is often necessary to carry out a logical test and depending upon the outcome, a new course of action is taken. Like most programming languages, C supports such conditional execution with the help of if..else or switch..case statements. Similarly, for executing a group of statements repetitively C provides a set of statements such as for statement, while-do statement, do-while statement. These classes of statements are called control statements.

5.1 if....else STATEMENT

The syntax of if statement/or if else statement which requires evaluation of a condition and branching according to the outcome of the decision test is written as follows :

if()

{

}

else

{

}

Since C treats logical values as integer type with value 0 for false and non-zero for true, the (enclosing the block within curly braces becomes mandatory if a block consists of multiple statements) is executed if returns a non zero value. The is executed when the in the if statement returns a zero value (false).

Example 5.1 :

# include

main()

{

int min,x,y;

printf(“Enter the value of x and y :”);

scanf(“%d %d”,&x,&y);

fflush(stdin);

/*Computes minimum of x and y */

if(x

min = x;

else

min = y;

printf(“%d is the minimum of %d and %d
”,min,x,y);

}

The if statements can be nested as in

if e1 s1;

else if e2 s2;

else s3;

or,

if e1

if e2

s1;

else s2;

else s3;

Example 5.2 :

# include

#define TRUE 1

#define FALSE 0

main()

{

int EQUAL,min,x,y,max;

printf(“Enter the value of x and y :”);

scanf(“%d %d”,&x,&y);

fflush(stdin);

/*Computes minimum of x and y */

if(x == y)

{

EQUAL=TRUE;

min=max=x;

}

else

{

EQUAL=FALSE;

if(x > y)

{

max=x;

min=y;

}

else

{

max=y;

min=x;

}

}

if(!EQUAL)

printf(“Minimum : %d Maximum : %d
”,min,max);

else

printf(“Both x and y are equal
”);

}

1. Identify the mistakes in the following program.

# include

main()

{

float basic;

printf(“Enter basic :”);

scanf(“%f”,&basic);

fflush(stdin);

if(basic = 0)

printf(“Invalid input
”);

else

printf(“Basic is %.2f”,basic);

}

2. Predict the output of the following C program

# include

main()

{

int a, b, c;

scanf (“%d %d %d”, &a, &b, &c);

if (a >b)

{

a + = b;

a++;

}

if(a >c)

a * = c;

else

c - = (a + b);

printf (“%d%d%d
”, a,b,c);

}

5.2 SWITCH STATEMENT

The switch statement is a generalisation of the if statement.

The syntax is

switch

statement

The switch statement is a compound statement which specifies alternate course of actions. Each alternative is expressed as a group of one or more statements which are identified by one or more labels called case labels. The following two different programs, intended to perform the same task, illustrate how nested if can be replaced by a switch....case construct.

/*program having nested if statements */

# include

main()

{

char category;

printf(“Enter Category :”);

category=getchar();

fflush(stdin);

if(category == ‘B’)

{

printf(“B.TECH Students
”);

/* B. TECH Processing */

}

else if(category == ‘M’)

{

printf(“M.Sc. Student
”);

/* M.Sc. Processing */

}

else

if(category == ‘T’)

{

printf(“M.TECH Student
”);

/* M.TECH Processing */

}

else

if(category == ‘P’)

{

printf(“Ph. D. Student
”);

/* Ph. D. Processing */

}

else

{

printf(“ERROR
”);

/* Error Processing */

}

}

/* program with switch case construct */

# include

main()

{

char category;

printf(“Enter Category :”);

category=getchar();

fflush(stdin);

switch(category)

{

case ‘B’ :

printf(“B.TECH Students
”);

/* B. TECH Processing */

break;

case ‘M’ :

printf(“M.Sc. Student
”);

/* M.Sc. Processing */

break;

case ‘T’ :

printf(“M.TECH Student
”);

/* M. TECH Processing */

break;

case ‘P’ :

printf(“Ph. D. Student
”);

/* Ph. D. Processing */

break;

default :

printf(“ERROR
”);

/* Error Processing */

}

}

5.3 REPETITIVE STATEMENTS IN C

C supports following three types of repetitive statements.

1. while( )

{

<>

}

2. do

{

} while( );

3. for (expression1; expression2; expression3)

{

<>

}

In the while and do while statements the evaluates true if it has non-zero value. In the while statement, the is executed when the returns true (non-zero value). In the do-while statement, the evaluation of the expression is done after the execution of the statement 1. The block is repeated only when this expression returns true (non-zero value). In the for statement, the expression1 is used to initialise the index parameter that controls the loop execution. The expression2 represents the condition to be satisfied for the loop to continue execution and the expression3 is used to alter the value of the index parameter. When the for statement is executed, the expression2 is evaluated and tested before every pass through the loop. The expression3 is executed at the end of every pass.

Consider the following program for computation of average which is shown to have been implemented using three types of repetitive statements.

Example 5.4 :

# include

main ()

/* calculate the average of a set of items; The variable maxitem determines the total number of items*/

{

int maxitem, n = 1;

float data, mean, sum =0;

printf(“How many items?”);

scanf (“%d”, &maxitem);

/* consider the following repetitive statements which accept numbers from the user as input once in every pass and calculate sum of all these numbers. The use of three different repetitive constructs are demonstrated here */

for(n=1;n<=maxitem;n++)

{

scanf(“%f”,&data);

sum += data;

}

do

{

scanf(“%f”,&data);

sum += data;

++n;

} while(n <= maxitem);

while(n <= maxitem)

{

scanf(“%f”,&data);

sum += data;

++n;

}

mean=sum / maxitem;

printf(“
The average is %f
”,mean);

}

Example 5.5 :

/* Program to check whether the number provided as input is a palindrome or not */

# include

main()

{

int number,digit,reverse=0,store_num;

printf(“
Accept any number :”);

scanf(“%d”,&number);

fflush(stdin);

store_num=number;

do{

digit=number % 10;

reverse=(reverse * 10) + digit;

number /= 10;

}while(number != 0);

if(number == reverse)

printf(“The number is a palindrome
”);

else

printf(“The number is not a palindrome
”);

}

Comma operator :

This operator is used along with the for statement to handle multiple indices. Thus for (expression1a, expression1b; expression2; expression) statement is used to initialise two separate indices through expression1a and 1b. Similarly for (expression1; expression2; expression3a, expression 3b) can be used to alter the values of the indices based on expression 3a and expression 3b.

Example 5.6 :

for (i=1, j=2; i<5,>

{

.......

}

The break statement

break;

This statement is used to terminate loops or to exit from a switch.

The continue statement is used to bypass the remaining statements in a loop i.e., these statements are skipped and next pass begins. It is used with while, do - while or for statements. 3. Predict the output of the following C programs.

a.

# include

main()

{

int i=0,x=0;

for(i=1;i <>

{

if(i % 2 == 1)

x += i;

else

x--;

printf(“%d ”,x);

}

printf(“
x = %d”,x);

} b. # include

main()

{

int a, b, c=0;

for(a=0; a <>

for(b=0; b <>

{

c += (a + b - 1);

printf(“%d”,c);

}

printf(“
c=%d”, c);

}

c. # include

main()

{

int a,b,c,i=0;

for(a=0;a <>

for(b=0;b <>

{

switch(a + b )

{

case 0:

i += 1;

break;

case 1: i + = 2;

case 2: i + = 3;

case 3:

i += 4;

break;

default :

i += 5;

}

printf(“%d ”,i);

}

printf(“
i = %d”,i);

}

Read More..

Friday, April 4, 2014

Array of Functions

In the article
Pointers to Function
, we saw how pointers can be made to point
at functions and hence can be used to invoke them.


By far the most important use of pointers to functions is to have arrays of
functions. This can be achieved as stated below


You already know that we can have arrays of pointers and pointers can be made
to point at functions. So combining both we can have array of pointers to functions
put differently, we can have array of functions.


The example program below demonstrates how we can have array of functions;
please note that this concept is mostly used in writing compilers and interpreters,
so you shouldn’t expect the program to do anything serious or useful!


  // Program to demonstrate
// array of functions
#include<iostream.h>

// -- FUNCTION PROTOTYPES --
void func1();
void func2();
void func3();
void func4();
void func5();
// -- ENDS --

void main()
{
// notice the prototype
void (*ptr[5])();

// arrays are made to point
// at the respective functions
ptr[0]=func1;
ptr[1]=func2;
ptr[2]=func3;
ptr[3]=func4;
ptr[4]=func5;

// now the array elements
// point to different functions
// which are called just like
// we access the elements of
// an array
for(int i=0;i<5;i++)
(*ptr[i])();
}

// -- FUNCTIONS DEFINITION --
void func1()
{
cout<<"Called Func1!
";
}

void func2()
{
cout<<"Called Func2!
";
}

void func3()
{
cout<<"Called Func3!
";
}

void func4()
{
cout<<"Called Func4!
";
}

void func5()
{
cout<<"Called Func5!
";
}
// -- ENDS --

Good-Bye!


Related Articles:


Read More..

An Example of User Authentication System in PHP

In this post we’re going to create a very simple user authentication
system in PHP. It’d be like the one’s you see while logging in to
various sites/services (emails, forums, social networking sites etc)


User authentication is a way for sites to know who you are among the other
registered users and showing you relevant content (may be confidential). For
example it’s only you ho is authorized to see your emails because you
only know your authentication information.


In this post we’re going to create two files, a HTML page which will
collect the username and password in a form. These information will then be
send to a PHP script, which will verify and show the required information.


Below is the PHP code:



<?php
//define some constants
define("USERNAME", "goodjoe");
define("PASSWORD", "123456");
define("REALNAME", "Joe Burns");

//have the data being passed
$user=$_GET[user];
$pass=$_GET[pass];

//if username and password match
if($user==USERNAME && $pass==PASSWORD)
{
echo "<h1>Hello ".REALNAME."</h1>";
echo "<p>Nice to see you logging in again...</p>";
echo "<p>USER: <i>".USERNAME."</i></p>";
}
//if not
else
{
echo "<h1>Wrong username or password!</h1>";
}
?>


The code above is pretty straightforward. You may change the constants that
hold the user information.


Now, as you know from Taking
User Inputs to Create Personalized Pages II
, we need a HTML page with
a form to send information to this script. Here it is:



<html>
<head>
<title>Simple Uesr Authentication System</title>
</head>

<body>
<form name="form1" method="get" action="verify.php">
<p>Uername
<input name="user" type="text" id="user">
</p>
<p>Password
<input name="pass" type="password" id="pass">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>


Refer to Taking
User Inputs to Create Personalized Pages II
to know more about the
form tag. Note that the PHP script must be of the name as in the “action=…”
of the form tag and in the same directory as the HTML page.


Notice this line


   <input name="pass"    type="password" id="pass">

The above code creates an element that shows asterisks (*) on entering anything,
just like on other sites.


Now, we’re ready to put these files on the server and request the HTML
page. Do it and play with the page for a while!


Creating a Simple User Authentication System in PHP



This is how it’d look.


A few points to note:




  • This is a very simple example and holds the user information in the script
    itself (hard coded). Real sites store user information in Databases.




  • For real-life applications, we’d also need to use session variables
    or cookies.




[Update: Read the next post An
Example of User Authentication System in PHP II
]


Related Articles:


Read More..

Tuesday, April 1, 2014

Ferrari 550 Maranello 1998 Ferrari 550 Wallpaper 1


Ferrari 550 Maranello

Ferrari 550 Maranello (2)

Ferrari 550 Maranello (3)

Ferrari

Ferrari

Ferrari

Ferrari

Ferrari

Ferrari

Ferrari

Read More..