Z Code Writer is an Online Blog for beginner programmers. Here, you will find tech, build your Programming Skills and other Programming Languages like C, C++, HTML, JavaScript, Java Code, Web Designing and the University of Sialkot (USKT) CGPA / GPA Calculator, etc.

Friday, 10 July 2020

First Come First Serve (FCFS) in Operating System

First Come First Serve (FCFS) in Operating System
             
        First Come First Served (FCFS) is a process scheduling algorithm in operating system (OS) that automatically execute processes and queued data by their sequence. In this Algorithm which process comes first to the queue is execute first, the next process will execute when the previous process complete their execution. FCFS is also known as (FIFO) First in First Out and First Come First Choice (FCFC).

Program in C / C++

#include<stdio.h>
int nxt=0,arry[5],c=0,d;
void menu();
void Insert();
void Delete();
void Display();
int main(){
int ch=1;
menu();
while(ch!=0){
printf(" Enter Your Choice ");
scanf("%i",&ch);
switch(ch){
case 0:
printf(" Exit ........ \n");
break;
case 1:
Insert();
break;
case 2:
Delete();
break;
case 3:
Display();
break;
default: 
printf(" Invalid Choice \n");
}
}
}

void menu(){
printf("--------- Menu --------\n");
printf(" Press 1 for Insert\n");
printf(" Press 2 for Delete\n");
printf(" Press 3 for Display\n");
printf(" Press 0 for Exit\n\n");
}

void Insert(){
if(c<5){
c++;
printf(" Enter data ");
scanf("%i",&d);
arry[nxt]=d;
nxt++;
}
else{
printf(" Que is Full \n");
}
}

void Delete(){
if(c>0){
c--;
for(int i=0;i<nxt;i++){
arry[i]=arry[i+1];
}
nxt--;
printf("\n Deletion Have Done  \n\n");
}
else{
printf(" Empty ...! \n");
}
}

void Display(){
if(c>0){
for(int i=0;i<nxt;i++){
printf(" Priority %d",i+1);
printf(" Job %d \n",arry[i]);
}
}
else{
printf(" Empty ...! \n");
}
}

First-Come-First-Serve-(FCFS)
First Come First Serve (FCFS)


Saturday, 2 May 2020

Define Bridge ...!

     A bridge is a type of computer network device that provides interconnection with other bridge networks that use the same protocol. Bridge devices work at the data link layer of the Open System Interconnect (OSI) model, connecting two different networks together and providing communication between them. Bridges are similar to repeaters and hubs in that they broadcast data to every node. However, bridges maintain the media access control (MAC) address table as soon as they discover new segments, so subsequent transmissions are sent to only to the desired recipient. Bridges are also known as Layer 2 switches.


A bridge uses a database to ascertain where to pass, transmit or discard the data frame.
If the frame received by the bridge is meant for a segment that resides on the same host network, it will pass the frame to that node and the receiving bridge will then discard it.
If the bridge receives a frame whose node MAC address is of the connected network, it will forward the frame toward it.


     In computer networking, a bridge serves the same purpose. It connects two or more local area networks (LANs) together. The cars, or the data in this case, use the bridge to travel to and from different areas of the network. The device is similar to a router, but it does not analyze the data being forwarded. Because of this, bridges are typically fast at transferring data, but not as versatile as a router. For example, a bridge cannot be used as a firewall like most routers can. A bridge can transfer data between different protocols (i.e. a Token Ring and Ethernet network) and operates at the "data link layer" or level 2 of the OSI (Open Systems Interconnection) networking reference model.


A bridge operates at data link layer. A bridge is a repeater, with add on the functionality of filtering content by reading the MAC addresses of source and destination. It is also used for interconnecting two LANs working on the same protocol. It has a single input and single output port, thus making it a 2 port device.

A network bridge is a computer networking device that creates a single aggregate network from multiple communication networks or network segments. This function is called network bridging.[1] Bridging is distinct from routing. Routing allows multiple networks to communicate independently and yet remain separate, whereas bridging connects two separate networks as if they were a single network.[2] In the OSI model, bridging is performed in the data link layer (layer 2).[3] If one or more segments of the bridged network are wireless, the device is known as a wireless bridge.

The main types of network bridging technologies are simple bridging, multiport bridging, and learning or transparent bridging

Sunday, 19 April 2020

Prime Number Program In C / C++

Prime Number Program In C / C++

     A whole Number which is greater than 1 and which is divider of itself. For example 13 divided only 13,17 divided only 17 and series is 2,3,5,7,11,13,17, ...

Prime Code in C

#include<stdio.h>
#include<conio.h>

