Professional Documents
Culture Documents
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
#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
for(int a=0;a<4;a++) {
print<<name[a]<<"\n";
}
Reading from and writing into arrays
int a[100];
int v,n;
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.
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.
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.
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.