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)

Friday, November 21, 2008

3rd-Learnings of the Week 2 [Caligdong]

We have already discussed the eighth chapter which is the conditional statements. I learned that conditional statements are statements that check an expression then may or may not execute statements depending on the result of the condition. There are 6 kinds of conditional statements: (1) If Statement, (2) If-Else Statement, (3) Nested-If Statement, (4) If-Else-If Ladder, (5) Switch statement, and (6) Nested Switch Statement.

The general form:
if ( expression)
statement;

Expression is relational or Boolean expression that evaluates to a TRUE (1) or FALSE (0) value. Statement may either be a single C Statement or a block of C statements. In an if statement, if the expression evaluates to (1), the statement that forms the target of the if statement will be executed. Otherwise, the program will ignore the statement.

If an if-else statement, if the expression is (1), the statement after the if statement will be executed. Only the code associated with the if or the code is associated with the else executes, never both. In C, the else is linked to the closest preceding if that does not already have an else statement associated with it. The general form of the if-else statement with block of statement is:
If (expression)
{
statement_sequence;
}
else
{
statement_sequence;
}

A common programming construct in C is the if-else- if ladder. In an if-else-if ladder statement, the expression are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder will not be executed. If none of the condition is true, the final else is executed. The final else is optional, you may include this part if needed in the program or you may not include if not needed.

The switch statement is a multiple-branch decision statement. In a switch statement, a variable is successively tested against a list or integer or character constants. If a match is found, a statement or block of statement is executed. The default part of the switch is executed if no matches are found. The break statement is used to terminate the statement associated with each case constant. It is a C keyword which means that at that point of execution, you should jump to the end of the switch statement by the symbol }.

The general form of the nested switch statement is:
switch (variable)
{
case constant:{
switch (variable)
{
case constant1:
statement sequence_1;
break;
case constant2:
statement_sequence_2;
break;
}
break;
}
case constant2:
statement sequence;
break;
default:
statement sequence;
}

Thursday, November 20, 2008

3rd- Learnings of the Week 1 [Caligdong]

INPUT STATEMENT is used to input a single character or a sequence of characters from the keyboard. Types of input statement:

Getch = A function used to input a single character from the keyboard without echoing the character on the monitor. Syntax: getch();
Example: ch = getch();

Getche = A function used to input a single character from the keyboard, the character pressed echoed on the monitor, line the READLN in PASCAL Syntax: getche();
Example: ch = getche();

Getchar = A function used to input a single character from the keyboard, the character pressed echoed on the monitor terminated by pressing Enter key. Syntax: getchar();
Example: ch = getchar();
Gets = A function used to input a single character from the keyboard, spaces are accepted, terminated by pressing enter key. Syntax: gets();
Example: gets(ch);

Scanf = A function used to input a single character or sequence of characters from the keyboard, it needs the control string codes in able to recognized. Spaces are not accepted upon inputting. Terminated by pressing spacebar. Syntax: gets();
Example: gets(ch);

OUTPUT STATEMENT is used to display the argument list or string on the monitor. Types of output statement:


Puts = A function used to display the argument list or string on the monitor. It does not need the help of the control string codes. Syntax: puts();
Example: puts(“hello”);

All format specifiers start with a percent sign (%) and are followed by a single letter indicating the type of data and how data are to be formatted.
%c – used for single char in C
scanf(“%c”, &ch); printf(“%c”, ch);

%d – decimal number (whole number)
scanf(“%d”,&num); printf(“%d”,num);

%e – scientific notation / exponential form
scanf(“%e”, &result); printf(“%e”, result);

%f – number with floating or decimal point
scanf(“%f”,&pesos); printf(“%f”,pesos);

%o – octal number
scanf(“%o”, &valuet); printf(“%o”, value);

%s– string of characters
scanf(“%s”,&str); printf(“%s”,str);

%u – unsigned number
scanf(“%u”, &value); printf(“%u”, value);

%x– hexadecimal numbers
scanf(“%x”,&value); printf(“%x”,value);

%X – capital number for hexadecimal number
scanf(“%X”, &nos); printf(“%X”, nos);

%%– print a percent sign
scanf(“%%”,&value); printf(“%%”,value);

List of commonly used escape sequence:
\\ - prints backslash
\’ – prints single quotes
\” – prints double quotes
\? – prints question mark
\n - newline

A function gotoxy is used to send the cursor to the specified location. Syntax: gotoxy(x,y);
Example: gotoxy(5,10);

/*y is assigned a numeric literal*/

It stores a value or a computational result in a variable. They are commonly used to perform most arithmetic operations in a program. It can also be used in printf() statement. Syntax: variable = expression
Example: y=1;

Saturday, October 11, 2008

Learnings of the Week (Quennie Rose Colegado)

Conditional Statements
Are statements that check an expression then may or may not execute a statement or group of statement depending on the result of the condition.
Types of Conditional Statements:
The If Statement
The If-Else Statement
The Nested-If Statement
The If-Else-If Ladder
The Switch Statement
The Nested Switch Statement
The If Statement - In an if statement, if the expression evaluates to TRUE (1), the statement or the block of statements that forms the target of the if statement will be executed. Otherwise, the program will ignore the statement or the block of statements.
The If-Else Statement - If an if-else statement, if the expression is TRUE (1), the statement or block of statement after the if statement will be executed; otherwise, the statement or block of statement in the else statement will be executed.
Note: Only the code associated with the if or the code that is associated with the else executes, never both.
The Nested-If Statement - One of the most confusing aspects of the if statement in any programming language is nested ifs. A nested if is an if statement that is the object of either an if or else. This is sometimes referred to as “an if within an if.” The reason that nested ifs are so confusing is that it can be difficult to know what else associates with what if.
The Else-If-Else Ladder - In an if-else-if ladder statement, the expression are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder will not be executed. If none of the condition is true, the final else is executed.
The Switch Statement - In a switch statement, a variable is successively tested against a list or integer or character constants. If a match is found, a statement or block of statement is executed. The default part of the switch is executed if no matches are found.
The Nested Switch Statement - It is just a switch within a switch statement.
Conditional Statements
Are statements that check an expression then may or may not execute a statement or group of statement depending on the result of the condition.
Types of Conditional Statements:
The If Statement
The If-Else Statement
The Nested-If Statement
The If-Else-If Ladder
The Switch Statement
The Nested Switch Statement
The If Statement - In an if statement, if the expression evaluates to TRUE (1), the statement or the block of statements that forms the target of the if statement will be executed. Otherwise, the program will ignore the statement or the block of statements.
The If-Else Statement - If an if-else statement, if the expression is TRUE (1), the statement or block of statement after the if statement will be executed; otherwise, the statement or block of statement in the else statement will be executed.
Note: Only the code associated with the if or the code that is associated with the else executes, never both.
The Nested-If Statement - One of the most confusing aspects of the if statement in any programming language is nested ifs. A nested if is an if statement that is the object of either an if or else. This is sometimes referred to as “an if within an if.” The reason that nested ifs are so confusing is that it can be difficult to know what else associates with what if.
The Else-If-Else Ladder - In an if-else-if ladder statement, the expression are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder will not be executed. If none of the condition is true, the final else is executed.
The Switch Statement - In a switch statement, a variable is successively tested against a list or integer or character constants. If a match is found, a statement or block of statement is executed. The default part of the switch is executed if no matches are found.
The Nested Switch Statement - It is just a switch within a switch statement.