Thursday, December 18, 2008

3rd-Learnings of the Week 5 [Caligdong]

The eleventh chapter is all about recursion. The repetitive process by which a functions calls itself is called recursion or circular definition. This is a way of defining something in terms of itself. A function is said to be recursive if a statement in the body of the function calls the function that contains it.

Base Case is the part of the recursive function that is found on the if clause. This contains the condition that should be satisfied at one point of execution to terminate the repetitive process done by the recursive function. General Case is the part of the recursive function that is found on the else-clause. This contains the function call of the recursive function to itself.

Direct recursions are recursive functions that can call itself through a function call directly inside the body of the function. Indirect recursions are recursive functions that can call another functions outside its function.


example for recursion:
Give the output of the ff. program when the value entered for a=5.

#include
main()
{
int a, b;
clrscr();
printf(“Enter a value:”);
scanf(“%d”, &a);
b= solve (a);
printf(“The new value is %d”, b);
getch();
}
solve (int a)
{ if (a == 1) return 2;
else return (solve (a-1) + 2);
}

OUTPUT

Enter a value: 5
The new value is 10.

Wednesday, December 17, 2008

3rd-Learnings of the Week 4 [Caligdong]

We have also discussed about iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met. It can be predefined as in the loop, or open ended as in while and do-while. There are three types of Iterative statements: (1) The For statements ,(2)The While statements (3)The Do-While statements.

We must also be aware of the following when using for statements. The For statement or for loop is considered as a predefined loop because the number or times it iterates to perform its body is predetermined in the loop’s definition. The For loop contains a counter whose values determine the number of times the loop iterates. The iteration stops upon reaching the number of times specified in the loop. Never place a semicolon right after the for header. Never change the value of the for loop’s counter in side the body of the loop. This will affect the result of the program. The increment part of the for loop is execute after the first iteration of the loop.

The while statement or while loop is an open-ended or event-controlled loop. The second type of open-ended or event-controlled loop is the do-while statement or do-while loopDo-While is a variation of the while statement which checks the condition at the bottom / end of the loop.This means that a do-while loop “always executes at least once”. In the do-while loop, when the condition evaluates to TRUE (1), the loop body will be executed, but when FALSE (0), program control proceeds to the next instruction after the do-while loop.

General forms:
FOR STATEMENT

for (initialization; condition; increment)
{
statement_sequence;
}

WHILE STATEMENT
while (condition)
{
statement_sequence;
}

DO-WHILE STATEMENT
do
{
statement_sequence;
} while (condition);

Saturday, December 6, 2008

3rd-Learnings of the Week 3 [Caligdong]

false false false MicrosoftInternetExplorer4 we have discussed about the functions and structured programming of chapter 10.
  • A c program is composed of at least one function definition, that is the main() function.
  • Execution of the program begins with main() and also ends with the main() function.
  • However, a C program can also be composed of other functions aside from the main().
  • The c program presented in previous slide is composed of 3 functions: the main function, the function greet1 and the function greet2.
  • Therefore we can say that we can create a program that is composed of other function aside from the main function.
  • Note: The main( ) function should always be present in every C program.
  • The c program presented in previous slide is composed of 3 functions: the main function, the function greet1 and the function greet2.
  • Therefore we can say that we can create a program that is composed of other function aside from the main function.
  • Note: The main( ) function should always be present in every C program.

FUNCTIONS

  • Functions are the building blocks of C in which all program activity occurs.
  • A function is also called a subprogram or subroutine. It is a part of a C program that performs a task, operation or computation then may return to the calling part of the program.
  • Other functions aside from the main( ) can only be executed by the program through a “function call”.
  • Note: Function call is a C statement that is used to call a function to execute C statements found inside the function body.
  • Going back to the example, greet1( ); is an example of a function call, calling the function greet ( ).
  • main ------clrscr------printf
greet1( ) function call------greet1( ) function greet2 function call----greet2( ) function getch( )

GENERAL FORM OF A FUNCTION

function_type function_name (parameters list)

{

body of the function;

}

Where

  • function_type specifies the type of value that the function will return.
  • function_name is any valid identifier name which will name the function.
  • Parameter list is a comma separated list of variables that receive the values when the function is called.
  • body of the function is composed of valid c statements that the function will execute.

ACTUAL PARAMETERS

  • Actual Parameters are the variables found in the function call whose values will be passed to the formal parameters of the called function.
  • Formal Parameters are the variables found in the function header that will receive from the actual parameters.

CALL BY VALUE AND PASS BY VALUE

  • In the method call by value, the values of the actual parameters are passed to the formal parameters.

CALL BY VALUE

  • In the method call by value, the values of the actual parameters are passed to the formal parameters.
  • Changes that happen to the values of the formal parameters inside the function will not affect the values of the actual parameters.

PASS BY VALUE OR CALL BY REFERENCE

  • The actual parameters also pass their value to the formal parameters.
  • But the changes that happen to the values of the formal parameters inside the function will affect the values of the actual parameters.
  • This is because the actual address of the variables is passed using the address of operator (&) together with the pointer operator (*).

#include

  • sqrt(x)
  • fabs(x) - calculates the absolute value of a number
  • ceil(x) - ceil (11.25)=12
  • floor(x) - floor(11.25)=11
  • sin(x)
  • cos(x)
  • tan(x)
  • pow(x,y)