You are on page 1of 51

8.

Programming
Array
An array is a data structure, which can store a
fixed-size collection of elements of the same data
type. An array is used to store a collection of data,
but it is often more useful to think of an array as a
collection of variables of the same type.
One- and two-dimensional arrays

A single row pill organiser represents a one-dimensional (1D) array, whereas a


pill organiser with multiple rows represents a two-dimensional (2D) array.
REMINDERS!

#include <iostream>
#define print std::cout
#define input std::cin

int main() {

return 0;
}
1. Declaring one-dimensional arrays
int a[3];

a[0]=5;
a[1]=10;
a[2]=15;

print<<a[0]<<"\n";
print<<a[1]<<"\n";
print<<a[2]<<"\n";
2. Declaring one-dimensional arrays
char b[2];

b[0]='A';
b[1]='B';

print<<b[0]<<b[1];
3. Declaring one-dimensional arrays
char* name;

name={"Silvia"};

print<<name<<"\n";
print<<name[0]<<"\n";
print<<name[5]<<"\n";
4. Declaring one-dimensional arrays

char* name[4] = {"Allan","Mike","Jason","Arnold"};

for(int a=0;a<4;a++) {
print<<name[a]<<"\n";
}
Reading from and writing into arrays
int a[100];
int v,n;

print<<"Enter the number of values you want to insert: ";


input>>v;
for(int b=0;b<v;b++){
print<<"Enter a number: ";
input>>n;
a[b]=n;
}
print<<"Your last number is "<<a[v-1];
PROBLEM SOLVING
1. Write a program to store the first 7 even numbers using
an array and then print those numbers using for loop.

a 2 4 6 8 10 12 14
PROBLEM SOLVING
1. Write a program to find the sum and average of one
dimensional integer array.
Enter number of elements you want to insert 5
Enter element 1:2
Enter element 2:2
Enter element 3:2
Enter element 4:2
Enter element 5:2
The sum of Array is :10
The average of Array is :2
int Arr[100],n,i,sum=0;
print<<"Enter number of elements you want to insert ";
input>>n;
for(i=0;i<n;i++)
{
print<<"Enter element "<<i+1<<":";
input>>Arr[i];
}
for(i=0;i<n;i++)
sum=sum+Arr[i];
print<<"\nThe sum of Array is :"<<sum;
print<<"\nThe average of Array is :"<<sum/i;
2D arrays
2D array can be defined as an array of arrays.
The 2D array is organized as matrices which can
be represented as the collection of rows and
columns.
1. Declaring two-dimensional arrays
int a[2][2];

a[0][0]=6;
a[0][1]=8;
a[1][0]=10;
a[1][1]=12;

print<<"SUM: "<<a[0][0]+a[0][1]+a[1][0]+a[1][1];
2. Declaring two-dimensional arrays
int a[2][2];
int sum=0;

a[0][0]=6;
a[0][1]=8;
a[1][0]=10;
a[1][1]=12;

for(int b=0;b<2;b++){
for(int c=0;c<2;c++){
sum = sum + a[b][c];
}
}
print<<"SUM: "<<sum;
Reading from and writing into arrays
int number[2][2];
for(int b=0;b<2;b++){
for(int c=0;c<2;c++){
print<<"Enter a number: ";
input>>number[b][c];
}
}
print<<"Your numbers are ";
for(int b=0;b<2;b++){
for(int c=0;c<2;c++){
print<<" "<<number[b][c];
}
}
1. String
#include <iostream>
#define print std::cout
#define input std::cin
#define string std::string

int main()
{

string name[5];
for(int a=0;a<5;a++){
print<<"Enter a name: ";
input>>name[a];
}

print<<"Hello! ";
for(int a=0;a<5;a++){
print<<", "<<name[a];
}
return 0;
}
2. String
#include <iostream>
#define print std::cout
#define input std::cin
#define string std::string

int main()
{

string data[5][2];
for(int a=0;a<5;a++){
print<<"Name and age please: \n";
for(int b=0;b<2;b++){
input>>data[a][b];
}
}
print<<"______________________\n";
print<<" NAME\t\tAGE\n";
print<<"______________________\n";
for(int a=0;a<5;a++){
for(int b=0;b<2;b++){
print<<data[a][b]<<"\t\t";
}
print<<"\n";
}
return 0;
}
9. Database structure
Databases
A database is a collection of data that is organised in a way
that makes it easy to store, retrieve and manipulate the data.
It can either be an electronic or a traditional paper-based
database.

