Você está na página 1de 55

4-,---' -- =-,--'

-!nput and output statements


-Evaluating Expression
!nput and output statements
!n Basic
!NPUT 8 OUTPUT STATENENTS
Write a program that inputs three
numeral values, assign them to
variables x, y, and z then prints them.
.'=-'- ,-, '=-'-- --' '+-='-= ` --= , -``

Example 1
START
END
Read X,Y,Z
Print X,Y,Z
Example 1 (cont.)
Example 1 (cont.)
REN This Program reads three numeral values then
REN print them
REN --------------------------------------------------------------
!NPUT X= ?",x
!NPUT Y= ?",y
!NPUT Z= ?",z
PR!NT X ="; x, Y ="; y, Z =";z
Program
Program
!NPUT 8 OUTPUT STATENENTS
Example 2
Write a program that inputs a string,
assign it to variable Name$, then print
it.
'+-='-= ` ,= -, .'=-'- ,-, '=-'-- --'
Example 2 (cont.)
START
END
Read Name$
Print Name$
Example 2 (cont.)
REN This Program reads a string and then
REN prints it
REN ----------------------------------------------
!NPUT What is your name? , Name$
PR!NT Hello ; Name$
END
Program
Program
Evaluating Expression !n Basic
Evaluating Expressions
Write a program that inputs name of a
student and his scores in courses X,Y,
and Z, then prints its name and average
score.
-`` _ -'=- , -''='' -' .'=-'- ,-, '=-'-- --'
-',- X,Y,Z -'=- =-,-- , --' ='-=- ,-, `
Example 1
Example 1 (cont.)
START
Read Name$
Read X,Y,Z
END
Average= (X+Y+Z)/3
Print
Name$,Average
Example 1 (cont.)
REN This Program reads scores of 3 courses then print their
REN average
REN ------------------------------------------------------------------------
!NPUT What is your name? , Name$
!NPUT What is your score in course X?", x
!NPUT What is your score in course Y?", y
!NPUT What is your score in course Z?", z
Average = (x +y +z ) f3
PR!NT Name$;"'s average score = ; Average
END
Program
Program
Evaluating Expressions
Write a program that inputs the value of kth term
(where k is to be input) of the series.
_''' -='' -, ='-=- ,-, '=-'-- --' ) .=-- --- -''' ' -,= (
,''---'' :
Example 2
Example 2 (cont.)
START
Read K
END
Calculate Tk
Print Tk
Example ` (cont.)
REN This Program calculates the Kth term of the given
REN series
REN -------------------------------------------------------------
!NPUT Enter value of K"; K
TK= (2 * K * (5 * K'2 +8)'(1f6))f(K + SQR( 7 * K ' 5))
PR!NT Tk = ";TK
END
Program
Program
Evaluating Expressions
Write a program that reads in an angle Y
in degrees, and calculate its cosine. Note
that you must convert the angle into
radian.
--=, , -'=-''- ,,' -, .'=-'- ,-, '=-'-- --'
_'' ,,''' -, .,,=- - --` -' ==` '+-'-- -,=
,-'-'' ,---''
Example 3
Example 3 (cont.)
START
Read X
END
Y=X*PI/180
Print COS(Y)
Example 3 (cont.)
REN This Program reads an angle in degrees and prints
REN its cosine
!NPUT Angle in degree = ,x
P! = ATN(1) * +
y= x * P! f180
PR!NT Angle in radian = ;y
PR!NT !ts cosine = ; COS(y)
END
Program
Program

IF statement
Looping
IF Statement in Basic
IF Statement
Example 1
Write a program that calculates the sum
of the numbers from A to B using the
formula of the sum of the arithmetic
series.
A B
.

