Friday, October 31, 2008

Learnings of the Week! (Sept. 29-Oct 31)

By: Steffany Queen P. Bigoy

This week, we have performed a series of activities, I admit that I haven’t perfect the scores but still I learned a lot of lessons. It is so useful and very important to us for our future.

Below, you will see how the user values were read.

Example:

#include
int main()
{
int a, b, c;
printf("Enter the first value:");
scanf("%d", &a);
printf("Enter the second value:");
scanf("%d", &b);
c = a + b;
printf("%d + %d = %d\n", a, b, c);
return 0;
}

Note that scanf uses the same sort of format string as printf (type man scanf for more info). Also note the & in front of a and b. This is the address operator in C: It returns the address of the variable (this will not make sense until we discuss pointers). You must use the & operator in scanf on any variable of type char, int, or float, as well as structure types (which we will get to shortly). If you leave out the & operator, you will get an error when you run the program. Try it so that you can see what that sort of run-time error looks like.

Let's look at some variations to understand printf completely. Here is the simplest printf statement:

printf("Hello");

This call to printf has a format string that tells printf to send the word "Hello" to standard out. Contrast it with this:

printf("Hello\n");

The difference between the two is that the second version sends the word "Hello" followed by a carriage return to standard out.

The following line shows how to output the value of a variable using printf.

printf("%d", b);

The %d is a placeholder that will be replaced by the value of the variable b when the printf statement is executed. Often, you will want to embed the value within some other words. One way to accomplish that is like this:

printf("The temperature is ");
printf("%d", b);
printf(" degrees\n");

An easier way is to say this:

printf("The temperature is %d degrees\n", b);

You can also use multiple %d placeholders in one printf statement:

printf("%d + %d = %d\n", a, b, c);

In the printf statement, it is extremely important that the number of operators in the format string corresponds exactly with the number and type of the variables following it. For example, if the format string contains three %d operators, then it must be followed by exactly three parameters and they must have the same types in the same order as those specified by the operators.

You can print all of the normal C types with printf by using different placeholders:

  • int (integer values) uses %d
  • float (floating point values) uses %f
  • char (single character values) uses %c
  • character strings (arrays of characters, discussed later) use %s

October 27 - 31, 2008

LEARNINGS OF THE WEEK
By: Cielito M. Cantero
IV – Rizal

seMbReak tYm!!!

8s the eNd of the 2nd Grading period…yaHoo!!

This week, I learned………nothing!!!..hehe,. rest time kasi na min ‘to..haha..

That’s aLL..Thank u!! (n_n)

Learnings of the week (Lorebeth)

October 27-31
by: Lorebeth Betinol


DRUM ROLL PLEASE...............................................................................................................................


I have Learned nothing because it is our semestral break, despite of having this break we are still busy with some projects.. That's all.. thank you.

wHEW!! iT SEEMS LIKE THIS ONE IS soooooooooooooooooooooooooooooooooo long..

Bye...

Saturday, October 25, 2008

Learnings of the week (Lorebeth)(Ocober 20-24)

This Week we are going to have our periodical test for the second quarter, so.... no serious class really happened.

Only the batch two had their activity about programming.
Learnings of the Week (Oct. 20-24)

By: Frea Diane T. Bautista

Ahmmm..this week, we just had a review. No new lessons have discussed. The Batch 2 had just perform an activity in the Computer Laboratory. And on 24 we had our 2nd Periodical Exam in TLE.

October 20 - 24, 2008

LEARNINGS OF THE WEEK
By: Cielito M. Cantero
IV – Rizal



Our Second Grading Examination was done this week last October 24 and the test was very hard for me specially the programming part. It takes a lot of time for me to answer it.

The day before the examination, we had an activity about programming. And again and again…I wasn’t able to solve the problem properly but I was able to run it.

We also had done the business planning this week. It really takes time and effort to make such project. And so teamwork and cooperation is a big help. Even though we had hard time to finish it, we still finished it and got high grades..hehe..

Saturday, October 18, 2008

Learnings of the Week(Oct.13-17)

By: Frea Diane T. Bautista

This week, we learned alot about looping and branching. We used it in some of our activities but what do looping and branching really mean.

For me, The for Statement

This is the famous for loop structure that many of you have been using to write your own algorithms. The for loop is used when the number of loops is known (unlike the while loop, as we will see later). The syntax of the for loop is:

for (initialization; conditional expression; stepping)
{
statement 1;
statement 2;
}

