2017 universities Ranking in Pakistan

Here are overall top universities of Pakistan.Some universities are at the top place in engineering fields but others are at Applied science, BA and in IT programs

Binary Search in C++

Search found at index

Quick sorting program

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Tree traversal (preorder,inorder,postorder) using linklist (c++)

Tree traversal (preorder,inorder,postorder) using linklist (c++)

Wednesday, 14 November 2018

"Infix to Post-fix" conversion and "parenthesis check using "User define" stacks C++


#include<iostream>
using namespace std;
char stack[100]; //
char postfix[100];
char exp[100]; // mathematical expression entered by the user
int top=-1;
/**********************************************/
void push1(char newitem)
{
                if(top=='\0')
                {
                                cout<<"Stack overflow"<<endl;
                }
                else
                {
                                exp[++top]=newitem;
                }
}
/**********************************************/
bool ArePair(char opening,char closing)
{
                if(opening == '(' && closing == ')') return true;
                else if(opening == '{' && closing == '}') return true;
                else if(opening == '[' && closing == ']') return true;
                return false;
}
/**********************************************/


void pop1()
{
                if(top==-1)
                {
                cout<<"Cannot pop. The stack does not exist"<<endl;
                }
                else
                {
                                top--;
                }
}
/**********************************************/
int rtop()
{
    if(top!=-1)
    {
        return exp[top];
    }
    else
    return false;
}
/**********************************************/
bool isempty()
{
    if(top==-1)
    {
        return true;
    }
    else
    {
        return false;
    }

}
/**********************************************/
bool AreParanthesesBalanced(char exp[])       // This function will check that parantheses are balanaced or not. If it if balance it will return true.
{
                for(int i =0;exp[i]!= '\0';i++)
                {
                                if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
                                {
                                    push1(exp[i]);
                                }

                                else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
                                {
                                                if(isempty() || !ArePair(rtop(),exp[i]))
                                                {
                                                   return false;
                                                }

                                                else
                                                {
                                                    pop1();

                                                }
                                }
                }
                return isempty();
}
/**********************************************/
int precedence(int ch)
{
 if(ch=='(')
        return 0;
 else if(ch=='+'||ch=='-')
    return 1;
 else if(ch=='*'||ch=='/'||ch=='%')
    return 2;
   return -1;
}
/**********************************************/
char pop()
{
  if(top==-1)
  {
   cout<<"\n stack is Empty";
  }
  else
  {
    char ch=stack[top];
    top--;
    return (ch);
  }
}
/**********************************************/
void push(int ch)
{
  if(top!=sizeof(stack)-1)
  {
    top++;
   stack[top]=ch;
  }
  else
    cout<<"Stack Overflow"<<endl;
}
/************************************************/
bool IsOperator(char C)
{
                if(C == '+' || C == '-' || C == '*' || C == '/' || C== '$')
                                return true;

                return false;
}
/************************************************/

bool IsOperand(char C)
{
                if(C >= '0' && C <= '9') return true;
                if(C >= 'a' && C <= 'z') return true;
                if(C >= 'A' && C <= 'Z') return true;
                return false;
}
/**********************************************/
void postfixfun(char exp[])
{
    int k=0;
    int j=0;
       int p=0;
    while(exp[k]!='\0')
    {
/*------------------------------------------------------------*/
        if(IsOperand(exp[k])==true)
        {
         postfix[j]=exp[k];
            j++;
        }
/*----------------------------------------------------------------*/
       else if(exp[k]=='(')
        {
          push(exp[k]);
        }
/*-------------------------------------------------------------------*/
       else if(IsOperator(exp[k])==true)
        {
            while(precedence(stack[top])>=precedence(exp[k]))
            {
                postfix[j]=pop();
            j++;
            }
            push(exp[k]);
        }
/*-------------------------------------------------------------------*/
        else if(exp[k]=')')
        {
            while(stack[top]!='(')
            {
                postfix[j]=stack[top];
                j++;
                pop();
            }
            pop();
        }
        k++;
    }
   
    while(top!=-1)
    {
    postfix[j]=pop();
    j++;
    }
}
/************************************************/
int main()
{

    cout<<"Enter your mathematic expression:"<<endl;
    cin>>exp;
    int size;
    size=sizeof(exp);

    if(AreParanthesesBalanced(exp))
    {
        cout<<"Expression is balanced \n" <<endl;
            cout<<"Postfix Expression="<<endl;
            postfixfun(exp);
            for(int i=0;i<size;i++)
            {
                cout<<postfix[i];
            }
    }
    else
    {
        cout<<"Not balanced"<<endl;
    }


    return 0;

}

New Website Design? Contact Us at Quatro Link

Saturday, 3 November 2018

A Program which uses linear searching? C++



Starts from the base index and compare it with the rest of the element of an array. If the serached index found, it returns 1 else it returns -1.


#include<iostream>

using namespace std;
/********************************************/
int linearSearch(int n[],int s,int size)
    {
        for(int i=0;i<size;i++)
        {
            if(n[i]==s)
            {
            return i;
            }
        }

        return -1;
    }

/*********************************************/
int main()
{
   int n[10];
   int s;
   int result;
   cout<<"Enter values in 'int' array"<<endl;
    for(int j=0;j<10;j++)
    {
    cin>>n[j];
    }
   cout<<"Enter number to search in array"<<endl;
   cin>>s;
   int size=sizeof(n)/sizeof(n[0]);
   result=linearSearch(n,s,size);

   if(result==-1)
   {
       cout<<"Value not found!"<<endl;
    }
    else
    {
    cout<<"value is on index"<<result<<endl;
    }
       return 0;

}

Tuesday, 23 October 2018

Implementation of "STACKS" using arrrays (Data structure)


#include <iostream>
#include <cstdlib>
using namespace std;

int stackarr[5];
int size=5;
int top=-1;
/***************************************************/
void push(int item)
{
    if(top==size-1)
    {
        cout<<"\n Stack is Overflow:"<<endl;
    }
    else
    {
        top++;
        stackarr[top]=item;
       cout<<item<<" \n Item is insorted"<<endl;
    }
}
/********************************************************/
int isFull()
{
    if(top=size-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
/*************************************************************/
int pop()
{
    if(top==-1)
    {
        return 0;
    }
    else
    {
        return stackarr[top--];
    }
}
/***************************************************************/
void disply()
{
    if(top==-1)
    {
        cout<<"\n Stack is Empty"<<endl;
    }
    else
    {
        for(int i=0;i<size;i++)
        {
            cout<<stackarr[i]<<" ";
        }
    }
}

/***************************************************************/
int isEmpty()
{
    if(top==-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }

}
/*************************************************************/
void topfun()
{
    if(isEmpty())
    {
        cout<<" \n Stack is Empty"<<endl;
    }
    else
    {
        cout<<"\n Top element= "<<stackarr[top]<<endl;
    }
}

/*************************************************************/
int main()
{
    int choice,item,r;
   while(1)
   {
    cout << "Enter 1 for push: " << endl;
    cout << "Enter 2 for pop: " << endl;
    cout << "Enter 3 for Top: " << endl;
    cout << "Enter 4 for display: " << endl;
    cout << "Enter 5 for exit: " << endl;
    cout<<"Enter your choice:"<<endl;
    cin>>choice;
        switch(choice)
        {
            case 1: cout<<"\n Enter items in stack: "<<endl;
                    cin>>item;
                    push(item);
                    break;

            case 2:     r=pop();
                        if(r==0)
                        {
                            cout<<"\n Stack is underflow"<<endl;
                        }
                        else
                        {
                            cout<<"\n ITem is popped "<<endl;
                        }
                        break;

            case 3: topfun();
            break;

            case 4: disply();
            break;

           case 5: exit(0);;
               break;

            default :
                cout<<"\n invalid input"<<endl;
        }
   }

    return 0;
}

Saturday, 20 October 2018

Quick sorting program



Worse case performance: O(n^2)
Best case performance: O(nlogn)
Average performance: O(nlogn)


#include <iostream>

void quicksort(int arr[],int low,int high);
int partition(int arr[],int low,int high);
using namespace std;
/***********************************/
int partition(int arr[],int low,int high)
{
    int pivot,i;
    pivot=arr[high];
    i=low-1;

    for(int j=low; j<high; j++)

    {
        if(arr[j]<=pivot)
        {
            i++;
            swap(arr[i],arr[j]);
        }
    }
    swap(arr[i+1],arr[high]);
    return(i+1);
}
/*********************************/
void quicksort(int arr[],int low, int high)
{
    if(low<high)
    {
    int pivot;
    pivot=partition(arr,low,high);

    quicksort(arr,low,pivot-1);

    quicksort(arr,pivot+1,high);
    }
}
/*********************************/
int main()
{
    int arr[5]={5,4,3,2,1};
    int n=5;
    cout << "Before sorting values is: ";
    for(int i=0;i<n;i++)
    {
    cout<<arr[i]<<" ";
    }
    quicksort(arr,0,n-1);
    cout<<"\n After sorting: ";
    for(int i=0;i<n;i++)
    {
    cout<<arr[i]<<" ";
    }
    cout<<endl;
    return 0;
}


Thursday, 18 October 2018

Merge sorting program in C++

#include <iostream>

using namespace std;
/****************************************************************************************************/
void merg(int *arr,int first,int last)
{
    int mid=(first+last)/2;
 int i=first;
 int j=mid+1;
 int k=first;
 int temp[100];

    while(i<=mid && j<=last)
    {
        if(arr[i]<arr[j])
            {
            temp[k++]=arr[i++];
            }
        else
            {
                temp[k++]=arr[j++];
            }
    }
        while(i<=mid)
        {
            temp[k++]=arr[i++];
        }
            while(j<=mid)
            {
                temp[k++]=arr[j++];
            }
    for(int i=first;i<=last;i++)
    {
        arr[i]=temp[i];
    }
}
/*********************************************************************************************************/
void mergesort(int arr[],int first,int last)
{
    if(first>=last)
    {
        return;
    }

    int mid=(first+last)/2;

    mergesort(arr,first,mid);
    mergesort(arr,mid+1,last);

    merg(arr,first,last);
}
/**************************************************************************************************************/

int main()
{
    int arr[100];
    int n;
    cout<<"Enter number of elements in array:"<<endl;
    cin>>n;
    cout<<"Enter numbers to be sort:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    mergesort(arr,0,n-1);
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    return 0;
}

Wednesday, 10 October 2018

Insertion sorting using pointers and function


Write a program for insertion sorting. 

 

#include <iostream>

using namespace std;

void insertionSort(int *p,int size);
void insertionSort(int *p,int size)
{
    int i,j,temp;
    for(i=1;i<size;i++)
    {
        temp=p[i];
        j=i-1;
        while(j>=0 && p[j]>temp)
        {
            p[j+1]=p[j];
        j--;
        }
        p[j+1]=temp;
    }
    for(i=0;i<size;i++)
    {
        cout<<p[i]<<" ";
    }
}

int main()
{
    int n;
    int *p;
    cout<<"Enter number of element you want to store in an a array:"<<endl;
    cin>>n;
    p=new int[n];
    cout<<"Now enter the values"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>p[i];
    }
    insertionSort(p,n);

}

Checkout more java programs on Zamacodes.com
























         


Operation with dynamically 2-D arrays


Write a menu driven program to do following operation on two dimensional arrays dynamically created at run time depending upon user’s requirement. Take the size of arrays from user. The size of first array is nxl, the size of second array is lxm. The arrays need not to be only square in size. 
 

1. Add the contents of the two array A and B and store it in third array C.  
2. Multiply array A and B and store the results in another array D 
3. Upper-half which takes a two dimensional array A, with size N rows and N columns as argument and prints the upper half of the array including diagonal. Here is an example,
   12 13 11 15 10                           12 13 11 15 10
   27 21 25 23 21                                21 25 23 21
   32 35 37 38 31                                     37 38 31
   40 41 45 40 41                                          40 41
   53 54 59 51 55                                               55
4. Lower-half which takes a two dimensional array A, with size N rows and N columns as argument and prints the lower half of the array excluding diagonal in the array.


Program:


#include <iostream>

using namespace std;

void sum(int **A,int **B,int row,int col);
void multi(int **A,int **B,int row,int col);
void Upper_half(int **A,int row,int col);
void lower_half(int **A,int row,int col);

void sum(int **A,int **B,int row,int col)
{
    int **C;

     C=new int*[row];
    for(int i=0;i<5;i++)
     C[i]=new int[col];
     // summing
      for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                    C[r][c]=(A[r][c])+(B[r][c]);
                }
            }

            // Printing array 'C' elements
    cout<<" \n Sum of two array 'A' & 'B' in to array 'C' :"<<endl<<endl;
            for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                cout<<C[r][c]<<"    ";
                }
            cout<<endl;
            }
}

void multi(int **A,int **B,int row,int col)
{
    int **D;

     D=new int*[row];
    for(int i=0;i<5;i++)
     D[i]=new int[col];

     // Multiplying Two arrays
            for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                    D[r][c]=(A[r][c])*(B[r][c]);
                }
            }

            // Printing array 'D' elements

            cout<<" \n Multiplaying Array 'A' & 'B' and saving into array 'D':"<<endl<<endl;
            for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                cout<<D[r][c]<<"    ";
                }
            cout<<endl;
            }

}

void Upper_half(int **A,int row,int col)
{
         cout<<" \n Upper-half including triangle of 2d array is:"<<endl<<endl;
          int i, j;

    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (i > j)
            {
                cout << "    ";
            }
            else
            cout <<A[i][j] << "   ";
        }
        cout << endl;
    }
}
void lower_half(int **A,int row,int col)
{
    cout<<" \n Upper-half including triangle of 2d array is:"<<endl<<endl;
          int i, j;

           for (i = 0; i < row; i++)
            {
                for (j = 0; j < col; j++)
                    {
                        if (i<j)
                        {
                            cout << "    ";
                        }
                        else
                            cout <<A[i][j] << "   ";
                    }
            cout << endl;
            }
}

int main()
{
    int **A;
    int **B;

    int row,col;
    cout<<"Enter the number of rows:"<<endl;
    cin>>row;
    cout<<"Enter the number of columns:"<<endl;
    cin>>col;

    A=new int*[row];
    for(int i=0;i<5;i++)
        A[i]=new int[col];

        B=new int*[row];
    for(int i=0;i<5;i++)
        B[i]=new int[col];

cout<<"Enter value for rows and colums of array 'A' :"<<endl;
         for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                    cin>>A[r][c];
                }
            }

cout<<"Enter value for rows and colums of array 'B' :"<<endl;
         for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                    cin>>B[r][c];
                }
            }

sum(A,B,row,col);
multi(A,B,row,col);
Upper_half(A,row,col);
lower_half(A,row,col);

    return 0;
}