Mail Merge Class Demonstration And Revision

In this class, we reviewed how to accomplish a mail merge , an automatically generated table of contents and how to password protect our document.

See the video below:

Reopening/Editing the existing data source.

A question arose from how we can return to the data source after its creation and use. How can we reopen it and add new records?

See the Gif below:

© 2020  Vedesh Kungebeharry. All rights reserved. 

Binary Search Implementation (with animated code)

This shows  a binary search for 1 then 39 then 24. See here: Binary Search Animation (Animated C code)

Introduction

In a binary search, an ordered list is searched for a key starting with the item at the middle of the list. If the item is not found at the location ,  a determination is made on which half of the list may contain the key, either the upper or lower half. This method is applied to search the midpoint of the half of the list which may contain the key.  This repeats until the key is found, or until the search space is reduced to a single location and  the list can no longer be divided into halves. If the key is found, the location of the element is returned, otherwise a dummy value is returned (e.g -1)

This method of searching fall in the category of algorithms called “Divide and Conquer” since a large portion of the search space is split and discarded on each iteration.

Table of Contents

  1. Introduction
  2. Binary Search Algorithm
  3. Implementation in C code
  4. Alternate Implementation In C using Recursion
  5. Updates

Binary Search Algorithm

Given a list of items of a finite size and a Key we traverse the list to find the key and output the location of the key found in the list.

Consider the scenario where we have a list, with unique items (in an array) and we wish to find a key:

Algorithm:
//initialization
//Assume our data is stored in an  array named  list of size N.
list[0..N-1] = {Assume a sorted list}
first=1
last=N-1
result=-1 //used to store the index of a successful search. If result is -1 , the search has failed
middle = (first-last)/2
while (first<=last)
	if list[middle]==key then
		result= middle
break
	else if key<list[middle] then
		last=middle-1;
	        else
		first=middle+1;
	        endif
	middle=(first-last)/2 + first
endif
endwhile
return result.

Implementation in C code

/*
Demo: Searching a sorted list using 
binary search
*/

#include <stdio.h>
#include <stdlib.h>

int binarySearch(int key, int  list[], int listSize);

int main()
{
    int list[10];
    list[0]=5;
    list[1]=10;
    list[2]=15;
    list[3]=20;
    list[4]=25;
    list[5]=30;
    list[6]=35;
    list[7]=40;
    list[8]=45;
    list[9]=50;

    int key;

    printf("\nenter a search key\n");
    scanf("%d",&key);
    while (key!=-1)
    {
        int result = binarySearch(key,list,10);
        printf("\nSearch result %d\n",result);
        printf("\nenter a search key\n");
        scanf("%d",&key);
    }

    return 0;
}

int binarySearch(int key, int  list[], int listSize)
{
    /*we use first and last to represent a 
    portion of the array. Initially, first and last
    points to the start of the array and the end of 
    the array respectively. Middle indexes the location
     to the middle of the array.
    */
    int result=-1;//-1 represents a failed search
    int first=0;
    int last=listSize-1;
    int middle = (last-first)/2;

    while (first<=last)//while first and last have not crossed...
    {
        if(key==list[middle])//if the key is found....
        {
            result=middle;
            return result;//return the current index and exit function
        }
        else//the key was not found
        {
            if(key<list[middle])//if the key would be found 
                                //from first to middle
                last=middle-1;//we now search the lower half
            else //key would be found from middle to last
                first=middle+1;//we now search the upper half
            middle=((last-first)/2)+first;//find the new midpoint.
        }
    }
    return result;
}

Alternate Implementation In C using Recursion

/*
Demo: Searching a sorted list using
binary search implemented using recursion
*/

#include <stdio.h>
#include <stdlib.h>

int binarySearch(int key, int  list[], int first, int last);

