C Pointers

I found some great tutorials on C pointers. Attempt the tutorials below by coding the examples found within the tutorials.

https://www.programiz.com/c-programming/c-pointers

https://www.programiz.com/c-programming/c-pointers-arrays

https://www.programiz.com/c-programming/c-pointer-functions

(optional) https://www.programiz.com/c-programming/c-dynamic-memory-allocation

(optional) – https://www.programiz.com/c-programming/c-pointer-examples

Database Terminology in Context

In this post, we observe our first database created in our last tutorial and learn the terminology associated with it. See the video below and the notes that follow:

Terminology Summary

TermDefinition
RecordA single row in a database table. (also known as tuple). A record consists of 2 or more fields.  
FieldA single item of data in a record. It can be thought of as a column in a table. (also known as an attribute)
Data typeThe type of data that can be saved in a field. The database management system (DBMS) only allows the specified datatype for each field.  For example, The DBMS would not allow text data to be saved in a field that has a data type of “Date/Time”.  
Primary KeyA field that uniquely identifies each row in a table or entity. It is always present for each row, i.e. It is never allowed to be blank or empty.
Foreign keyThis is a field used to store primary key data from another table. This allows for tables to be related.  
Key (or Key field)Either a primary key or foreign key. Primary keys and foreign keys are called keys because they can be used to represent a record in a table.  
Composite Keywhen two or more keys are combined to form another.  
Composite Primary KeyWhen 2 Keys are combined to form a primary key.
Candidate KeyA key or composite key that can be reasonably assumed to uniquely identify a row in a table.  

Terminology With examples and explanation

Record – A single row in a database table. (also known as tuple). A record consists of 2 or more fields. E.g, a single row in our subject table.

Field – A single item of data in a record. It can be thought of as a  column in a table . (also known as an attribute) E.g  “First Name” in our student Table

Data type – The type of data that is allowed to be saved in a field. The database management system (DBMS) only allows the specified datatype for each field.  For example, The DBMS would not allow text data to be saved in a field that has a data type of “Date/Time”.

Primary Key  – A field that uniquely identifies each row in a table or entity. It is always present for each row, i.e It is never allowed to be blank or empty. E.g Student ID

Foreign key – This is a field used to store primary key data from another table. This allows for tables to be related. E.g Student ID in our enrolment table.

Key (or Key field) – Either a primary key or foreign key. Primary keys and foreign keys are called keys because they can be used to represent a record in a table. E.g any primary or secondary key found in the database tutorial.

Composite Key – when two or more keys are combined to form another. E.g (Student ID, Subject Code) from our Enrolment Table.

Composite Primary Key  – When 2 Keys are combined to form a primary key. For example, in our student table, we could use a students last name, first name and DOB combined to be the primary key for our table .(This is assuming that we never encounter two students with the same names and date of birth!)

Candidate Key  – A key or composite key that can be reasonably assumed to uniquely identify a row in a table. E.g (First Name, Last Name, DOB) in the student table.

© 2021  Vedesh Kungebeharry. All rights reserved. 

Introduction To Databases

Introduction

Definition: A database is a collection of related data stored in separate data structures, usually table structures. Data stored in databases contain a minimal amount of duplication.

Alternative Definition: A database is as store of data organized into related tables.

A database purpose is to store and manage information; and to be easily searchable to produce filtered information on the data by querying the database.

Facilities querying the database are provided by the Database Management System (DBMS).

For example, a school’s database can store all information about the school in different tables – Teacher information in a teacher table , Student information in a student table, subject information in a subject table.

Because the tables are related to each other , we can perform queries on the database to find:

The list of subjects for a particular student,

The number of students assigned to a subject,

The list of student assigned to a teacher,

…..etc.

Required reading

Information technology for CSEC , 3rd Edition, Howard Campbell , pages 222 to 225

Database Introduction Tutorial Video from earlier class (Thurs 7th Jan 2021)

The following content and skills were demonstrated in the class:

