C Pointers – Live Demo

This post contains a live class demo of pointers. The prerequisites for this class is shown below the video.

Pre-requsites

Attempt the tutorials found here:

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

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

Video from today’s class (Thurs 7th Jan 2021)

The file produced from the video above can be found here:

https://drive.google.com/file/d/1fLPNIhxWgoFnlqIop7elm9Ph9jY-CH1s/view?usp=sharing

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

© 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.