thumbnail

Contact

Author

Med Amine TERBAH

Med Amine TERBAH

Chapter 8 : Algorithm and its elements

Demystifying Algorithms: Unveiling the Power of Problem-Solving

Introduction:

Algorithms are the hidden heroes behind the technology that shapes our modern world. They are the underlying instructions that enable our computers, smartphones, and even everyday appliances to perform complex tasks with speed and precision. In this blog post, we embark on a journey to demystify algorithms, exploring their importance, their structure, and their real-world applications. So buckle up and get ready to dive into the fascinating realm of problem-solving!

What are Algorithms?

  • Algorithms are step-by-step instructions designed to solve specific problems.
  • They provide a systematic approach to breaking down complex tasks into manageable subtasks.
  • Algorithms exist in various domains, from mathematics and computer science to everyday life situations.

The Building Blocks of Algorithms:

  • a) Variables: Variables store data and allow us to manipulate and process information.

  • They act as the memory units of an algorithm, enabling it to perform computations and make decisions.

  • b) Control Structures: Control structures determine the flow and logic of an algorithm.

  • Conditional statements (if-else, switch) and loops (for, while) help make decisions and repeat actions.

  • c) Operations:

  • Operations are the actions performed by an algorithm to transform or manipulate data.

  • They can include arithmetic calculations, comparisons, or string manipulations, depending on the problem.

Real-World Algorithmic Applications:

  • Sorting: Algorithms like Bubble Sort or Quick Sort arrange data in a specific order.
  • Searching: Algorithms like Binary Search help locate items efficiently within a sorted list.
  • Machine Learning: Algorithms power intelligent systems, enabling them to learn patterns and make predictions.
  • Routing: Algorithms optimize navigation, finding the shortest path between two points on a map.
  • Data Compression: Algorithms like Huffman Coding reduce file sizes without losing important information.

The Sentence Analyzer Algorithm

  • Imagine you have the task of counting characters, vowels, and words in a sentence. Let's have some fun by challenging you with a problem-solving exercise!
  • Your task is to write an algorithm that reads a sentence character by character until a period (.) is entered.
  • Along the way, count the total number of characters, vowels, and words in the sentence.
  • Remember to assume that words are separated by spaces.
  • Implement the algorithm using the given variables and conditions mentioned earlier in this blog post.
  • Once you have your solution, put it to the test and see how accurately it analyzes the sentence!
ALGORITHM sentence_functions
VAR

    ch: CHAR;
    
    l : INTEGER :=0;
    w : INTEGER :=0;
    V : INTEGER :=0;
    
    letter: CHAR;
    point: CHAR:=".";
    space: CHAR:=" ";
    
    vowel_1 : CHAR:="a";
    vowel_2 : CHAR:="e";
    vowel_3 : CHAR:="i";
    vowel_4 : CHAR:="o";
    vowel_5 : CHAR:="u";
BEGIN
REPEAT
    Write("please enter a sentence character by character")
    Read(ch)
    FOR letter FROM 'a' to 'z' STEP 1   DO
        IF (ch=letter) THEN
            l := l + 1;
        END_IF
        
        IF (sen[i]=vowel_1 OR sen[i]=vowel_2 OR sen[i]=vowel_3 OR sen[i]=vowel_4 OR sen[i]=vowel_5) THEN
            V:=V+1;
        END_IF
    END_FOR
    IF (ch=space) THEN
        w:=w+1;
    END_IF

UNTIL (ch=point)
 Write("This sentence has",l,"characters");
 Write("This sentence has",V,"vowels");
 Write("This sentence has",w,"words");
END