Example 1 (cont.)
START
END
READ A,B
PRINT S
A>B
C =A
A =B
B =C
N =B A +1
S =N / 2 * ( A +B )
Y
N
Example 1 (cont.)
Program
Program
REM This program calculates the sum of the numbers from A to B
REM using the formula of the sum of the arithmetic series
REM ----------------------------------------------------------------
I NPUT The first number equals ; A
I NPUT The second number equals ; B
I F A>B THEN
C = A
A = B
B = C
END I F
N = B A + 1
S = N / 2 * ( A + B )
PRI NT Sum of numbers from; A ; to; B ; = ; S
END
IF Statement
Example 2
Write a program that computes the grade of
a student based on the percentage of his
total score (S). The grade is computed
according to the following rules:

,
:
Example 2 (cont.)
Read S
Print
pass
Print
Fail
Print
Good
Print
VGood
Print
Excellent

S>=85
START
STOP
S>=75
S>=65
S>=50
Y
Y
Y
Y
N
N
N
N
Range Grade
85 S 100 Excellent
75 S < 85 Very Good
65 S < 75 Good
50 S < 65 Pass
S < 50 Fail
Example 2 (cont.)
Program
Program
REM This program reads the percent of the total score of a
REM student and computes his/her grade
REM -------------------------------------------------------
INPUT The total score ( % ) = ; S
IF S>=85 THEN
PRINT EXCELLENT
ELSEIF S>=75 THEN
PRINT VERY GOOD
ELSEIF S>=65 THEN
PRINT GOOD
ELSEIF S>=50 THEN
PRINT PASS
ELSE
PRINT FAIL
END IF
END
IF Statement
Example 3
Write a program that calculates
the factorial of an integer N
N

Example 3 (cont.)
I =I +1
F =F * I
I =2
F =1
START
END
Read N
Print F
Print
Invalid Number
I >N
N <0
Y
Y
N
N
REM This program calculates
REM the factorial of a positive integer N
REM ----------------------------------------
INPUT The number = ; N
IF N < 0 THEN
PRINT Invalid number
GOTO 30
END IF
I = 2
F = 1
10 IF I > N THEN 20
F = F * I
I = I + 1
GOTO 10
20 PRI NT FACTORI AL = ; F
30 END
IF Statement
Example 4
Write a program that generates
the first N terms of the Fibonacci
series 1, 1, 2, 3, 5, ..
N
1 1 2 3 5 ...
Example 4 (cont.)
REM This program generates the first N
REM terms of the Fibonacci series : 1, 1,
REM 2, 3, 5, 8,..
REM ------------------------------------------
INPUT The number of terms = ; N
F1 = 1
F2 = 1
I = 1
10 IF I > N THEN 20
PRINT F1
F3 = F1 + F2
F1 = F2
F2 = F3
I = I + 1
GOTO 10
20 END
I =I +1
F3 =F1 +F2
F1 =F2
F2 =F3
I =1
F1 =1
F2 =1
START
END
Read N
Print F1
I >N
Y
N
Looping
Looping
Example 1
Write a program that calculates the
factorial of an integer N
N
Example 1 (cont.)
Program
REM This program calculates the factorial
REM of a positive integer N
REM factorial N = N * (N-1) * . * 2 * 1
REM --------------------------------------------
INPUT The number = ; N
IF N < 0 THEN
PRINT Invalid number
GOTO 10
END I F
F = 1
FOR I = N TO 1 STEP -1
F = F * I
NEXT I
PRI NT FACTORI AL = ; F
10 END
Next I
F =F * I
F =1
START
END
Read N
Print F
Print
Invalid Number
N <0
Y
N
For I =1 To N
Looping
Example 2
Write a program that generates the first
N terms of the Fibonacci series 1, 1, 2, 3,
5, ..
N
1 1 2 3 5 ...
Example 2 (cont.)
Program:
REM This program generates the first N
REM terms of the Fibonacci series :
REM FIB(N) = FIB (N-1) + FIB (N-2)
REM ----------------------------------------
INPUT The number of terms = ; N
F1 = 1
F2 = 1
I = 1
FOR I = 1 TO N
PRI NT F1
F3 = F1 + F2
F1 = F2
F2 = F3
NEXT I
END
NEXT I
F3 =F1 +F2
F1 =F2
F2 =F3
F1 =1
F2 =1
START
END
Read N
Print F1
For I =1 To N
Looping
Example 3
Write a program that reads N positive
integer values and calculates the sum of
all odd and even numbers, where N is an
input
N

