Unit # 5
Computer Programming
class: 8th
Exercise Questions
Q4. Give brief answers to the following questions. i) What is program? program? Give few examples of programs. Ans: A set of instructions given to the computer to perform a specific task is called called a program. A computer works according to the instruction written in program. Few examples of programs are: A program to find area of a circle for a gives radius. A program to find sum and average of five values A program to find the grades of students A program to find greater of two values ii) What is the purpose of programmin programming g language? Ans: A programming programming language is designed to develop programs or or instructions to communicate communicate with computer to solve various problems. Programming languages are the means of communication between user and computer. iii) Differentiate between a constant and variable. Ans: A quantity whose value value cannot be changed changed during execution of program program is called constant. A quantity whose value can be changed during execution of program is called variable. Variables are the named memory locations that can store store data and result of processin processing. g. Variables give meaningful names to constants. iv) Differentiate between Syntax error and logical error. Ans: Syntax error is a type of error that occurs when a program statement does not follow the syntax of programming language. A type of error that occurs due to wrong logic of the program is called logical error.
v) What are the rules for defining/declaring defining/declari ng variables in GW-BASIC? Ans: Following are the rules for defining/declaring variables in GW-BASIC: Alphabets and numbers can be used for variables. The first character of the variable should be an alphabet. No special symbol i s allowed except Underscore Underscor e ( _ ). Underscore ( _ ) cannot be used as a first or last character. vi) Name different types of constants with examples. Ans: Constants are classified as string constants and numeric constants. String constants: These are series of alphabetic or alphanumeric character written in double quotation marks. For example “Pakistan” , “Haider” and “H.No.107” et c.
Numeric constants: These T hese are the numbers, for example 15 , 20.50 , -50 etc. vii) Give the precedence of the arithmetic operators. Ans: Precedence is the priority priority that is followed while applying operators in an expression. In other words the order in which operators in an expression are evaluated is known as precedence. It is also known as hierarchy of operators. The precedence of arithmetic operators is given below: Pri ori ty level level Operator Symbol Precedence
First Exponential Operator
^ Highest Second Multiplication, Divide *OR /
Third MOD Operator MOD
Fourth Addition or Subtraction + OR Lowest
viii) What is the purpose of modulus operator (MOD)? Ans: Modulus operator is used to get the remainder after division. For For example if a = 10 and b = 3 , the MOD operator ( a MOD b) will return 1 after performing division. ix)
What is the purpose of following GW-BASIC GW -BASIC commands?
1. LIST
b. RUN
c. LOAD
d. SAVE
Ans: a) LIST Command: List command is used to display all or specific statements of a program from memory mem ory on screen. The shortcut key for list command comma nd is F1. Syntax: LIST [line number][- line number] Example: LIST it displays al lines of the program. b) RUN Command: RUN command is used to execute the program that is currently loaded in
the memory. The shortcut key for RUN command is F2. Syntax: RUN Example: RUN it runs the program currently in memory. c) LOAD Command: LOAD command is used to load a saved program file from disk to
memory. The shortcut key for list command is F3. Syntax: LOAD filename Example: LOAD “Test.bas”
it loads the file “Test.bas” in memory.
d) SAVE Command: SAVE command is used store a program from memory to disk
permanently. The shortcut short cut key for list comma nd is F4. filenam e Syntax: SAVE filename Example: SAVE “Test.bas”
it stores the file “Test.bas” from memory to disk.
Q5. (i) What is arithmetic arithmeti c expression? expression? Explain different types of operators with examples. Ans: Arithmetic Expression: An expression that consists of different arithmetic operators, variables and constants is called arithmetic expression. It is evaluated by performing arithmetic operation on numeric numeric values. There are three common types of operators. operators.
Arithmetic Operators ( +, -, *, /, ^, MOD) Assignment Operator (=) Relational Operators (< , >, , =) a)
Arithmetic Operators: These are use to perform mathematical calculations like addition,
subtraction, division, multiplication and exponentiation. Their explanation with examples is as follows: Operator symbol Operation Description Example
+ Addition Gives the sum of values A+B+C, X+50, 67+45 Subtraction Gives the difference of values X-56, A-C, 80-56 * Multiplication
Gives the product of values A*B, Y*50, 45*60 / Division Gives the quotient X/Y, B/40, 125/25 ^ Exponentiation Raise the value to the power of an exponent A ^ 10, 5^ 2, A^B MOD MODULUS Gives the remainder after division 35 MOD 6, X MOD Y
b)
Assignment Operator: An operator which is used to assign a value to a variable is called
assignment operator. In programming languages “Equal to sign (=)” is used as an assignment
operator. For example
A=5 ,
X = A + 2.54 etc.
c)
Relational Operators: These are used to perform comparisons on two values. The result
of comparison is either true (non zero) or false (zero). Following is their description with examples: Operator symbol Operation Description Example
= Equal to Returns true if the values are equal and false if not A=B Not equal to Returns true if the values are not equal and false if they are equal. 57 > Greater than Returns true if the first number is greater than second and false if not 15 > 11 Less than Returns true if the first number is less than second and false if not
7<9 >= Greater than OR equal to Returns true if the first number is greater than or equal to the second and false if not (X+1) >= 7 Less than OR equal to Returns true if the first number is less than or equal to the second and false if not Y
(v) Explain the purpose of the following GW-BASIC statements with their syntax and examples. a. PRINT
b. INPUT
c. READ and DATA
d. IF-THEN-ELSE IF-THEN -ELSE
Ans: a) PRINT Statement: PRINT statement is used to display message or output to the screen. As shortcut”?” can be used for PRINT command. string/varia bles/constants nts] Syntax: line number PRINT [ string/variables/consta String: It indicates the sequence of characters that is displayed on screen. Variables: It indicates the variables whose values to be displayed on screen. Constants: It indicates the constants to be displayed on screen. For example:
10 PRINT “I Love Pakistan”
10 PRINT X, Y, Z
will print I Love Pakistan will print values of variables x, Y and Z
10 PRINT 205 , -101
will print constant values 205 and -101
b) INPUT Statement: INPUT statement is used to take input from the user during the execution
of the program. Syntax:
line number PRINT [ string; string; ] list of variables
String is used to prompt the user to enter the required value using keyboard and it is optional. List of variables variable s is used to store the entered en tered values. Example:
10
CLS
20
INPUT “Enter a number”, X
30
PRINT “Square of your number is=”, X^2
This program will get value for X from user at execution time and will print its square. c)
READ and DATA Statement: READ/DATA statement is a combination of two
statements used together when there is a need to process large number of variables with given data. READ statement defines the list of variables while DATA statement contains constant values for these variables in READ statement. Values in READ and DATA statements should be separated by commas. Syntax: line number READ list of variables separated by commas.
line number DATA list of constants separated by commas. Example:
10
READ X, Y, Z, K
20
DATA 8, 9, 13, 15
The above program will specify four variables x, y, z and k and assign them values 8, 9, 13 and 15 respectively. If values in DATA statement are more than variables in READ statement then
the extra values are ignored but if the variables in READ statement are more than the values in DATA statement then syntax error encounters i.e. “Out of Data”. d)
IF-THEN-ELSE Statement: IF-THEN-ELSE is a decision making statement, depending
upon the condition, it takes some action or changes the order of execution. It helps the computer to check whether a condition is true or false. Syntax:
IF expression/condition THEN statement(s)1 ELSE statement(s)2 stateme nt(s)2
If the expression or condition is true then Statement(s)1 will be executed otherwise Statement(s)2 will be executed. T Flowchart:
Condition Statement (s) Statement (s) F Output:
Enter your marks: 65 You have passed Example:
10
CLS
20
INPUT “Enter your marks:”; marks
30
IF marks >= 40 THEN PRINT “ You have passed”
40
END
Short Questions other than exercise
i)
Explain the use of semicolon with PRINT.
Ans: Two or more variables can be separated in print statement by semicolon. If semicolon is used the second value appears right after the first value. There is no space between values. Example: PRINT “Apple” ; “Mango” ; “Orange” Output will be as: AppleMangoOrange ii)
Explain the use of comma with PRINT.
Ans: Two or more variables can be separated separated in print statement by comma. If comma com ma is used each value is displayed in 14 spaces. A maximum of five values can be printed in single line using commas. Example: PRINT “Apple” , “Mango” , “Orange” Output will be as: Apple iii)
Mango
Orange
What is expression? What are its types?
Ans: An expression is a combination of symbols (operands) and operators operators that represent a value. There are two types of expressions: Arithmetic expression uses arithmetic operators to perform arithmetic calculations. Relational expression uses relational operators to compare two values. iv)
Define programming.
Ans: Programming is a technique to develop programs. Different programming languages are used to write programs. Simply computer programming is all about developing, implementing and maintaining the programs. v)
How arithmetic arithm etic expression can be assigned to a variable?
Ans: An arithmetic arithmeti c expression can be assigned to a variable by by using using assignment assig nment operator (=). The variable is written on the left side of assignment operator and expression is written on right side of assignment operator. First expression is evaluated and result is stored in variable e.g. a = c – d * 10 vi)
Differentiate Differentiat e between bug and debugging.
Ans: An error in a program is called a bug. The process of finding and removing errors in a program is called debugging. de bugging. Errors can be found by testing t esting the program. vii)
Differentiate Diff erentiate between save and load command.
Ans: SAVE command is used to store a program from memory to the disk permanently whereas load command is used to load a stored program from disk to memory. viii)
How the commands are differ from statements?
Ans: Commands are key words which are used to issue instructions to the computer to perform specific task. They do not need any line number while statements are instructions for the computer program to perform an action. Each statement is started with a line number. Statements are executed according to line number in ascending order. ix) Ans:
Differentiate Differentiat e between string constant and numeric constant. The string string constant may consist consist of alphanumeric alphanumeri c or numeric numeric data data and is written in double quotation marks while numeric constant consists of numeric data i.e. only numbers
and is written without quotation marks. x)
Why variables are used with INPUT statement?
Ans: The variables are used with INPUT statement to store the input data. Two or more variables must be separated by comma. xi)
Write the use of $ in GW-BASIC.
Ans: A dollar sign $ is used after a string variable name such as A$ and Address$. The dollar sign indicates that the variable contains a string value. xii)
What is the use of arithmetic arithmet ic operators?
Ans: Arithmetic operators are used to perform perform arithmetic calculations like addition, subtraction, subtracti on, division, multiplication and exponentiation. xiii)
What is the use and syntax of CLS command?
Ans: CLS Command: CLS stands for Clear Screen. It is used to clear the contents of the screen. Syntax:
xiv)
CLS
Define precedence.
Ans: Precedence is the priority that is followed while applying operators in an expression. In other words the order in which operators in an expression are evaluated is known as precedence. It is also known as hierarchy of operators. xv)
Define the syntax of programming language.
Ans: The syntax of programming language is the set of rules that defines the combination of symbols used by that language xvi)
What is “Out of Data” error?
Ans: If the variables in READ statement are more than the values in DATA statement then syntax error encounters i.e. “Out of Data”
Unit # 4
Problem Solving
Exercise Questions Q4. Give brief answers to the following questions.
i)
What are the key features of a problem statement?
Ans: A problem statement should have the following features: Clarity and precision Identification of what would be studied Identification of key factors or variables Identification of key concepts and terms ii)
What is flowchart?
Ans: Flowchart is a graphical representation of the solution of a problem. Standard symbols are used to represent different steps of solution. These symbols are linked together with arrows. These arrows show the flow of steps performed to solve a problem. iii)
What are the uses of flowchart?
Ans: Flowchart helps in finding the solution of a problem and facilitates in showing the input, process and output of the problem. pr oblem. It is helpful h elpful in understanding understan ding the logic of the problem. probl em. iv)
What are the advantages of drawing flowchart?
Ans: Flowchart is a helpful helpful tool in solving a problem. Different benefits of flowchart are as follows: It helps to understand the logic of the problem solving process. It provides graphical representation of the process to find the solution of a problem. It displays the flow of process in a logical order.
v)
What is the use of decision symbol?
Ans: Diamond symbol is used to represent decision step in the flowchart. flowchart. A condition is given in the diamond symbol. The flow of control from diamond symbol may go in two possible directions. It goes to one direction if the condition is true and in other if it is false. Q5. Give detailed answers to the following questions.
i)
Explain the elements of a problem statement.
Ans: Different elements of a problem statement are as follows: What is given - the input The processing requirements What is required – the Output 1. 1.
What is given - the input: Input is the raw material given to the computer to solve
a problem. It is also called data or raw facts. The problem statement defines the inputs required to solve a problem. The correct input must be given in order to get the correct output. Problem: Preparing Tea:
The solution of above problem requires different input material such as electric kettle, tea bags, water, milk and sugar 1. The Processing Requirements: It includes the operations and actions performed on input to get desired results. The problem statement defines the processing requirements for the solution of a problem. Problem: Preparing Tea:
The processing requirements for this problem are as follows: Boiling the water in the kettle
Adding sugar, tea bags and milk 1. 3.
What is required – required – the the Output:
The problem statement defines the required output. Output is the required result that is obtained after processing. It is solution of a problem. Problem: Preparing Tea:
The output of this problem is the prepared tea that is served in the cup. 1. What is flowcharting? How flowcharts help in problem solving? Ans: Flowcharting is a process of creating flowcharts for solving a problem. It is a tool to analyze different processes. It can be used in different fields such as programming, engineering and science etc. Flowchart: Flowchart is a graphical representation of the solution of a problem. Standard symbols are used to represent different steps of solution. These symbols are linked together with arrows. These arrows show the flow of steps performed to solve a problem. Benefits of Flowchart: Flowchart is a helpful tool in solving a problem. Different benefits of
flowchart are as follows: It helps to understand the logic of the problem solving process. It provides graphical representation of the process to find the solution of a problem. It displays the flow of process in a logical order. 1. Explain different symbols used to draw flowcharts with examples. Ans: Flowchart Symbols The standard symbols are used to draw a flowchart. The commonly used standard symbols to draw flowchart are as follows.
1. 1.
Start/ Terminal
Oval is used to represent the start and end of the flowchart. It is also called terminal. Example: the following example displays the start and end steps in the flowchart.
Start End 1. 2.
Input/ Output
Parallelogram is used to represent an input or output step in the flowchart. Example : the following example displays the input and output steps in the flowchart.
Input M Display M
1. 3.
Process
Rectangle is used to represent a processing or computational operation in the flowchart. Example: the following example displays the processing steps in the flowchart.
Sum = a + b 4.
Flow lines
Arrows are used to represent the direction of flow in the flowchart. There are four flow lines to represent four directions.
1. 5.
Decision
Diamond symbol is used to represent decision steps in the flow chart. A condition is given in the diamond. The flow of control from diamond symbol may go in two possible directions. It goes to one direction if the condition true or yes and to second direction if condition is false or no. No Yes Is n>5 ? Example: the following example displays the decision step in the flowchart. Short Questions Other than Exercise
1. Define a problem. Ans: A problem is an obstacle, hurdle, difficulty or challenge or any situation that needs to be solved. 2. Define problem solving. Ans: Problem solving is a process to solve different problems. The goal of problem solving is to get the desired result in a short and perfect manner. 3. Name the steps involved involv ed to solve problem in computer. co mputer. Ans: Problem solving includes the following steps: 1.
i.
Identification of problem
2.
ii.
Specify requirements requir ements
3.
Iii.
Analyze the problem
4.
iv.
Design algorithm & draw flowchart
5.
v.
Write the program / Coding
6.
vi.
Test and debug the program
7.
vii.
Implement the program
8.
viii.
Maintain and update the program
9.
ix.
Documentation
10. Differentiate between problem and problem statement. Ans: A problem is an obstacle, hurdle, difficulty or challenge or any situation that needs to be solved, while a problem statement is concise description of the problem to be solved. 4. Write any three guidelines for flowcharting. Ans: Following guidelines should be followed while drawing flowchart: Flowchart should be clear, neat and easy to follow. Flowchart is either from left to right or top to bottom. Only one flow line should enter and come out of the process symbol. Only one flow line should enter decision symbol but two flow lines could come out of the decision symbol, one for each possible answer. 5. Draw process symbol. Why process symbol is used in flowchart? Ans: Rectangle is used to represent a processing or computational operation in the flowchart.
6. What is the purpose of parallelogram and rectangle symbol in flowchart? Ans: Parallelogram is used to represent an input or output step in the flowchart.
7. Which is the terminal symbol in flowchart? Ans: Oval is used to represent the start and end of the flowchart. It is also called terminal.
8. What is the goal of problem solving? Ans: the goal of problem solving is to get the desired result in a short and perfect manner. 9. What are the limitations of flow chart? Ans Flowchart has following limitations: It is difficult to draw a flowchart for a complex problem. If alterations are required, flowchart is to be redrawn. 10. Write the elements of problem statement. Ans: Different elements of a problem statement are as follows: What is given - the input The processing requirements What is required – the Output 11. How you can make flowchart more effective? Ans: Avoid the intersection of flow lines, while drawing a flowchart, to make it more effective. 12. How flowchart symbols are connected? Ans: Lines with arrow heads are used to connect symbols and show the flow of logic and data in a flowchart.