As you can see, the initialization, the expression that controls the loop, and the stepping parts are all defined in the top of the statement and in one location. The parts are also separated by semicolons, and you begin the looped statements, or the statements that will be executed in the loop, using a block ( { } ). The first part of the for statement is the initialization part; use this part to initialize the variables that will be used in the algorithm of the for loop. Note that these variables are allocated on the stack, and this part will be executed only one time, because it doesn't make any sense to declare and initialize the same variable with the same value each time in the loop.

The next part is the conditional expression, which determines whether the loop will be executed again or not. If the condition (something like i < myArray.Length) evaluated to false, control passes to the first statement after the for statement block. If the condition evaluated to true, the body of the for block (the controlled statements) will be executed.

The last part is the stepping part. Usually it will be a counter that will increment the variable that the conditional expression uses, because at some point we need the conditional expression to evaluate to false and terminate the execution of the for loop. Note that the stepping part executes after the controlled statements. That is, first the initialization part executes (one time only) and the variables allocate space on the stack; second, the conditional expression is evaluated, and if true, the controlled statements execute, followed by the stepping part execution, and again the conditional expression is evaluated and the process iterates.

Note that any of the three parts that make the for statement can be empty; although it's not common, it can happen. Take a look:

static void Main(string[] args)
{
int x = 10;
for(; x < 100; x += 10)
{
Console.WriteLine(x);
}
Console.ReadLine();
}

Compile the method into a class and run the application; you will get the following result:



As you can see, we omit the initialization part and we just put in the semicolon. Also note that the counter can increment or decrement, and it's up to you to define the algorithm.

The for loop can be used to define very complex algorithms; this happens as a result of nesting the for loops. Let's take a very simple example which extends the above loop example. It will simply write the same numbers, but this time with a little difference.

public class Loops
{
static void Main(string[] args)
{

for(int x = 10; x < 100; x += 10)
{
Console.WriteLine();
Console.WriteLine(x);
for(int y = x - 1, temp = x - 10; y > temp; y--)
{
Console.Write("{0}, ", y);
}
}
Console.ReadLine();
}
}

Compile the class and run it, and you will get the following result:



I have used a nest for loop to print all the numbers between two iterations from the outer for loop. The ability to use for loops actually makes great programmers.

LEARNINGS OF THE WEEK (Oct. 13 - Oct. 17)


LEARNINGS OF THE WEEK

By: Cielito M. Cantero

IV – Rizal

The branching and Looping!..

Branching and Looping

In C, both if statements and while loops rely on the idea of Boolean expressions. Here is a simple C program demonstrating an if statement:

#include 
 
int main()
{
int b;
printf("Enter a value:");
scanf("%d", &b);
if (b <>
printf("The value is negative\n");
return 0;
}

This program accepts a number from the user. It then tests the number using an if statement to see if it is less than 0. If it is, the program prints a message. Otherwise, the program is silent. The (b <> portion of the program is the Boolean expression. C evaluates this expression to decide whether or not to print the message. If the Boolean expression evaluates to True, then C executes the

single line immediately following the if statement (or a block of lines within braces imme

diately following the if statement). If the Boolean expression is False, then C skips the line or block of lines immediately following the if statement.



Here's slightly more comp

lex example:

#includ
e
 
int main()
{
int b;
printf("Enter a value:");
scanf("%d", &b);
if (b <>
printf("The value is negative\n");
else if (b == 0)
printf("The value is zero\n");
else
printf("The value is positive\n");
return 0;
}

In this example, the else if and else sections evaluate

for zero and positive values as well.

Here is a more complicated Boolean expression:

if ((x==y) && (j>k))
z=1;

else
q=10;

This statement says, "If the value in variable x equals the value in variable y, and if the value in variable j is greater than the value in variable k, then set the variable z to 1, otherwise set the variable q to 10." You will use if statements like this throughout your C programs to make decisions. In general, most of the decisions you

make will be simple ones like the first example; but on occasion, things get more complicated.

Notice that C uses == to test for equality, while it uses = to assign a value to a variable. The && in C represents a Boolean AND operation.

Here are all of the Boolean operators in C:

equality          ==

less than         <
Greater than      >
<=                <=
>=                >=
inequality        !=
and               &&
or                ||
not               !

You'll find that while statements are just as easy to use as if statements. For example:

while (a <>
{
printf("%d\n", a);
a = a + 1;
}

This causes the two lines within the braces to be executed repeatedly until a is greater than or equal to b. The while statement in general works like this:



C also provides a do-while structure:

do
{
printf("%d\n", a);
a = a + 1;
}
while (a <>

The for loop in C is simply a shorthand way of expressing a while statement. For example, suppose you have the following code in C:

x=1;
while (x<10)
{
blah blah blah
x++; /* x++ is the same as saying x=x+1 */
}

You can convert this into a for loop as follows:

for(x=1; x<10;>
{
blah blah blah
}

Note that the while loop contains an initialization step (x=1), a test step (x<10), and an increment step (x++). The for loop lets you put all three parts onto one line, but you can put anything into those three parts. For example, suppose you have the following loop:

a=1;
b=6;
while (a <>
{
a++;
printf("%d\n",a);
}

You can place this into a for statement as well:

for (a=1,b=6; a <>

It is slightly confusing, but it is possible. The comma operator lets you separate several different statements in the initialization and increment sections of the for loop (but not in the test section). Many C programmers like to pack a lot of information into a single line of C code; but a lot of people think it makes the code harder to understand, so they break it up.

= vs. == in Boolean expressions

The == sign is a problem in C because every now and then you may forget and type just = in a Boolean expression. This is an easy mistake to make, but to the compiler there is a very important difference. C will accept either = and == in a Boolean expression -- the behavior of the program changes remarkably between the two, however.

Boolean expressions evaluate to integers in C, and integers can be used inside of Boolean expressions. The integer value 0 in C is False, while any other integer value is True. The following is legal in C:

#include 
 
int main()
{
int a;
 
printf("Enter a number:");
scanf("%d", &a);
if (a)
{
printf("The value is True\n");
}
return 0;
}

If a is anything other than 0, the printf statement gets executed.

In C, a statement like if (a=b) means, "Assign b to a, and then test a for its Boolean value." So if a becomes 0, the if statement is False; otherwise, it is True. The value of a changes in the process. This is not the intended behavior if you meant to type == (although this feature is useful when used correctly), so be careful with your = and == usage.

Friday, October 17, 2008

learnings of the week (lorebeth betinol)

This week I can say that we are so busy. We had our activity about computer programming and its all about Conditional statements, though I can say that its hard but I can say that I'm learning
every sesssion and that's important.... I'm signing out.

Friday, October 10, 2008

LEARNINGS OF THE WEEK (Oct. 6 - Oct. 10)

LEARNINGS OF THE WEEK

By: Cielito M. Cantero

IV – Rizal

This week, we still have series of activities in programming using the turbo. We usually use scanf in programming. But now, let’s see how it works.

The scanf function allows you to accept input from standard in, which for us is generally the keyboard. The scanf function can do a lot of different things, but it is generally unreliable unless used in the simplest ways. It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to-use.

The simplest application of scanf looks like this:

scanf("%d", &b);

The program will read in an integer value that the user enters on the keyboard (%d is for integers, as is printf, so b must be declared as an int) and place that value into b.

The scanf function uses the same placeholders as printf:

  • int uses %d
  • float uses %f
  • char uses %c
  • character strings (discussed later) use %s

You MUST put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.

In general, it is best to use scanf as shown here -- to read a single value from the keyboard. Use multiple calls to scanf to read multiple values. In any real program, you will use the gets or fgets functions instead to read text a line at a time. Then you will "parse" the line to read its values. The reason that you do that is so you can detect errors in the input and handle them as you see fit.

The printf and scanf functions will take a bit of practice to be completely understood, but once mastered they are extremely useful.

Try This!

  • Modify this program so that it accepts three values instead of two and adds all three together:
·                #include 
·                 
·                int main()
·                {
·                int a, b, c;
·                printf("Enter the first value:");
·                scanf("%d", &a);
·                printf("Enter the second value:");
·                scanf("%d", &b);
·                c = a + b;
·                printf("%d + %d = %d\n", a, b, c);
·                return 0;
·                }
  • Try deleting or adding random characters or words in one of the previous programs and watch how the compiler reacts to these errors.

For example, delete the b variable in the first line of the above program and see what the compiler does when you forget to declare a variable. Delete a semicolon and see what happens. Leave out one of the braces. Remove one of the parentheses next to the main function. Make each error by itself and then run the program through the compiler to see what happens. By simulating errors like these, you can learn about different compiler errors, and that will make your typos easier to find when you make them for real.

C Errors to Avoid

  • Using the wrong character case - Case matters in C, so you cannot type Printf or PRINTF. It must be printf.
  • Forgetting to use the & in scanf
  • Too many or too few parameters following the format statement in printf or scanf
  • Forgetting to declare a variable name before using it