Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Algorithm study guide

The document provides an overview of fundamental computer concepts, including the definition of computers, programming languages, and algorithms. It explains the structure of algorithms, input/output instructions, basic operators, control structures like loops and switch cases, and the concept of arrays and functions. Additionally, it introduces personalized types or structures in programming, emphasizing their importance in object-oriented programming.

Uploaded by

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

Algorithm study guide

The document provides an overview of fundamental computer concepts, including the definition of computers, programming languages, and algorithms. It explains the structure of algorithms, input/output instructions, basic operators, control structures like loops and switch cases, and the concept of arrays and functions. Additionally, it introduces personalized types or structures in programming, emphasizing their importance in object-oriented programming.

Uploaded by

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

Chapter 1) Computer concepts

- Computer: a machine (CPU + memories + input/output peripherical)


- Computer = hardware + software
- Hardware
o Input devices  keyboard, mic, mouse
o Output devices  screen, printer, speaker,...
- Program: a program is a set of logical instructions that execute a planned task on a computer.
- Programmer: A person or another program that creates (or writes or codes) the instructions to be
executed by computer.
- Programming language: Like a normal language, is a set of word (or keyword), symbols (+, -, *, ^,…),
function, operation that the programmer uses to instruct the computer. Example: C#, Python, Java,…

Chapter 2) Algorithm
I. Definition
Algo is a programming language that uses our own language (English) as syntax to write instruction for the
computer.

II. Structure of an algo program


Syntax of a program:

START
Instruction 1
…2
…n
END
III. Output instruction in Algo
We will use the function DISPLAY to print information on the screen of a computer.

Syntax:

DISPLAY (info)

Detail:

- DISPLAY : a keyword, a function that print information on the screen


- Info : information to be displayed, can contain string, character, symbol, operation, number,…

Example:

START
DISPLAY (“ LASALLE COLLEGE” + nxline)
DISPLAY (“My first program” + nxline)
DISPLAY (“Hello world!”)
END

START
DISPLAY (“ IGA” + nxline)
DISPLAY (“1 Nutella is 8$” +nxline)
DISPLAY (“5 Nutella is ” + (8+5) + ” $” + nxline)
END

START
DISPLAY (“ AGE CACULATOR”+ nxline)
DISPLAY (“ - - - - - - - - - - -”+ nxline)
DISPLAY (“I am born in 2000”+ nxline)
DISPLAY (“We are in 2024”+ nxline)
DISPLAY (“So, I am ” + (2024 - 2000) + “ years old”)
END
START
DISPLAY (“ LASALLE COLLEGE”+ nxline)
DISPLAY (“ - - - - - - - - - - - - - -”+ nxline)
DISPLAY (“I am Dimitri, here are my grades”+ nxline)
DISPLAY (“ -Midterm (30%) is 80/100”+ nxline)
DISPLAY (“ -Final (45%) is 90/100”+ nxline)
DISPLAY (“ -Project (25%) is 50/100”+ nxline)
DISPLAY (“So, my average is ” + (80*30/100 + 90*45/100 + 50*25/100) + “/100” + nxline)
END
IV. Input instruction
This instruction will read (or recuperate) and store the value entered by the user in a variable.

A variable is a name of a reserved memory space that will be declared BExE the reading.

In Algo, to recuperate value (input) from the user, we will use the function READ()

Syntax:

READ (VariableName)

Detail:

- READ: is a keyword that will read and stores the user input in the variable between “( )”
- VariableName: a memory space, declared before the reading, the value will be stored in it.

Remark

- Before the use of variable, we need to declare it first.


Syntax to declare variable.
Type VariableName

Detail:

- Type: the nature of the value of the variable (character, numeric, Boolean)

Example:

Character: name
Numeric: grade, salary
Boolean: is empty.
Example of READ:

START
Character name
Numeric age
DISPLAY(“ FBI” + nxline)
DISPLAY(“Enter your name: ”)
READ(name)
DISPLAY(“Enter your age: ”)
READ(age)
DISPLAY(“Sir or Miss ” + name + “,” + nxline)
DISPLAY(“You are ” + age + “ years old” + nxline)
END
START
Character Add, Sub, Mul, Div
Numeric Val1, Val2
DISPLAY(“ CACULATOR” + nxline)
DISPLAY(“ - - - - - - - - - -” nxline)
DISPLAY(“Enter value 1: ")
READ(Val1)
DISPLAY("Enter value 2: ")
READ(Val2)
Add = Val1 + Val2
Sub = Val1 - Val2
Mul = Val1 * Val2
Div = Val1 / Val2
DISPLAY("The addition of" + Val1 +" and ” + Val2 + “ is ” + Add + nxline)
DISPLAY("The subtraction of" + Val1+ “ by ” + Val2 + “ is ” + Sub + nxline)
DISPLAY ("The multiplication of" + Val1+ " by ”+ Val2 + “ is ” + Mul + nxline)
DISPLAY (" The division of" + Val1 + “ by ” + Val2 + “ is ” + Div)
END

Chapter 3) Basic operators in Algo


The affectation operator “=”

This operator “=” allows us to assign (or affect, or stare) a variable (or the result of on instruction) in
VARIABLE

Example:

grade = 25  grade <= 25

grade = 75+20  grade <= 95


average = grade + 3  grade <= 98

Exercise

Write a group of instructions (only = instruction) that will switch (or exchange) the context of 2 variables.

Ins Val1 Val2

Val1 = 9 9 _

Val2 = 3 9 3

Use only “=” to make Val1 = 3 and Val2 = 9

Ins Val1 Val2 Val3


Val1 = 9 9 _ _
Val2 = 3 9 3 _
Val1 = Val3 9 3 9
Val1 = Val2 3 3 9
Val2 = Val3 3 9 9

The Arithmetic operators

- Addition ‘ + ’

Result = 15+5  result  20

Remark ( the ‘+’ between values of character type will acts like a concatenation (put them together) operator
result = “15” + “5”  result  result  “155”

- Subtraction ‘ - ‘

Result = 15 – 5  result  10

- Multiplication ‘ * ’

Result = 15*5  result  75

- Division ‘ / ’

Result = 5/2  result  2

Result = 5.0/2  result  2.5

( Real/Integer => Real )

( Integer/Real => Real )

- Modulo ‘ % ’ is the remainder of an integer division

Result = 5 % 2  result  1

Result = 4 % 2  result  1

CHAPTER 8
V. 4) The Switch Case
The Switch Case is an elegant alternative of the nested if where the comparison operators are only the “==”

Syntax

Switch (variable)

Case value1:

blockInstruction1;

Break;

Case value 2:

Blockinstruction2;

Break;

default

Blockinstructiondefault:

Break;

Example:

Version nested ifs

If (gender == ‘f’)

{title = “Miss”;}

Else if (gender == ‘F’)

{title = “Miss”;}

Else if (gender == ‘m’ || gender == ‘M’)

{title = “Sir”;}

Else

{title = “Sir or Miss”}


Version Switch case

.
switch (gender)
{
case 'F':
case 'f':
title = "Miss";
break;
case 'M':
case 'm':
title = "Sir";
break;
default:
title = "Miss or Sir";
break;
}

VI. The Conditional Operator ‘?:’


This operator is an efficient alternative of the if/else, because it can be inserted into other instruction.

Syntax:

(test)? Valoraction1 : valoraction2;

Details

Test = 1 or many comparison operators

? = keyword

Valoraction1 = a value that will be returned or an instruction that will be executed if the test is true.

Valoraction2 = the same than above, when the test is false.

Version 1:
max = (val1 > val2) ? val1 : val2;
Console.WriteLine(max);
Version 2:
Console.Write("the maximum is" +((val1 > val2) ? val1 : val2));

CHAPTER 9) repetitive structure in C#


VII. The do-while loop (repeat-while RW)
Syntax:
Do
{
Block2repeat;
}
While (test)
Example
Int16 nb;
Do
do
{
Console.Write("Enter your name: ");
name = Console.ReadLine();
}
while (name == "");

VIII. The while loop (while – repeat)


Syntax
While (test)
{
Block2repeat
}
Example
Single grade;
Console.Write("Enter your grade: ");
grade = Convert.ToSingle(Console.ReadLine());
while (grade < 0 || grade > 100)
{
Console.WriteLine("Please a number from 0-100: ");
grade = Convert.ToSingle(Console.ReadLine());

}
Console.WriteLine("Thanks");

IX. The FOR LOOP (FOR/REPEAT)


Syntax
For (init; test; instruct)
{
BlockToRepeat;
}
Details
Init: declairation and initialization of local variable (ex: counter)
Test: 1 or many comparison operators that reload for the loop
Instruct: additional arithmetic instruction(increment, decrement)
Examples:
Display a number (per line) from 80 to 10
By step of -20
80

60

40

20
for (Int16 number=80; number >= 10; number = number-20) ;
{
Console.Write("-value: " + number+”\n”);
}

The choice of the right loop

The three loops can be used in some situations but there is a way to find the right loop.

- If we know how many times we are looping


 Choose the FOR loop
- Else (we don’t know)
o If we need to execute the block that repeat at least once  cooose the DO/WHILE
o Else(if we can test first)  choose the WHILE

CHAPTER 10 (THE ARRAY)


I) Introduction
- An array is a fundamental date structure in programming (all languages)
- An array is a variable that will store a group (or many) of values that are the same type.
- An array is a horizontal column of data or cells, where each cell has an unique index (a number starting
from zero/0)
II) Declaration of the array
- An array is a variable, so like all variables, before using them, we need to declare them.

