IGU 3rd Sem Pratical File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

Government College

Ateli
Mahendragarh, Haryana

Practical File
On

Computer Fundamentals & Database Management


Branch/Year – BCA

Session-2023

Submitted To: Submitted By:

Mrs. Name:

Roll No:
INDEX
S.No. Experiment / Topic Date Signature /
Remarks
1. Implementation of Stack Using Array in
“C”

2. “C” Program to search an element in a


linked list

3. “C” Program to sort array in ascending


& descending order using bubble sort

4. “C” Program to Implement Linear


/sequential search
5. “C” Program to Implement Binary
Search
6. “Create Database” Statement with
example
7. “Create Table” Statement

8. “Drop Database” Statement with


example

9. “Alter Table” Statement

10. “Backup Database” Statement


Implementation of Stack Using Array in C

He C Program is written for implementation of STACK using Array, the basic operations of stack are
PUSH () and POP (). PUSH function in the code is used to insert an element to the top of stack, POP
function used to remove the element from the top of stack.

C Program for STACK Using Arrays

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
OUTPUT FROM THIS INPUT
Enter the size of STACK[MAX=100]:10
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12

Enter the Choice:1


Enter a value to be pushed:24

Enter the Choice:1


Enter a value to be pushed:98

Enter the Choice:3

The elements in STACK

98
24
12
Press Next Choice
Enter the Choice:2

The popped elements is 98


Enter the Choice:3

The elements in STACK

24
12
Press Next Choice
Enter the Choice:4
EXIT POINT
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}

}
Output

Linked List: 10 15 20 25
Enter element to search: 20
Item found at position: 3
C Program to search an element in a linked list
There is Two methods to Search the element in list –

Method 1: Iterative

Method 2: Recursive

Method 1 (Iterative)

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

struct Node
{
int data;
struct Node *next;
};

void display (struct Node *node)


{

//as linked list will end when Node is Null


while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
printf ("\n");
}

int searchElement (struct Node *head, int item)


{
struct Node *current = head; // Initialize current
int index = 0;
// traverse till then end of the linked list
while (current != NULL)
{
if (current->data == item)
{
return index;
}
current = current->next;
index++;
}
return -1;
}
int main ()
{
int item;

//creating 4 pointers of type struct Node


//So these can point to address of struct type variable
struct Node *head = NULL;
struct Node *node2 = NULL;
struct Node *node3 = NULL;
struct Node *node4 = NULL;

// allocate 3 nodes in the heap


head = (struct Node *) malloc (sizeof (struct Node));
node2 = (struct Node *) malloc (sizeof (struct Node));
node3 = (struct Node *) malloc (sizeof (struct Node));
node4 = (struct Node *) malloc (sizeof (struct Node));

head->data = 10; // data set for head node


head->next = node2; // next pointer assigned to address of node2

node2->data = 15;
node2->next = node3;

node3->data = 20;
node3->next = node4;

node4->data = 25;
node4->next = NULL;

printf ("Linked List: ");


display (head);

printf ("Enter element to search: ");


scanf ("%d", &item);

int index = searchElement (head, item);

if (index == -1)
printf ("Item not found");
else
printf ("Item found at position: %d", index + 1);

return 0;
}
Output

Linked List: 10 15 20 25
Enter element to search: 10
Item found at position: 1
Method 2 (Recursive)
#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
};

void display (struct Node *node)


{

//as linked list will end when Node is Null


while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
printf ("\n");
}

int searchElement (struct Node *head, int item, int index)


{
// Base case
if (head == NULL)
return -1;

// If data is present in current node, return true


if (head->data == item)
return index;

// not present here will check for next position


// in next recursive iteration
index++;

// Recur for remaining list


return searchElement (head->next, item, index);
}

int main ()
{
int item;

//creating 4 pointers of type struct Node


//So these can point to address of struct type variable
struct Node *head = NULL;
struct Node *node2 = NULL;
struct Node *node3 = NULL;
struct Node *node4 = NULL;

// allocate 3 nodes in the heap


head = (struct Node *) malloc (sizeof (struct Node));
node2 = (struct Node *) malloc (sizeof (struct Node));
node3 = (struct Node *) malloc (sizeof (struct Node));
node4 = (struct Node *) malloc (sizeof (struct Node));

head->data = 10; // data set for head node


head->next = node2; // next pointer assigned to address of node2

node2->data = 15;
node2->next = node3;

node3->data = 20;
node3->next = node4;

node4->data = 25;
node4->next = NULL;

printf ("Linked List: ");


display (head);

printf ("Enter element to search: ");


scanf ("%d", &item);

int index = searchElement (head, item, 0);

if (index == -1)
printf ("Item not found");
else
printf ("Item found at position: %d", index + 1);

return 0;
}
Output:

Enter number of elements :: 6

Enter 6 integers ::

Enter 1 integer :: 4

Enter 2 integer :: 1

Enter 3 integer :: 8

Enter 4 integer :: 6

Enter 5 integer :: 0

Enter 6 integer :: 9

Sorted list in descending order : 9 8 6 4 1 0


C Program to sort array in descending order using bubble sort

Sort array in descending order using bubble sort :-


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same
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. Instead of declaring individual variables, such as number0,
number1, …, and number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is
accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.

Here is source code of the C Program to sort array in descending order using bubble sort. The C program
is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also
shown in below.

#include<stdio.h>
int main(){
int array[50], n, i, j, temp;
printf("Enter number of elements :: ");
scanf("%d", &n);
printf("\nEnter %d integers :: \n", n);
for(i = 0; i < n; i++)
{
printf("\nEnter %d integer :: ", i+1);
scanf("%d", &array[i]);
}

for (i = 0 ; i < ( n - 1 ); i++){


for (j= 0 ; j < n - i - 1; j++){
if(array[j] < array[j+1]){
temp=array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("\nSorted list in descending order : ");
for ( i = 0 ; i < n ; i++ )
printf(" %d ", array[i]);
return 0;
}
Output

Enter the value of num


6
Enter the elements one by one
23
45
67
89
12
34
Input array is
23
45
67
89
12
34
Sorted array is...
12
23
34
45
67
89
C Program to Sort N Numbers in Ascending Order using Bubble Sort
This C Program sorts the numbers in ascending order using bubble sort. Bubble sort is a simple sorting
algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of
adjacent items and swapping them if they are in the wrong order. Here we need to sort a number in
ascending order.Here is source code of the C program to sort the numbers in ascending order using
bubble sort. The C program is successfully compiled and run on a Linux system. The program output is
also shown below.

#include <stdio.h>
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, j, num, temp;

printf("Enter the value of num \n");


scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
}
Output :

How Many Elements?10


Enter array elements:
21
22
34
12
66
77
87
99
09
39
Enter element to search 87
Element found at index 6
…Program finished with exit code 0
Press ENTER to exit console.
C Program to implement Linear/sequential Search
What is a Linear Search?
A linear search, also known as a sequential search, is a method of finding an element within a list. It
checks each element of the list sequentially until a match is found or the whole list has been searched.

A simple approach to implement a linear search is

 Begin with the leftmost element of arr[] and one by one compare x with each element.
 If x matches with an element then return the index.
 If x does not match with any of the elements then return -1.

#include<stdio.h>

int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);

printf("Enter array elements:n");


for(i=0;i<n;++i)
scanf("%d",&a[i]);

printf("nEnter element to search:");


scanf("%d",&x);

for(i=0;i<n;++i)
if(a[i]==x)
break;

if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");

return 0;
}
Output

Enter A Number : 67

THE NUMBER 67 EXISTS IN THE ARRAY


Sequential Search Programs in C :
C program to search a key number in an array using Sequential Search Method.

#include <stdio.h>
#include <conio.h>
main()
{
int arr[]={12,23,78,98,67,56,45,19,65,9},key,i,flag=0;
clrscr();
printf("\nENTER A NUMBER: ");
scanf("%d",&key);
for(i=0;i<10;i++)
{
if(key==arr[i])
flag=1;
}
if(flag==1)
printf("\nTHE NUMBER %d EXISTS IN THE ARRAY",key);
else
printf("\nTHE NUMBER %d DOES NOT EXIST IN THE ARRAY",key);
getch();
}
Output:
If the key is present:

Enter number of elements


5
Enter 5 Integers
1
2
3
4
5
Enter value to find
4
Found at Location 4.
--------------------------------
(Program exited with code: 0)
Press any key to continue. . . .

If Key is not present:

Enter number of elements


5
Enter 5 Integers
1
2
3
4
5
Enter value to find
7
Not found! 7 isn’t present in the list.

---------------------------------------------------
(Program exited with code: 0)
Press any key to continue. . . . .
C Program to implement Binary Search
Binary Search In C:
A Binary Search is a sorting algorithm, that is used to search an element in a sorted array. A binary
search technique works only on a sorted array, so an array must be sorted to apply binary search on the
array. It is a searching technique that is better then the liner search technique as the number of
iterations decreases in the binary search.The logic behind the binary search is that there is a key. This
key holds the value to be searched. The highest and the lowest value are added and divided by 2.
Highest and lowest and the first and last element in the array. The mid value is then compared with the
key. If mid is equal to the key, then we get the output directly. Else if the key is greater then mid then
the mid+1 becomes the lowest value and the process is repeated on the shortened array. Else if the key
value is less then mid, mid-1 becomes the highest value and the process is repeated on the shortened
array. If it is not found anywhere, an error message is displayed.

Example

#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}
Command syntax
CREATE DATABASE
The CREATE DATABASE statement is a Data Definition Language (DDL) statement in SQL that creates a
new database. The syntax for the statement is:

 CREATE DATABASE DB_NAME


 CREATE DATABASE IF NOT EXISTS my DB
 SHOW DATABASES
 USE my DB

Here are some things to note about the CREATE DATABASE statement:

 Database names: Database names are case-sensitive on Linux or UNIX, but not on Windows.
 Privileges: The user must have the CREATE privilege for the database.
 Connection: If a previous CONNECT statement established an explicit connection to a database,
the user cannot use the CREATE DATABASE statement until they use the DISCONNECT statement
to close the connection.

The CREATE DATABASE command initializes a new database with an optional user-defined collating
sequence, creates the three initial table spaces, creates the system tables, and allocates the recovery log
file. When you initialize a new database, the AUTOCONFIGURE command is issued by default.

Important: The Triple Data Encryption Standard (3DES) native encryption option is deprecated and
might be removed in a future release. As a replacement, use the Advanced Encryption Standard (AES)
native encryption option.

Note: When the instance and database directories are created by the Db2® database manager, the
permissions are accurate and cannot be changed.

When the CREATE DATABASE command is issued, the Configuration Advisor also runs automatically. It
means that the database configuration parameters are automatically tuned for you according to your
system resources. In addition, Automated RUNSTATS is enabled. To disable the Configuration Advisor
from running at database creation, refer to the DB2_ENABLE_AUTOCONFIG_DEFAULT registry variable.
To disable Automated RUNSTATS, refer to the auto_runstats database configuration parameter.

Adaptive Self-Tuning Memory is also enabled by default for single partition databases. To disable
Adaptive Self-Tuning-Tuning Memory by default, refer to the self_tuning_mem database configuration
parameter. For multi-partition databases, Adaptive Self-Memory is turned off by default.

If no code set is specified on the CREATE DATABASE command, then the collations that are allowed are:
SYSTEM, IDENTITY_16BIT, language-aware-collation, and locale-sensistive-collation (SQLCODE -1083).
The default code set for a database is UTF-8. If a particular code set and territory is needed for a
database, the required code set and territory can be specified in the CREATE DATABASE command.

This command is not valid on a client.

Scope
In a partitioned database environment, this command affects all database partitions that are listed in
the db2nodes.cfg file.The database partition from which this command is issued becomes the catalog
database partition for the new database.

Command syntax
Authorization
You must have one of the following authorities:

 SYSADM
 SYSCTRL

Required connection

Instance. To create a database at another (remote) database partition server, you must first attach to
that server. A database connection is temporarily established by this command during processing.
Syntax 1
The syntax for the CREATE TABLE AS statement when copying all of the columns in SQL is:

CREATE TABLE new_table

AS (SELECT * FROM old_table);

Example 1.1
Let's look at an example that shows how to create a table by copying all columns from another table.

CREATE TABLE suppliers

AS (SELECT *

FROM companies

WHERE id > 1000);

Syntax 2
The syntax for the CREATE TABLE AS statement copying the selected columns is:

CREATE TABLE new_table

AS (SELECT column_1, column2, ... column_n

FROM old_table);

Example 2.1
Let's look at an example that shows how to create a table by copying selected columns from another
table.

CREATE TABLE suppliers

AS (SELECT id, address, city, state, zip

FROM companies
WHERE id > 1000);

CREATE TABLE AS Statement


This SQL tutorial explains how to use the SQL CREATE TABLE AS statement with syntax and examples.

Description
You can also use the SQL CREATE TABLE AS statement to create a table from an existing table by copying
the existing table's columns.

It is important to note that when creating a table in this way, the new table will be populated with the
records from the existing table (based on the SELECT Statement). Just like Syntax

Create Table - By Copying all columns from another table


This would create a new table called suppliers that included all columns from the companies table.

If there were records in the companies table, then the new suppliers table would also contain the
records selected by the SELECT statement. For Example 1.1

Create Table - By Copying selected columns from another table


Again, if there were records in the companies table, then the new suppliers table would also contain the
records selected by the SELECT statement.

Create Table - By Copying selected columns from multiple tables


Syntax
The syntax for the CREATE TABLE AS statement copying columns from multiple tables is:

CREATE TABLE new_table

AS (SELECT column_1, column2, ... column_n

FROM old_table_1, old_table_2, ... old_table_n);

Example
Let's look at an example that shows how to create a table by copying selected columns from multiple
tables.

CREATE TABLE suppliers

AS (SELECT companies.id, companies.address, categories.cat_type

FROM companies, categories

WHERE companies.id = categories.id

AND companies.id > 1000);


This would create a new table called suppliers based on columns from both
the companies and categories tables.

Syntax

Alter Table:-
ALTER TABLE
Use the ALTER TABLE statement to alter the definition of a nonpartitioned table, a partitioned table, a
table partition, or a table subpartition. For object tables or relational tables with object columns, use
ALTER TABLE to convert the table to the latest definition of its referenced type after the type has been
altered.

Note: Oracle recommends that you use the ALTER MATERIALIZED VIEW LOG statement, rather than
ALTER TABLE, whenever possible for operations on materialized view log tables.

Prerequisites
The table must be in your own schema, or you must have ALTER object privilege on the table, or you
must have ALTER ANY TABLE system privilege.

Additional Prerequisites for Partitioning Operations

If you are not the owner of the table, then you need the DROP ANY TABLE privilege in order to use the
drop_table_partition or truncate_table_partition clause.

You must also have space quota in the tablespace in which space is to be acquired in order to use the
add_table_partition, modify_table_partition, move_table_partition, and split_table_partition clauses.

When a partitioning operation cascades to reference-partitioned child tables, privileges are not required
on the reference-partitioned child tables.

When using the exchange_partition_subpart clause, if the table data being exchanged contains an
identity column and you are not the owner of both tables involved in the exchange, then you must have
the ALTER ANY SEQUENCE system privilege.

Additional Prerequisites for Constraints and Triggers

To enable a unique or primary key constraint, you must have the privileges necessary to create an index
on the table. You need these privileges because Oracle Database creates an index on the columns of the
unique or primary key in the schema containing the table. To enable or disable triggers, the triggers
must be in your schema or you must have the ALTER ANY TRIGGER system privilege.

Additional Prerequisites When Using Object Types

To use an object type in a column definition when modifying a table, either that object must belong to
the same schema as the table being altered, or you must have either the EXECUTE ANY TYPE system
privilege or the EXECUTE object privilege for the object type.
Additional Prerequisites for Flashback Data Archive Operations
To use the flashback_archive_clause to enable historical tracking for the table, you must have the
FLASHBACK ARCHIVE object privilege on the flashback archive that will contain the historical data. To
use the flashback_archive_clause to disable historical tracking for the table, you must have the
FLASHBACK ARCHIVE ADMINSTER system privilege or you must be logged in as SYSDBA.

Backup Database Command syntax


BACKUP DATABASE Command
The BACKUP DATABASE command creates a backup copy of a database or a table space. A backup of
your database and related stored data must be created to prevent data loss if a database service outage
occurs.

For information about database backup operations between different operating systems and hardware
platforms, see “Backup and restore operations between different operating systems and hardware
platforms”.

Scope
In a partitioned database environment, if no database partitions are specified, this command affects
only the database partition on which it is run.

If the option to perform a partitioned backup is specified, the command can be called only on the
catalog database partition. If the option specifies that all database partition servers are to be backed up,
it affects all database partition servers that are listed in the db2nodes.cfg file. Otherwise, it affects the
database partition servers that are specified on the command.

Authorization

One of the following authorities:

 SYSADM
 SYSCTRL
 SYSMAINT

Required connection
Database. This command automatically establishes an exclusive connection to the specified database.

Note: If a connection to the specified database exists, that connection is terminated and a new
connection established specifically for the backup operation. The connection is stopped at the
completion of the backup operation.

You might also like