int main()
{
    int list[10];
    list[0]=5;
    list[1]=10;
    list[2]=15;
    list[3]=20;
    list[4]=25;
    list[5]=30;
    list[6]=35;
    list[7]=40;
    list[8]=45;
    list[9]=50;

    int key;

    printf("\nenter a search key\n");
    scanf("%d",&key);
    while (key!=-1)
    {
        int result = binarySearch(key,list,0,9);
        printf("\nSearch result %d\n",result);
        printf("\nenter a search key\n");
        scanf("%d",&key);
    }

    return 0;
}

int binarySearch(int key, int  list[], int first, int last)
{
    /*we use first and last to represent a
    portion of the array. Initially, first and last
    points to the start of the array and the end of
    the array respectively. Middle indexes the location
     to the middle of the array.
    */
    int result=-1;//-1 represents a failed search
    int middle = (last-first)/2+first;

    if (first<=last)//if first and last have not crossed...
    {
        if(key==list[middle])//if the key is found....
        {
            result=middle;
            return result;//return the current index and exit function
        }
        else//the key was not found
        {
            if(key<list[middle])//if the key would be found
            {                   //from first to middle

               //we now search the lower half
                result= binarySearch(key,list,first,middle-1);
                return result;
            }

            else //key would be found from middle to last
            {
                result= binarySearch(key,list,middle+1,last);//we now search the upper half
                return result;
            }

        }
    }
    return result;
}

Updates

2023/12/26

Added animated image and link to its source post.

2023/12/17

Fixed binary search algorithm , the element at middle was included every time then a partition made:

else if key<list[middle] then
		last=middle;
	        else
		first=middle;
	        endif
	middle=(first-last)/2 + first

It was changed to:

else if key<list[middle] then
		last=middle-1;
	        else
		first=middle+1;
	        endif
	middle=(first-last)/2 + first

Added a paragraph on Divide and Conquer

2022/2/3

Added introduction paragraph “In a binary … value is returned (e.g -1)

Added heading “Binary Search Algorithm”

© 2020  Vedesh Kungebeharry. All rights reserved. 

Selection Statements: Practise Exercise(s)

For these flowchart solutions, you may opt to:

  1. Draw the solution in your notebook OR
  2. Draw the solution in a word processing document OR
  3. Draw the solution in an online tool OR
  4. Create a solution using flowgorithm.

Exercise 1

Create a flowchart solution which prompts the user to enter a student’s math mark and outputs “You have passed” if the mark is 50 and/or greater or “You have failed” if the mark is less than 50.

Exercise 2

Create a flowchart solution which prompts the user to enter two numbers, A and B, and outputs the larger number.


© 2020  Vedesh Kungebeharry. All rights reserved. 

Videos: Search/Replace, Tables, Columns

These video(s) were chosen for each topic because they cover the subject matter in a relevant context. They definitely were not created specifically for the CSEC Syllabus, however , they do address the fundamental concepts in real world scenarios and classroom context.

 3. use appropriate editing features to structure and organize a document; Drag and drop editing: perform block operations on selected areas of text within a document.

Use search and replace functions appropriately to edit a document.

Use of tables, table styles, shading, borders, row and column insertion, split cells, split tables, text direction and cell margins, cell size.

Use of columns (one, two, three, left and right columns, column breaks).
        https://www.youtube.com/watch?v=8ZSlu4DWJ5k    

https://www.youtube.com/watch?v=XNBrCEgzddw    

    https://www.youtube.com/watch?v=X1n2VG1yxFs  

© 2020  Vedesh Kungebeharry. All rights reserved. 

Serial, Sequential, Direct (Random) Access

In this class we seek to understand the concept of logical storage for computer files stored on a physical medium.

Even though all data is stored as a sequence of zeros and ones not this binary data; we learned that this data can have some logical organization on the physical access medium.

Serial access is where data can only be accessed in the order that it was written. This is usually because of the inherent nature of the storage medium for example a magnetic tape.

Sequential access is a form of Serial access, when we organize our data in some order before storing the Data on the storage medium.

Direct access is where the location of the data on the storage medium is known beforehand, thus accessing a logical file on a direct access medium does not require accessing any other data but that specific file.