Syntax:

Type[] array name = new type[sign];

Detail:

Type = the type of the elements (content) of the array.

[] = keyword, this indicates that the object is an array

New = keyword, this will reserve a memory space for the array.

[sign] = a number indicating the single (or max number of elements)

Examples:

Single [] tabGrade = new Single[25];

Int16 [] tabAges = new Int16[10];

Char [] tabAlphabets = new Char[26];

String [] tabDayofTheWeek = new String[7];


III) Each element (or cell) of an array has an unique index that allows us to access this particular element
for reading or writing data in

Syntax

Arrayname[Index of the element]

Example

Single[]tabGrade = new Single[5];

- The index of the first element is always 0


- The index of the last element is (sign -1)

CHAPTER 11 Our Own Function


I) Introduction:
- Function is fundamental in programming.
- Function is a set of logical and planned instructions that describe the a specific task in a program.
- A program is a set of function
- Like a variable, we need to declare (or create) a function before its use.
II) Declaration of function

Syntax

visibility type functionName ([type listparameter])


{
Block instruction;
[return value;]
}
Details

[ ]= indicates that this part is optional

Visibility = keywords (public or private) that indicate the scope of our function.

Type = the type of a function is the type of the VALUE RETURNED by the function; if the function does not return a
value, its type will be “VOID”.

FunctionName = choose a meaningful name for your function, usually use a verb in a name.

Parameter = 0 or many objects (or variables) that the function needs from the OUTSIDE to be able to do its job or
task.

[Return value ] = will be used only if the function returns a value.

Examples:
public void DiplayTitle(string anytitle)
{
Console.WriteLine("\t" + anytitle.ToUpper());
Console.WriteLine("\t _________________");

}
private Int32 toSquare(Int32 value)
{
Int32 result;
result = value ^ 2;
return result;
}

CHAPTER 12 Personalized type or the structure


I) Introduction
- The ‘struct’ is a fundamental data structure in programming (it introduces the concept of object oriented
programming)
- The ‘struct’ is a set of information (or properties, or attributes, or characteristics) that describe one
particular entity in a program.
- After the creation (or declaration) of a ‘struct’, it becomes like a new TYPE
II) Declaration
1) Syntax
struct StructureName
{
Public type Attribute 1;
Public type Attribute 2;
Public type Attribute 3;
Public type Attribute N;
}
2) Details
Struct: keywork, indicating the creation of a new data structure type.
StructureName: please choose a meaningful name.
attribute: variable that will store the value of the property.
3) Examples
struct Date
{
Public Int16 day;
Public Int16 month;
Public Int16 year;
}
III) Initialization and use of struct

Table of comparison between a normal type Int16 and a Struct

You might also like