Sunday, November 25, 2007

Chapter 1, Lesson 4

Welcome to Beginners Programming

Before we go on, let’s take a little peep at variables. What are variables? A variable is a term given to a thing, or a certain value, that may change. In mathematics, a variable often represents an "unknown" quantity that has the potential to change, in computer science, it represents a place where a quantity can be stored.

Example:
X = 100
FirstName=”John Doe”
Sum = 1+1
Average = 300/5

Quiz1=3, Quiz2 =5, Quiz3=5
This_is_another_Average = (Quiz1+ Quiz2+ Quiz3)/3

Variables can be any letter or number but it cannot start with a number. Numerous tutorials or info about variables can be found over the internet nowadays, but we will stick on the basics.

The relational operator

= Equal to
> Greater than
<>= Greater than or equal to
<= Less than or equal to <> Not equal to

The mathematical operator
+ addition
- deduction
* Multiplication
/ Division


Are you ready?

Flowchart

During my school days, we actually had 1 course for flowcharting; we did not get to use the computer until we finish the flowcharting course. It is somewhat frustrating not to be able to use the computer but hey, when the time came, computer programming is not a problem anymore. For beginners, flowchart is essential because you can see the flow of the program before making the actual code. For now we dig ourselves with flowcharts. I’m sure every programmer knows about this but hey, I’m assuming you are a beginner, so here we go.

Firstly you have to memorize the basic flowchart symbols as seen in Figure 1 in Chapter 1, Lesson 1, so you will know what the symbol is for and can interpret what is going on with the flow of your program.

Problem 1

Draw a flowchart that would find the sum of the two numbers 5 and 7. Display the sum

Figure 1:



Let’s try to put a variable, and apply some rules. Initialize all variables.

Figure 2:


As you can see, in figure 1, we just added the two numbers and placed the result in a variable called Sum, then we print it on the screen, however, in figure 2, we created 3 variables , X, Y and Sum.


We let X=5 , Y=7 and Sum = 0. Then we compute for the sum.


Either way, we produce the same result, we printed out the Sum of the two numbers. So now the question will be, what if X and Y is unknown , and we let the user / operator decide what number to be computed, then we have problem 2.

Problem 2:

Draw a flowchart that would input 3 numbers. Find the sum of the 3 numbers and print the sum.

Figure 3:



We have initialized 4 variables, the X, Y, Z and Sum. Then we ask the user to input or enter a number for X, Y and Z.



And the rest is history. Let’s try getting the average of the 3 numbers.

Problem 4:

Draw a flowchart that would input 3 numbers. Find the sum of the 3 numbers, compute for the average and print the sum and the average.

Figure 4:



In the initialization phase, we added 1 variable called Average. , at the processing, we added the computation for average (Average = Sum / 3). Then we added a print symbol that prints the average.

There. Very easy isn’t it? Its easy to draw, its easy to read, and its easy to follow. Let’s try a problem which is closer to reality.

“To be or not to be! “

Decisions. We encounter decision making almost 95% of our activities in life, what we eat, drink, do, and etc. We will also apply decision making in flowchart. We will modify the first flowchart I created in Lesson 1.

Problem 5

Given the 3 quiz grades of the student, Quiz1: 80 , Quiz2: 82, Quiz3: 78. Draw a flowchart that would compute for the average grade. Display your answer, and print “Passed” if the average grade is greater than or equal to 80, else print “Failed”.

Solution (Flowchart):





As you can see, the hexagon initializes the variables to be used in the entire flow.




We have computed for the sum of the three quiz grades and the average:



Or we can do this and still get the average:

Average = (Quiz1 + Quiz2 + Quiz3) / 3

The open and close parenthesis is used to group the 3 quizzes then divided by 3 to get the average grade. Either way we still get the result.

The diamond is where we determine if the computed average grade is greater than or equal to 80.



the flow then branches out, one on the left if it satisfies the average >= 80, else it branches at the bottom of the diamond, and prints the desired output, depending on the average grade that was computed. >= 80 prints “passed” , “failed” if <>

Problem 6

Input the Name and Gender of a person. Print the Name and “You are male” if gender is ‘M’ else print “You are female”.

Solution (Flowchart):




Again, we initialized the variables needed, but this time we get our data from the keyboard (user types in the name and gender), then test if the inputted gender is “M” for male or “F” for female. If male then we print “You are male” on the screen else “You are female”.

Are you up to solve the next problem? Let’s try:

Problem 7

Draw a flowchart that would input the name, age and gender of a person, print “Hello” if age is less than or equal to 59, else “Hi” and print also the name.

It’s very simple, just follow the previous flowchart examples.

Well that’s it for now.


-End of lesson 4 Beginners Programming

1 comment:

Anonymous said...

hello :)

I'm doing a flowchart and one command i have to input is: "The value of A divided by B is calculated and stored in C"
How shall I store the result in C?
Simply by doing an input/output box and write "Store answer in C"?
Thanks a lot for your help and also for this lesson, it was great and I kinda learned or at least understood some facts...