close

Study session 2

1. chapter: Basic algorithms

Pseudocode

1. chapter: Basic algorithms

(Not very cohesive...)

Why to learn?

We don't code data structures that much, but we need to understand what they are.

We need to understand pros and cons about data structures.

That same applies also for algorithms

Definitions

Chapter has some kind (!) of definitions for both.

Read definitions about algorithms and datastructures (remember: they are abstract!)

Build your own definition, use short definition and some concrete examples

How we do algorithms

No clear path to good algorithm

Experience helps (do/try it yourself)

Efficient use of simple building blocks

It can take a lot of time

If difficult enough, think on paper/whiteboard first

Main ideas of chapter

C# basics

Pseudocode

Pseudocode

More like normal text without all code syntax

There is no "official" pseudocode syntax

Focuses logic and no need to know language syntax

Practice

1. We can write some C# code to pseudocode

2. We can write some pseudocode to C# code

static int FindMax(int[] array)
{
    int max = array[0];
    foreach (int num in array)
    {
        if (num > max)
        {
            max = num;
        }
    }
    return max;
}
int FindMax(array)
    max = array[0]
    foreach num in array
        if num > max
            max = num
    return max
static double CalculateAverage(int[] array)
{
    int sum = 0;
    foreach (int num in array)
    {
        sum += num;
    }
    double average = (double)sum / array.Length;
    return average;
}
    
double CalculateAverage(array)
    sum = 0
    foreach num in array
        sum += num
    average = sum / array length
    return average

Reverse array

reverseArray(array)
    start = 0
    end = array length - 1
    while start < end
        temp = array[start]
        array[start] = array[end]
        array[end] = temp
        start++
        end--
        
static void ReverseArray(int[] array)
{
    int start = 0;
    int end = array.Length - 1;
    while (start < end)
    {
        int temp = array[start];
        array[start] = array[end];
        array[end] = temp;
        start++;
        end--;
    }
}
        
bool IsPrime(number)
    if number < 2
        return false
    for 2 to Math.Sqrt(number)
        if number % i == 0
            return false
    return true
        
static bool IsPrime(int number)
{
    if (number < 2)
    {
        return false;
    }
    for (int i = 2; i <= Math.Sqrt(number); i++)
    {
        if (number % i == 0)
        {
                return false;
        }
    }
    return true;
}
        

Extra exercises:

Write some of your code from previous day (or other C# code/algorithms) to pseudocode

Start planning course material exercises with pseudocode rather than C# code