0% found this document useful (0 votes)
2 views

CSI106_LAB02

The document outlines key concepts in computer science, focusing on Unified Modeling Language (UML) and pseudocode for algorithm definition. It includes various algorithms for tasks such as arithmetic operations, sorting, and determining properties of numbers. Additionally, it provides structured pseudocode examples for practical implementation of these algorithms.

Uploaded by

hmy101006
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSI106_LAB02

The document outlines key concepts in computer science, focusing on Unified Modeling Language (UML) and pseudocode for algorithm definition. It includes various algorithms for tasks such as arithmetic operations, sorting, and determining properties of numbers. Additionally, it provides structured pseudocode examples for practical implementation of these algorithms.

Uploaded by

hmy101006
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CSI106: Foundations Of Computer Science

Duration 90 minutes
Fullname Nguyễn Thị Huyền My
Student code SE201997
Email [email protected]

1.Unified Modeling Language (UML) is a graphical language used for analysis and
design.

Reference: APPENDIX B Unified Modeling Language (UML), page 539,


TextBook_Foundations of Computer Science_Behrouz Forouzan_2017
2.Pseudocode

One of the most common tools for defining algorithms is pseudocode.


Pseudocode is an English-like representation of the code required for an
algorithm.

Reference: APPENDIX C Pseudocode, page 553, TextBook_Foundations of


Computer Science_Behrouz Forouzan_2017

Algorithm
Purpose The purpose is a short statement about what the algorithm does.
It needs to describe only the general algorithm processing.
Pre The precondition lists any precursor requirements. For example,
we require that the list be available to the algorithm
Post The postcondition identifies any effect created by the algorithm.
For example, the algorithm may require the printing of data.
Return We believe that every algorithm should show what is returned
from the algorithm. If there is nothing to be returned, we advise
that null be specified.

LAB
Write an algorithm to allows users to input two integers and an operator of
1 UML
four operators +, -, *, / then print out the result to the monitor.
2 Write an algorithm that will find the smallest integer among five integers:
UML
12, 34, 9, 20, 7
3 Write algorithm that reads in three numbers and writes them all in sorted
UML
order
4 Write pseudocode to print all multiples of 5 between 1 and 100 (including
Pseudocode
both 1 and 100). (1, 5, 10, 15, …, 100)
5 Write an algorithm which accepts an integer N and raises it to the power of
Pseudocode
3. Print the answer.
6 Pseudocode Write an pseudocode to print y: y = 2x^2 – 6
7 Pseudocode Write an algorithm in Pseudocode for the insertion sort
8 Write Pseudocode that tells the user whether the number they enter is even
Pseudocode
or odd.
9 Write Pseudocode that performs the following:
Ask a user to enter a number.
If the number is between 0 and 10, write the word blue.
Pseudocode
If the number is between 10 and 20, write the word red.
If the number is between 20 and 30, write the word green.
If it is any other number, write that it is not a correct color option.
1 Write pseudo code that will perform the following.
0 a) Read in 5 separate numbers.
b) Calculate the average of the five numbers.
Pseudocode c) Find the smallest (minimum) and largest (maximum) of the five entered
numbers.
d) Write out the results found from steps b and c with a message describing
what they are
ANSWER
1
2
3

Algorithm : PrintMultiplesOfFive
Purpose : Print all multiples of 5 between 1 and 100.
Pre : None
: The multiples of 5 are
Post
printed.
Return: None
{
FOR number FROM 5 TO 100 STEP 5 DO
PRINT number
ENDFOR
}
4

5
Algorithm : CubeNumber (N)
Purpose : Compute and print the cube of an integer N.
Pre : Given an integer N.
Post : The cube of N is printed.
Return : None.
{
INPUT N // Accept an integer N
result ← N * N * N // Raise N to the power of 3
PRINT result // Print the result
}

Algorithm : ComputeY (x)


Purpose : Compute and print the value of y=2x2−6y = 2x^2 - 6y=2x2−6.
Pre : Given an integer or real number xxx.
Post : The computed value of yyy is printed.
Return : None.

6 {
INPUT x // Accept the value of x
y ← 2 * (x * x) - 6 // Compute y = 2x^2 – 6
PRINT y // Print the result
}

7 Algorithm : InsertionSort (array, n)


Purpose : Sort an array of size nnn using the insertion sort algorithm.
Pre : Given an array of nnn elements.
Post : The array is sorted in ascending order.
Return : The sorted array.
{
INPUT array, n // Accept the array and its size

FOR i FROM 1 TO n-1 DO

key ← array[i] // Select the current element

j←i-1

// Shift elements that are greater than key to one position ahead

WHILE j ≥ 0 AND array[j] > key DO

array[j + 1] ← array[j]

j←j-1

ENDWHILE

array[j + 1] ← key // Insert the key at the correct position

ENDFOR

PRINT array // Print the sorted array

8
Algorithm : CheckEvenOdd (N)
Purpose : Determine if a number is even or odd.
Pre : Given an integer N.
Post : Prints whether N is even or odd.
Return : None.
{
INPUT N // Accept an integer N from the user
IF N MOD 2 = 0 THEN
PRINT "The number is Even"
ELSE
PRINT "The number is Odd"
ENDIF
}

9 Algorithm : CheckColorOption (N)

Purpose : Determines and prints a color based on the input number.

Pre : Given an integer NNN.

Post : Prints "blue", "red", "green", or an error message based on the number range.

Return : None.

INPUT N // Ask the user to enter a number

IF N >= 0 AND N < 10 THEN

PRINT "blue"

ELSE IF N >= 10 AND N < 20 THEN

PRINT "red"

ELSE IF N >= 20 AND N < 30 THEN

PRINT "green"

ELSE

PRINT "Not a correct color option"

ENDIF
}

1
0

Algorithm : ProcessFiveNumbers

Purpose : Read five numbers, compute the average, find the minimum and maximum,
and display the results.
Pre : Given Five numerical inputs.
Post : Prints the average, smallest, and largest numbers.
Return :None.

{
// Step (a): Read five numbers
INPUT num1, num2, num3, num4, num5
// Step (b): Calculate the average
sum ← num1 + num2 + num3 + num4 + num5
average ← sum / 5
// Step (c): Find the smallest and largest numbers
min ← num1
max ← num1
IF num2 < min THEN
min ← num2
ENDIF
IF num3 < min THEN
min ← num3
ENDIF
IF num4 < min THEN
min ← num4
ENDIF
IF num5 < min THEN
min ← num5
ENDIF
IF num2 > max THEN
max ← num2
ENDIF
IF num3 > max THEN
max ← num3
ENDIF
IF num4 > max THEN
max ← num4
ENDIF
IF num5 > max THEN
max ← num5
ENDIF
// Step (d): Print the results
PRINT "The average of the numbers is: ", average
PRINT "The smallest number is: ", min
PRINT "The largest number is: ", max
}

You might also like