int main(){
int num,i,j;
int temp=0;
printf(" Enter Any integer Number ");
scanf("%d",&num);
if(num<2){
printf(" Number Should be greater than 1 \n");
}
else{
for(i=2;i<=num;i++){
for(j=2;j<i;j++){
if(i%j==0){
temp=1;
}
}
if(temp==0){
printf("\n %d",i);
}
else{
temp=0;
}
}
}
return 0;

}


Prime Number Code in C++

#include<iostream>
using namespace std;

int main(){
int num,i,j;
int temp=0;
cout<<" Enter Any integer Number ";
cin>>num;
if(num<2){
cout<<" Number Should be greater than 1 \n";
}
else{
for(i=2;i<=num;i++){
for(j=2;j<i;j++){
if(i%j==0){
temp=1;
}
}
if(temp==0){
cout<<"\n"<<i;
}
else{
temp=0;
}
}
}
return 0;

}

Saturday, 21 December 2019

Implementation Of Hash Table Code

Implementation Of Hash Table Code


#include<iostream>
using namespace std;
// Create an Entity
class Entry{
public:
            int key;
            int value;
Entry(int k,int v){
            key= k;
            value = v;
}
};
class HashTable{
Entry **temp;
            public:
            int size;
//Constructor
            HashTable() {
                        cout<<" Enter Size of Table: ";
                        cin>>size;
                        temp = new Entry * [size];
                        for (int i = 0; i< size; i++) {
                        temp[i] = NULL;
                        }
            }
// Hashing Funtion
int HashFun(int k) {
        return k % size;
    }
// Insertion In Hashing
void Insert(int k,int v){
            if(k<=size && k>0){
int h = HashFun(k);
        while(temp[h] != NULL && temp[h]->key == k){
        h = HashFun(h + 1);
        }
        if(temp[h] != NULL)
            delete temp[h];
        temp[h] = new Entry(k, v);
        }
    else{
            cout<<" You Are Enter Invalid Key "<<endl<<endl;
}
    }
// Searching In Hashing
            int SearchKey(int k){
                        int h = HashFun(k);
while (temp[h] != NULL && temp[h]->key != k){
                        h = HashFun(h + 1);
                        }
                        if(temp[h] == NULL){
                        return -1;
                        }
                        else{
                        return temp[h]->value;
                        }
            }
// Delection In Hashing
void Delete(int k){
            int h = HashFun(k);
while (temp[h] != NULL){
                                    if(temp[h]->key == k)
                                    break;
                                    h = HashFun(h + 1);
                        }
                        if (temp[h] == NULL){
                                    cout<<" No Element Found at Key "<<k<<endl<<endl;
                        return;
                        }
                        else{
                                    delete temp[h];
                        }
            cout<<" Element Successfuly Deleted "<<endl<<endl;
            }
};

int main(){
            HashTable h;
            int k, v;
            int c;
                        while(1){
                                    cout<<endl;
                                    cout<<" 1. Insert Element into Table"<<endl;
                                    cout<<" 2. Search Element from Key"<<endl;
                                    cout<<" 3. Delete Element from Key"<<endl;
                                    cout<<" 4. Exit"<<endl;
                                    cout<<"\nEnter your Choice: ";
                                    cin>>c;
            switch(c){
                        case 1:
                                    cout<<"Enter Element to Insert: ";
                                    cin>>v;
                                    cout<<"Enter key at which Element to be Insert: ";
                                    cin>>k;
                                    h.Insert(k, v);
                                    break;
                        case 2:
                                    cout<<"Enter key which Element you want to Search: ";
                                    cin>>k;
                                                if(h.SearchKey(k) == -1){
                                                            cout<<" No Element Found at Key "<<k<<endl<<endl;
                                                            continue;
                                                }
                                    else{
                                                cout<<" Element at Key "<<k<<" : ";
                                                cout<<h.SearchKey(k)<<endl<<endl;
                                    }
                                    break;
                        case 3:
                                    cout<<"\n Enter Key which Element you want to Delete: ";
                                    cin>>k;
                                    h.Delete(k);
                                    break;
                        case 4:
                                    exit(1);
                        default:
                                    cout<<"\n Please Enter Correct Option\n";
                        }
            }
   return 0;
}


Hashing Introduction

Introduction 

Searching Techniques:

        There are several searching techniques like linear search, binary search etc. In these techniques, time taken to search any particular element depends on the total number of elements. For Example complexity of Linear Search O(n), Binary Search O(logn), Hash table O(1).

Hashing:

          Hashing is an algorithm that calculates a fixed-size bit value from indexes. It is a searching technique that Used to Uniquely identify a specific object from a group of similar object.

Hash:

          Hash is a function that convert one value to another. Hashing data is a common practice in computer science and is used for several different purposes.

Hash Function:

      Hash function is a function that maps any number or string to a small integer value. Hash function takes the data item as an input and returns a small integer value as an output. The small integer value is called as a hash value. Hash value of the data item is then used as an index for storing it into the hash table.
 h(k) = k mod (%) SizeOfArray