N .
Example 3 (cont.)
INPUT Enter the value of N = , N
ODDSUM = 0
EVENSUM = 0
FOR I = 1 TO N
PRINT VALUE # ; I
INPUT NUM
IF ( NUM <= 0 ) THEN
PRI NT Not POSI TI VE I NTEGER!
GOTO 10
END IF
IF ( NUM MOD 2 = 0 ) THEN
EVENSUM = EVENSUM+ NUM
ELSE
ODDSUM = ODDSUM+ NUM
END IF
NEXT I
PRINT SUM OF EVEN NUMBERS = ; EVENSUM
PRINT SUM OF ODD NUMBERS = ; ODDSUM
10 END
Next I
ODDSUM =
ODDSUM +NUM
START
END
Read N
Print EVENSUM,
ODDSUM
Y
N
For I =1 To N
ODDSUM =0
EVENSUM =0
NUM mod 2 =0
Read NUM
EVENSUM =
EVENSUM +NUM
Looping
Example 4
Write a program that finds the minimum
and maximum in a list of N input values,
where N is an input

N . N .
Example 4 (cont.)
Program:
INPUT ENTER THE VALUE OF N = , N
PRINT VALUE # 1
INPUT NUM
MI N = NUM
MAX = NUM
FOR I = 2 TO N
PRINT VALUE #; I
INPUT NUM
IF ( NUM < MIN ) THEN MIN = NUM
IF ( NUM > MAX ) THEN MAX = NUM
NEXT I
PRI NT MI NI MUM NUMBER = ; MI N
PRI NT MAXI MUM NUMBER = ; MAX
END
Next I
START
END
Read N
Print MAX, MIN
Y
N
For I =2 To N
MIN =NUM
MAX =NUM
NUM <MIN
Read NUM
MIN =NUM
Read NUM
MAX =NUM
NUM >MAX
Y
N
THANK YOU
Chapter 7
Searching and Sorting Techniques
Linear Search
Example
Write a program that searches for a value X in an
unordered list A of size N. !f the value is found then
print its location in the list.
- --- ,= --' _ ,--= -, = -=-''- ,-, '=-'-- --'
'`' '--= N --=, ' --'-'' _ '+,- _-=, ,
Linear Search (Flow Chart)
For I = 1 to N
START
Read X
Read N
Read A(I)
Next I
A
Print
"Not Found"
I=1
X=A(I)
Print
"Found in location ", I
I = I+1
I > N
Y
N
Y
END
N
A
Linear Search (Code)
REM This program searches for a value X in a list A of size N
REM ---------------------------------------------------------
D!N A(100)
!NPUT "Number of elements = "; N
FOR ! = 1 TO N
PR!NT "Element #"; !;
!NPUT A(!)
NEXT !
!NPUT "Search for "; X
! = 1
10 !F X = A(!) THEN
PR!NT "Found in location "; !
END
ELSE
! = ! + 1
!F ! <= N THEN GOTO 10
END !F
PR!NT "value is not found"
END
Binary Search
Write a program that reads an ordered
ascending list and a number, and searches
for that number in the ordered list using
binary search technique.
',-='-- --- --' --'-- ,-, '=-'-- --' , -=-''- ,-, `
-'-`'' -=-'' -,=- ---'' --'-'' _ ,- =
Binary Search (Flow Chart)
START
Read N
For I = 1 To N
Read ARR(I)
Next I
Read Item
First = 1
Last = N
Print Mid
First <= Last
Mid = Int((Last+First)/2)
Item>ARR(Mid) First = Mid+1
Item<ARR(Mid) Last = Mid-1
Last = -1
Last = -1
Print "Item Not
Found"
END
N
N
N
Y
Y
Y
N
A
A
Y
Binary Search (Code)
REM This program first inputs a list of ordered items then searches
REM in this ordered list using binary search technique
REM ---------------------------------------------------------------
D!N ARR(100)
!NPUT " NUNBER OF ELENENTS = ", N
FOR ! = 1 TO N
PR!NT "ELENENT #", !, " = "
!NPUT ARR(!)
NEXT !
!NPUT " !TEN TO SEARCH FOR = ", !TEN
F!RST = 1
LAST = N
WH!LE F!RST <= LAST
N!D = !NT((LAST + F!RST) f 2)
!F (!TEN > ARR(N!D)) THEN F!RST = N!D + 1 ELSE !F (!TEN < ARR(N!D)) THEN
LAST = N!D - 1 ELSE LAST = -1
WEND
!F(LAST = -1)THEN PR!NT " !NDEX = "; N!D ELSE PR!NT " !TEN NOT FOUND"
END
Selection Sort
Example
Write a program that reads N numbers and
prints them in ascending order, using the
Selection Sort method.
'+-,-- -- '+-=,, -'-=`' - =,-=- --'-- ,-, '=-'-- --'
',-='-- '-,-- . ',-=`'- -,--'' -,= -=--' .
Selection Sort (Flow Chart)
Max = A(1)
Maxloc = 1
A
Max = A(J)
Maxloc = J
For I = N To 2 Step -1
ForJ = 2 To I
A(J) > Max
START
Read N
N
For I = 1 to N
Read A(I)
Next I
END
Next J
A(Maxloc) = A(I)
A(I) = Max
Next I
For I = 1 to N
Read A(I)
Next I
A
Selection Sort (Code)
REM A program to sort a list of N numbers using Selection Sort
REM -----------------------------------------------------------------
D!N A(100)
REM {1) Read N
!NPUT "Enter the number of elements N: ";N
REM -----------------------------------------------------------------
REN (2) Read the list in an array
For ! = 1 To N
PR!NT "A(";!;") = ";
!NPUT A(!)
NEXT !
REM -----------------------------------------------------------------
REM {3) Use the selection algorithm to sort the array.
REM We need N-1 selection passes because after N-1 selections for the REM Nth, {N-1)th, .. 2nd
REM locations, the number in the first location will have been
REM already put in its right location
FOR ! = N TO 2 STEP -1
REN !nitialize the Nax and Naxloc to A(1) and 1 respectively
Nax = A(1)
Naxloc = 1
Selection Sort (Code) Continue
REM Start the search for the maximum in the locations 2,3, . I
FOR J = 2 TO !
!F (A(J) > max) THEN
REN A(J) is greater than the max seen until now, so consider
REN A(J) the max
Nax = A(J)
Naxloc = J
END !F
NEXT J
REM Now, we swap the found Max with the number in the Ith location
A(Naxloc) = A(!)
A(!) = Nax
NEXT !
REM -----------------------------------------------------------------
REM {4) Output the sorted list
PR!NT "The sorted list is :-"
For ! = 1 To N
PR!NT "A(";!;") = "; A(!)
NEXT !
END
Bubble Sort
Example
Write a program that reads N numbers and
prints them in ascending order, using the
Bubble Sort method.
'+-,-- -- '+-=,, -'-=`' - =,-=- --'-- ,-, '=-'-- --'
',-='-- '-,-- . ='--''- -,--'' -,= -=--' .
Selection Sort (Flow Chart)
Swap A(J),A(J+1
For I = N To 2 Step -1
ForJ = 1 To I-1
A(J) > A(J+1)
Y
N
START
Read N
For I = 1 to N
Read A(I)
Next I
A
Next J
END
Next I
For I = 1 to N
Read A(I)
Next I
A
Bubble Sort (Code)
D!N A(100)
!NPUT "Enter the number of elements N: ";N
For ! = 1 To N
PR!NT "A(";!;") = ";
!NPUT A(!)
NEXT !
FOR ! = N TO 2 STEP -1
FOR J = 1 TO !-1
!F (A(J) > A(J+1)) THEN
tmp = A(J)
A(J) = A(J+1)
A(J+1) = tmp
END !F
NEXT J
NEXT !
PR!NT "The sorted list is :-"
For ! = 1 To N
PR!NT "A(";!;") = "; A(!)
NEXT !
END

Você também pode gostar