below is a video which demonstrates an analogy for data storage for direct access serial access and sequential access:

© 2020  Vedesh Kungebeharry. All rights reserved. 

Closure on Sequence with flow charts

Exercise 1 :

Modify previous example to find the average of four numbers. The previous example can be found below:

https://islandclass.wordpress.com/2020/11/02/flowchart-practise-finding-the-average-of-3-numbers/

Exercise 2:

Create a program which Prompts the user to enter a the radius of a circle and outputs the circumference and diameter.

Hint:

IPO Table

InputProcessing Output
radiusCircumference <– 2* Pi * radius
Area <– Pi * r * r
Circumference
Diameter
IPO Table

Exercise 3

Create a solution which prompts the user to enter the length and breadth of a rectangle and output the area.

Hint:

InputProcessingOutput
length, breadtharea<– length* breadtharea

© 2020  Vedesh Kungebeharry. All rights reserved. 

Videos: Data Validation And Verification

These video(s) were chosen for each topic because they cover the subject matter in a relevant context. They definitely were not created specifically for the CSEC Syllabus, however , they do address the fundamental concepts in real world scenarios and classroom context.

 differentiate  between  validation  and
verification of data;
 Difference between validation and
verification. 
 https://www.youtube.com/watch?v=wNMAtc_PzuI
 14. identify appropriate validation and verification checks given a particular scenario; and, Methods of validation: range check, reasonableness checks, data type checks, consistency checks, presence, format and length.

Methods of verification: double entry and proofreading (to identify and correct typographical and transpositional errors).
 https://www.youtube.com/watch?v=OeHe61pT9CI  

More Detailed:  https://www.youtube.com/watch?v=iS9tqYuVQ08

© 2020  Vedesh Kungebeharry. All rights reserved. 

Bubble Sort Implementation & Demonstration

Bubble Sort (Ascending)

  1. In this algorithm we compare  the first two adjacent items in the  list. If the first item is larger than the item next to it, we swap them, else we do nothing.
  2. This process (step1) is repeated until we have reached the end of the list.  At this point, the largest item is at the end of the list, we say it was “bubbled” up the list.
  3. Repeat step 1 and 2 for items 1 .. n-1, then 1 .. n-2 and so on until we reach 1..2

It seems difficult to understand but we can observe the algorithm in action in the following flowchart:

You can download the file here:

https://drive.google.com/file/d/1RvZ-dBvbHf0icL083d7vSnnce3_XDyyj/

Demonstration

See : https://youtu.be/KgqM6sC5Q4M

Exercise

Implement the algorithm in c with the following modifications:

  • The array stores double data
  • Print the array to the screen as a comma separated list before sorting.
  • Print the array to the screen as a comma separated list after sorting.

Challenge

Create a function with the following functions declaration which will sort any double array using bubble sort:

            void bubbleSort(double array[], int sizeOfArray);

© 2020  Vedesh Kungebeharry. All rights reserved. 

Hyperlinks

Definition: A hyperlink is actionable text or an object found in a hyper text document which navigates the user to the start of another hypertext document or a specific location within a hypertext document. An action could be from a gui : a click or a tap; or using the keyboard to press the “enter key” on a selected link.

Examples:

This is a hyperlink to wikipedia.org

This is a hyperlink to wikipedia.org opened in a new tab or browser.

This is a link to ……..

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

The Bottom Of The Page

This is a link to the top of the page

That is all!

© 2020  Vedesh Kungebeharry. All rights reserved. 

Flowchart Practise – Finding the average of 3 numbers.

Task

Create a flowchart algorithm which prompts the user to enter 3 numbers and outputs the average of the numbers.

Guided Solution

First, let us use an IPO Table:

InputProcessing Output
Three Numbers num1, num2, num3average=(num1+num2+num3)/3average

After considering what needs to be done, we see that we need 4 variables

Solution in flowgorithm:

Download the solution here

© 2020  Vedesh Kungebeharry. All rights reserved.