Properties of Hash Function:

- It is efficiently computable.
          - It minimizes the number of collisions.
          - It distributes the keys uniformly over the table.


Collision:

        Hash function is get a number of a key, there is possibility that there are two key result in same value. The situation where a newly inserted key maps to an already occupied slot in hash table is called collision. We must need to solve it. There are basic two methods of solving collision.


 

2. Separate Chaining:

             In this technique, each cell point to a linked list of a record. If any key has same number than that index creates a linked list Which have same function value.



2. Open Addressing: 

        Like separating chaining, open addressing is also a method for handling collisions. In this collision, All the element is stored in a separate index (itself) in hash table. If any bucket is already occupied, then it is increased key every time until it found empty index to place. It has also three types.

i.                   Quadratic Probing:


Quadratic probing is similar to linear probing but it has one difference When collision occurs, we probe for 𝑘^2 bucket in 𝑘^𝑡ℎ iteration. We keep probing until an empty bucket is found. Formula h(k) =  mod (%) SizeOfArray


ii.                   Quadratic Probing:

Quadratic probing is similar to linear probing but it has one difference When collision occurs, we probe for 𝑘^2 bucket in 𝑘^𝑡ℎ iteration. We keep probing until an empty bucket is found. Formula h(k) =  mod (%) SizeOfArray


iii.                   Double hashing:

Double hashing is similar to linear probing but it has also difference We use another hash function hash2(k) and look for k * hash2(k) bucket in 𝑘^𝑡ℎ iteration. It requires more computation time as two hash functions need to be computed. h(k) = key mod(%) SizeOfArray
h(k) = (h(k) –k) mod(%) h(k)

Saturday, 14 December 2019

1D Array

#include <iostream>
using namespace std;

int main()
{
    int arr[10],sum=0,i;
    cout<<"Enter 10 numbers"<<endl;
    for(i=0;i<10;i++)
    {
        cin>>arr[i];
        sum = sum+arr[i];
    }
    cout<<"Sum = "<<sum;
    return 0;
}

Triangle

#include<iostream>
using namespace std;
int main(){
for(int i=1; i<=5; i++){
for(int j=1; j<=i; j++){
cout<<" ";
}
for(int k=5; k>=i; k--){
cout<<"*";
cout<<" ";
}
cout<<endl;
}

}

Sunday, 18 August 2019

Nested 'if' With Sequence of 'if' Statement

Nested 'if' With Sequence of 'if' Statement

      The nested 'if' structure may contain multiple 'if' statements that may be nested up to any level. Some 'if' statements are executed while other may be skipped.
In a sequence of 'if' statements, multiple 'if' statements are written one after the other. All 'if' statements are executed in an order. No 'if' statement is skipped.
Following program finds whether a number is positive, negative or zero:

         #include <stdio.h>
          void main (void)
         {
                  int num;
                  printf ("Enter a number ? ") ;
                  scanf ("%d" ,  &num) ;
                  if (num > 0)
                           printf ("The number is positive") ;
                  if (num < 0)
                            printf ("The number is negative") ;
                  if (num == 0)
                            printf ("The number is zero") ;
           }

      In the above program, three 'if' statements are given in a sequence. Suppose the user enters a positive number. The number is tested with first 'if' statement. The answer is true because number is positive. The message is displayed on the display screen. After displaying the message, other two 'if' statements will also be executed. Although the answers of these two 'if' statements will be false. There is no need to execute these 'if' statements. These statements should be skipped. Otherwise processor of computer has to consume extra time without any purpose.
      The 'if-else-if' structure can be used to handle this kind of situation.


The 'if-else-if' Statement

      The 'if-else-if' statement is also called multiple 'if-else' statement or nested 'if-else' statement. It is used to execute on block of statements from multiple blocks of statements. In this statement/structure, multiple conditions and multiple blocks of statements are given. When any given condition is true, the statement associated with that condition are executed. All other blocks of statements are ignored.
      The general syntax of nested 'if-else' is as follows:

                 if (condition-1)
                 { Block-1 }
                 else if (condition-2)
                        { Block-2 }
                 ------------------------
                 ------------------------
                 else if (condition-m)
                        { Block-m }
                        else
                        { Block-n }

      The use of "else" part of condition-m is optional.



Working of 'if-else-if' Structure


     The 'if-else-if' structure is executed as follows:
  • The conditions in this structure are evaluated in the given sequence until a true condition is found.
  • When true condition is found, the statements associated with that condition are executed. All remaining conditions are skipped.
  • If all conditions are false, then the block of statements following the last else is executed.

Flowchart of 'if-else-if' Structure

     The flowchart of nested 'if-else' structure is as follows: