Wednesday, February 17, 2010

merge sort code in c++ to count number of comparisons

#include < iostream >
#include < stdio.h >

using namespace std;

int count = 0; //count of comparisons
int n = 0;
const int MAX_ITEMS = 100;
void merge(int values[], int leftFirst, int leftLast, int rightFirst, int rightLast);
void printarray( int a[], int n);
void mergesort(int a[], int start, int end){  //no significant comparisons are done during splitting
   
    if(start < end){
        int mid = (start+end)/2;   
        mergesort(a,start, mid);
        mergesort(a,mid+1,end);
        merge(a, start,mid, mid+1, end);
    }
}
void merge(int values[], int leftFirst, int leftLast, int rightFirst, int rightLast){
        int temparray[MAX_ITEMS];
        int index = leftFirst;
        int saveFirst = leftFirst;

        while((leftFirst <= leftLast)  && ( rightFirst <= rightLast)){//compare and select smallest from two subarrays

            if(values[leftFirst] < values[rightFirst]){
                temparray[index]  = values[leftFirst]; //smallest assigned to temp
                leftFirst++;
            }
            else
            {
                temparray[index]  = values[rightFirst];
                rightFirst++;
            }
            index++;
            count++;  //count of comaparisons done during merge. One comparison is done per iteration of while loop. 
        }
       
        while(leftFirst <= leftLast){ 

            temparray[index] = values[leftFirst];
            leftFirst++;
            index++;
           
        }
        while(rightFirst <= rightLast){
            temparray[index] = values[rightFirst];
            rightFirst++;
            index++;
           
        }
       
        for(index = saveFirst; index <= rightLast; index++)//copies from temp array to values array
            values[index] = temparray[index];
        printarray(values,n);
        cout << endl;

    }

void printarray( int a[], int n){
    for (int i=0; i < n; i++)
        cout << a[i] << "  ";
}

int main(){
   
    cout << "Enter number of  elements to be sorted : ";
    cin >>n;

    int a[MAX_ITEMS];
   
for (int i=0; i < n; i++){
        if(i==0)
            cout << "Enter the first element: ";
        else
            cout << "Enter the next element: ";
        cin >>     a[i];
    }
   
    int start = 0;
    int end = n-1;
      mergesort(a, start, end);
    printarray(a, n);

    cout << endl;
    cout  << "Number of comparisons : "<< count << endl;
}

Friday, February 12, 2010

systems programming (UNIX, C)

How to toggle lettercases.

struct termios info;
    int   rv = tcgetattr(0, &info);
    if(info.c_oflag & OLCUC)
            info.c_oflag &=  ~OLCUC;
    else
        info.c_oflag |= OLCUC;
    tcsetattr(0, TCSANOW, &info);

______________________________________________________________

How can we measure CPU time ( user + kernel) of a program in UNIX.

1. Use clock() system call to measure CPU time.
      The clock() function returns the  amount  of  CPU  time  (in
     microseconds)  used  since  the first call to clock() in the
     calling process.
      example
          time_t time = clock();
          int i=0;
          while(i<10000){
              printf("%d\n", i);
              i++;
          }
         time = clock();
         printf( "\nCPU time : %ld\n" , time);// prints CPU time  taken  to print numbers from 0 to 999


2.  Use time command
     example
     bash$     time gcc file.c
3.  Use getitimer and setitimer with ITIMER_REALPROF

Friday, February 5, 2010

IMac

How can I access(SSH) a remote machine from my iMac.

Access the terminal from application -> Utilities -> Terminal
On prompt type ssh -l uername host 
On prompt for password ,  type pasword

How can I switch between two terminal windows in iMac
command- ~

Wednesday, February 3, 2010

c programmin errors, warnings and solutions

Warnings:
  1.  warning: incompatible implicit declaration of built-in function 'malloc'
    solution - add #include < stdlib.h> 
  2.  warning: incompatible implicit declaration of built-in function 'strlen'
    solution-   add #include < string.h>