Skill/ContentTimestampLink
Database Definition00:30https://youtu.be/sxzuxKSK_I8?t=49
Purpose Of Databases02:17https://youtu.be/sxzuxKSK_I8?t=137
Creating a Database in Access03:39https://youtu.be/sxzuxKSK_I8?t=219
Creating and Saving tables05:25https://youtu.be/sxzuxKSK_I8?t=325
Table Structure in Design View07:34https://youtu.be/sxzuxKSK_I8?t=454
Toggling Design View08:01https://youtu.be/sxzuxKSK_I8?t=481
Primary Keys08:23https://youtu.be/sxzuxKSK_I8?t=505
Adding Table Fields (Columns)08:57https://youtu.be/sxzuxKSK_I8?t=537
Data Types: Date/Time10:28https://youtu.be/sxzuxKSK_I8?t=631
Adding and editing Data11:13https://youtu.be/sxzuxKSK_I8?t=674
Auto Number Primary Key14:29https://youtu.be/sxzuxKSK_I8?t=869
Uniqueness Of Primary Keys15:15https://youtu.be/sxzuxKSK_I8?t=915
Creating Subject table16:14https://youtu.be/sxzuxKSK_I8?t=974
Manual Primary Key17:57https://youtu.be/sxzuxKSK_I8?t=1078
Presence Check of PK19:27https://youtu.be/sxzuxKSK_I8?t=1167
PKs cannot be Duplicated20:30https://youtu.be/sxzuxKSK_I8?t=1230
Bridge Entities for Relationships and Joins22:09https://youtu.be/sxzuxKSK_I8?t=1329
Composite Primary Key23:07https://youtu.be/sxzuxKSK_I8?t=1387
Yes/No Boolean datatype25:41https://youtu.be/sxzuxKSK_I8?t=1541
Relationship Creation with Lookup Wizard27:11https://youtu.be/sxzuxKSK_I8?t=1631
Examining the lookup33:26https://youtu.be/sxzuxKSK_I8?t=2006
Creating 2nd Lookup and Relationship33:41https://youtu.be/sxzuxKSK_I8?t=2081
Linking Data to create New Information about entities39:58https://youtu.be/sxzuxKSK_I8?t=2398
Creating forms automatically from built relationships42:17https://youtu.be/sxzuxKSK_I8?t=2537
Conclusion44:02https://youtu.be/sxzuxKSK_I8?t=2642

Video

The file produced from the video above and an accompanying tutorial hando and can be found here:

https://drive.google.com/drive/folders/1RYNjWTlLJqayxCZPllZSYhmo1PoXjWrv?usp=drive_link

Updates to this post

Unknown Date: Added Class Video and link to database file

2026-05-10:

  • Added Chapters containing Skills in Video and Inserted Table.
  • Added section “Updates to this Post”
  • Removed Download Button

© 2021  Vedesh Kungebeharry. All rights reserved. 

Fillable forms – (Microsoft Word) – Live Class

Hello Students, this post contains a live capture of our class on fillable forms.

Please note:

  1. Not all form controls were demonstrated, the list box and checkbox controls were deliberately left out of the demonstration for your research and learning during homework.
  2. Homework: Information Technology for CSEC 3rd Edition , Howard Campbell – complete the tutorial found from pages 154-157 (this includes all controls that were omitted in class from part 1 above)
  3. Optional exercise – Use google forms to produce a form which captures the information from the tutorial from part 2 above.

© 2021  Vedesh Kungebeharry. All rights reserved. 

Singly Linked List

The  linked list ADT consists of nodes that are linked together by a pointer.  We can think of a node programmatically as a C Structure, Node, which contains 2 variables:

  1. A variable called value , which is used to store the data in the list. Value can be set to any data type depending on what is needed. (Note that some implementations name this variable as ‘element’)
  2. A variable called next, which is a pointer to a node.

Our linked list class contains one global variable of type node pointer, which points to the first item in the list.

The list can be thought of a queue of people in a line with name tags (data), however, in this queue, people can join at the front, back and cut in line.

Operations performed on a linked list are shown below:

OperationExplanationExample
add(value) or addLast(value)Adds the value to the end of the list.Someone joins the end of the line
addFirst(value)Adds a value to the top/front of the listSomeone joins the line from the front.
insert(value, i)Adds/inserts a value at location i counting from top/frontSomeone cuts in line at the become the ith position in the list.  If i exceeds the size of the list, the person is added to the end of the list
size()Returns the size of the listFind out the amount of people on the line
isEmpty()Returns true if the queue is empty, i.e, size==0Find out if the line is empty.
delete(i)Deletes/removes a node at location i, and returns the valueSomeone leaves the line from the ith location
Some operations that are performed on a linked list.

Some Linked List algorithms

Performing the operations above is not as straightforward as our previous ADTs.  In this discussion, we examine the algorithms for performing operations in the list.

Initialization

First<--null

addFirst

AddFirst(newValue)
{
	Create the new Node newListItem
	newListItem.value <-- newValue
	newListItem.next  <--first
	first<--newListItem
}

add last

AddLast(newValue)
{
Create the new Node newListItem
newListItem.value  newValue

If the list is empty Then
       first <-- newListItem
       newListItem.next <-- NULL
Else
	Create Nodes currentNode, previousNode
	while(currentNode!=NULL)
        {
           previousNode=currentNode;
            currentNode=currentNode->next;
       	}
        
        previousNode->next=newNode;
}

In class assignment/homework 1

The rest of the operations  on the linked list were omitted:

NumberOperation
1size()
2isEmpty()
3insert(value, i)
4delete(i)

Using to the algorithms above as a guide, write separate algorithms which implements the operation listed in the table above.

(Solution here: Solution: Singly Linked List -In class assignment/homework 1

Code for a linked list in C

//Note that the operations from the in class assignment/homework are not included.
#include <stdio.h>
#include <stdlib.h>


/*keep track of the size of the list.
Initially, the size is 0,
thus  the count is 0.*/
int count = 0;

/*create a structure called "Node_n"
and give it a type definition
named Node.
We do this to avoid using the
"struct" keyword everytime
we declare a  Node_n structure.

It's simply a quality of life improvement
*/
typedef struct Node_n
{
    int data;
    struct Node_n * next;/*note that we cant
                        use the typedef here ,
                        e.g "struct Node * next;"
                        since the type definition
                        has not been created yet.
                        if used it will produce
                        a warning when you try to
                        assign Node Structures to
                        each other such as in the
                        add function etc:
                        "assignment from incompatible
                        pointer type
                         [-Wincompatible-pointer-types]|"
                             */
} Node;



/*we declare Node structure to
represent the first item at the
start of the list.

It is assigned a mull pointer value,
which can be thought of a value of
0 for the pointer.
*/
Node * first;

/*functionDeclarations 
that were IMPLEMWNTED 
in this code*/
void addFirst(int value);
void addLast(int value);
int size();
void printList();

/*functionDeclarations 
that were NOT IMPLEMENTED 
in this code. It was left 
for student discussion  
on how this should be implemented*/

void deleteNode(int location);//for delete(i) 
                              //algorithm from above                                
void insert(int value, int location);

int main()
{
    //initialization
    first=NULL;


    //codetesting
    addFirst(2);
    addFirst(1);
    addLast(3);
    addLast(4);
    printList();

    return 0;
}

void addFirst(int value)
{
    //create new node....
    /*we use malloc
        to allocate a portion  of
        RAM memory to store the
        data contained within a node.
        The amount of memory allocated is
        the size of the node datatype.


        the newly allocated node
        Node structure's pointer is now assigned
        to  our pointer named "newNode"

        note that "sizeof" is a unary operator
        much like the address operator "&"

        sizeof only requires parentheses
        only when  it is evaluating a data type
        so :

        int x;
        printf("%d", sizeof x);

        would complile and work fine.
        */


    Node * newNode = malloc(sizeof (Node));
    /*Question: how different would it be create
    new node without a pointer and achieve the
    same result in the line above?*/

    //.....store the data in the new node
    newNode->data=value;


    /*It is best practice to check to see of the list
    is empty and not based on the integer size being
    zero. This is because most list traversal happens
    through pointers  and must be stopped when null
    is reached. If using size to check if it's empty,
    the programmer may forget to set the last link
    in the list to be null resulting in infinite
    traversal looping*/
    //if the list is empty..
    if( first==NULL)
    {

        first=newNode;
        first->next=NULL;/*set next to null,
                        indicating that
                        this is the last item
                        in the list so far*/

    }
    else// the list contains items///
    {
        //create links..
        newNode->next=first;
        first=newNode;
        //recall that the list is null terminated still.
    }
    count++;
}

/*Future exercise 3:
adde a node called last which always
stores the last item in the list.
modify addFirst to ensure that
add first is being tracked.
modify this code to omit the
traversal using the while loop*/

void addLast(int value)
{
    /*What would happen if we used node
    structures instead of pointers?
    try to implement this function without
    delaring pointers,
    i.e declare newNode,currentNode,
    previousNode as a node and not a pointer
    to a Node
    Answer: when current is set to null, we will
    not be able to dereference the null value to
    a node so that current can now bw assigned
    to a NULL node. There is no such thing
    as a null struct, only null pointers*/
    Node * newNode = malloc(sizeof(Node));;

    newNode->data = value;
    newNode->next = NULL;

    //if the list is empty
    if(first==NULL)
        first=newNode;
    else
    {
        Node * currentNode , * previousNode;

        currentNode=first;
        previousNode=first;

        /*while  the end of the list
        has not been reached*/
        while(currentNode!=NULL)
        {
            previousNode=currentNode;
            currentNode=currentNode->next;
        }
        /*loop terminates with previousNode
        being the last one in the list
        */
        previousNode->next=newNode;
    }
    count++;
}

void printList()
{
    printf("Start list...\n");
    Node * current = first;
    while (current !=NULL)
    {
        printf("%d\n", current->data);
        current=(Node *)(current->next);
    }
    printf("End list...\n");
}



int size()
{
    return count;
}

In class assignment/homework 2

The rest of the operations on the linked list  were omitted in the code:

NumberOperation
1IsEmpty()
2Insert(int value, int i)
3deleteNode(i)

Using to the Code  above as a guide, write functions which implements the operation listed in the table above. Update your main method to test the operations so that they behave as expected.

Solution here: Solution: Singly Linked List -In class assignment/homework 2

© 2020  Vedesh Kungebeharry. All rights reserved. 

Updates to this post

2024 – 09 – 21

  • changed function names from upper camel case to lower camel case.
  • Added links to solutions.

A pivot table’s intended use.

This video was captured in class to demonstrate how we manipulate pivot tables to find trends.

Here , we take a “behind the scenes approach” to understand our data , even though pivot tables are used for creating summary tables and determining trends in unfamiliar data.

This is the excel tutorial file used in the demonstration:

https://drive.google.com/file/d/1M4bU4XB8Dz0ygcDv5P8P7PnT0ARWpaUk/view?usp=sharing

© 2020  Vedesh Kungebeharry. All rights reserved. 

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.