Databases are essential tools that


organisations, companies and
other institutions use to store data
about their employees, students,
products, sales etc.
Single-table databases
There are two types of databases:
Relational databases and single-table databases (also known
as flat-file databases). A relational database is made up of two
or more tables joined together using links known as
relationships, but a single-table database, as the name
suggests, is a database that has only one table.

A table is a collection of rows and columns. The rows, except the first row,
represent the records, and the columns represent the fields, or attributes.
A good example of a single-table database is a spreadsheet.
Fields/columns
A field is a space for a particular piece of information about an entity. An
entity can be anything about which data is collected, such as a person, a
pen, a book etc.
Fields for the ‘book’ entity
Records/rows
This is a group of fields related to an entity. For example, all
the fields about a particular book are collectively known as a
record. The records in a database are shown as rows. When
you count the number of records in a database, you do not
include the column headings in the first row.
Data types, variables and constants
A database holds data in specific formats. Each field
in a table must have an associated format that tells the
computer how the data it contains will be used. This
is referred to as the data type of a field.

It is the responsibility of the person designing or managing


the database to define the data types for each field in a table
at the time when the data dictionary is first defined. A data
dictionary is a table that shows the name of each field to be
used in a database table, its data type.
Primary keys
A primary key is a field that uniquely identifies one record
from all others in a database table.
Additional keys can also be used, such as foreign keys,
which are used when creating relationships between tables.
A foreign key is a key that is a primary key in one table, but
just a normal field in different table. It is used to create a
relationship between two tables in a relational database.
Consider the database table shown below. Which field do you think would be
best to act as the primary key and why?
Database manipulation
There are several ways that stored
data can be retrieved and presented
to the users. For example, data can
be presented as graphs, charts, lists
or tables, and data can be stored and
displayed as text, images, sound,
videos, etc. But the way that data is
found on a database is through
something called a query.
Structured query language
When data is stored in a database,
there must be a way of accessing it
and using it efficiently. There are
different ways data can be accessed
or retrieved from a database. The
most common method is to use a
query, and this is normally done
using a standard database language
called structured query language
(SQL).
SQL is a database manipulation language that is used
specifically to communicate with a database. It can be used
to perform operations on the database, including adding
records, deleting records, updating records, etc.
SQL DROP TABLE Statement
The DROP TABLE statement is used to drop an existing
table in a database.

DROP TABLE table_name;


The SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a
new table in a database.

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);
The SQL CREATE TABLE with Primary Key

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
SQL INSERT INTO Statement
The INSERT INTO statement is used to insert
new records in a table.

INSERT INTO table_name


VALUES (value1, value2, value3, ...);
Employee table
ID Name Position Age DOB Salary
SQL UPDATE Statement
The UPDATE statement is used to modify the
existing records in a table.

UPDATE table_name
SET column1 = value1, column2 = value2
, ...
WHERE condition;
SQL DELETE Statement
The DELETE statement is used to delete existing
records in a table.

DELETE FROM table_name WHERE condition


;
1. SQL SELECT Statement
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the
result-set.

SELECT column1, column2, ...


FROM table_name;

SELECT * FROM table_name;


2. SQL SELECT Statement
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the
result-set.

SELECT * FROM table_name


WHERE condition;
SELECT * FROM table_name
WHERE condition AND condition;
SELECT * FROM table_name
WHERE condition OR condition;
SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in
ascending or descending order.
The ORDER BY keyword sorts the records in ascending order
by default. To sort the records in descending order, use
the DESC keyword.

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2,
... ASC|DESC;
1. SQL COUNT(), AVG() and SUM() Functions
The COUNT() function returns the number of
rows that matches a specified criterion.

SELECT COUNT(column_name)
FROM table_name
WHERE condition;
2. SQL COUNT(), AVG() and SUM() Functions
The AVG() function returns the average value of a
numeric column.

SELECT AVG(column_name)
FROM table_name
WHERE condition;
3. SQL COUNT(), AVG() and SUM() Functions
The SUM() function returns the total sum of a
numeric column.

SELECT SUM(column_name)
FROM table_name
WHERE condition;
SQL Exercises and Practice
1. Create a table salesman and add the following records.

You might also like