Showing posts with label CODING. Show all posts
Showing posts with label CODING. Show all posts

Saturday, January 27, 2018

Lenovo android phone not visible on android studio

My Lenovo K6 power was not visible on device chooser window. I did enable the developer option and usb debugging option. Even after that my phone was not visible on Android studio device chooser window. Then finally I followed following link and it worked :

https://lenovomobilesupport.lenovo.com/in/en/solutions/ht502856

Friday, January 19, 2018

Two Sum

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for (int i = 0; i< nums.size(); i++) {
            for (int j = i + 1; j < nums.size(); j++) {
                if (target == (nums[i] + nums[j]))  {
                    vector<int> v(2,0);
                    v[0] = i; v[1] = j;
                    return v;
                }
                    
            }
        }
    }
};
Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Sunday, December 31, 2017

multi threaded quick sort using pthreads in C/C++

multi threaded quick sort using pthreads in C/C++

#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

using namespace std;

int i=0;
pthread_t threads[5];


int x[20];

struct thread_data{
    int  upper_bound;
    int lower_bound;
    int *arr;
};
int semid;



void set_sem()
{
    semid=semget(12345,1,IPC_CREAT|0666);
    perror("semget");
    semctl(semid,0,SETVAL,5);
}
int get_sem()
{
    return semctl(semid,0,GETVAL,0);
}

void create_thread(int x[20], int first, int last);


sembuf psembuf={0,-1,SEM_UNDO};
sembuf vsembuf={0,1,SEM_UNDO};


void *quicksort(void *threadarg)
{

    int getv;
    semop(semid,&psembuf,1);
    getv=get_sem();
    cout<<"semaphore value  decrease to  "<<getv;
    struct thread_data *my_data;
    my_data = (struct thread_data *) threadarg;
    int first,last;
    first=my_data->upper_bound;
    last=my_data->lower_bound;
     int pivot,j,temp,i;
    if(first<last){
        pivot=first;
        i=first;
        j=last;
        cout<<"\nbefore done firts loop i = "<<i<<"j="<<j<<endl;
        for(int w=first;w<=last;w++) {
         
            cout<<endl<<"x["<<w<<"] ="<< *((my_data->arr) +w)<<endl;
        }
        while(i<j){
            while(*((my_data->arr)+i) <= *((my_data->arr)+pivot) && i<last)
                i++;
            while(*((my_data->arr)+j) > *((my_data->arr)+pivot) && j>first)
                j--;
            if(i<j){
                temp=*((my_data->arr)+i);
                *((my_data->arr)+i)=*((my_data->arr)+j);
                *((my_data->arr)+j)=temp;
            }
         
        }
        cout<<"\ndone firts loop\n"<<endl<<first<<endl<<last<<endl;

        temp=*((my_data->arr)+pivot);
        *((my_data->arr)+pivot)=*((my_data->arr)+j);
        *((my_data->arr)+j)=temp;
        for(int t = first;t<=last;t++)
            cout<<*((my_data->arr)+t)<<endl;
        for(int w=first;w<=last;w++) {
         
            cout<<endl<<"x["<<w<<"] ="<< my_data->arr[w]<<endl;
        }
        if (first < j-1) {
            cout<<"create thread first = "<<first<<"last = "<<j-1<<endl;
            create_thread(x,first,j-1);
        }
        else if (first == j-1 && my_data->arr[first]>my_data->arr[j]) {
            temp = my_data->arr[first];
            my_data->arr[first] = my_data->arr[j];
            my_data->arr[j] = temp;
        }

        if (j+1<last)
        {
            cout<<"create thread first = "<<j+1<<"last = "<<last<<endl;
            create_thread(x,j+1,last);
        }
        else if (j+1 == last && my_data->arr[last]< my_data->arr[j])
        {
            temp = my_data->arr[last];
            my_data->arr[last] = my_data->arr[j];
            my_data->arr[j] = temp;
        }
     
    }

 
 
 
    semop(semid,&vsembuf,1);

    // pthread_exit(NULL);
}


void create_thread(int x[20],int first, int last)
{
    int rc=0;
    i++;

    pthread_attr_t attr;
    void *status;
    int  getv;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    cout<<" entered create thread" << i <<endl<<"first"<<first<<"last"<<last;
    struct thread_data td;
    td.upper_bound=first;
    td.lower_bound=last;
    td.arr=x;
    getv=get_sem();
    cout<<"semaphore value is  "<<getv<<endl;
    if(getv <  1 )
    {
        sleep(5);
    } 
    rc = pthread_create(&threads[i], NULL,
            quicksort, (void *)&td);
    if (rc){
        cout << "Error:unable to create thread," << rc << endl;
        exit(-1);
    }


    pthread_attr_destroy(&attr);
    rc = pthread_join(threads[i], &status);
    if (rc){
        cout << "Error:unable to join," << rc << endl;
        exit(-1);
    }

    cout<<"existing";

 
}


int main()
{

    int size,i;

    cout<<"Enter size of the array: ";
    cin>>size;

    cout<<"Enter  elements: ";
    for(i=0;i<size;i++)
        cin>>x[i];
    cout<<" hello "<<endl;


    set_sem();

    cout<<"semaphore set to "<<get_sem();
    create_thread(x,0,size-1);

    cout<<"Sorted elements: "<<endl;
    for(i=0;i<size;i++)
        cout<<x[i]<<" "<<endl;

    return 0;
}

Friday, December 29, 2017

C/C++ Program to draw circle using mid point approach

//Program to draw circle using mid point approach
Source code
#include<dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void plotcircle(int x,int y,int a,int b)
{
int xmax=getmaxx();
int ymax=getmaxy();
putpixel(x+xmax/2+a,ymax/2-y-b,15);
putpixel(xmax/2+y+a,ymax/2-x-b,15);
putpixel(x+xmax/2+a,ymax/2+y-b,15);
putpixel(y+xmax/2+a,ymax/2+x-b,15);
putpixel(xmax/2-x+a,ymax/2-y-b,15);
putpixel(xmax/2-y+a,ymax/2-x-b,15);
putpixel(xmax/2-x+a,ymax/2+y-b,15);
putpixel(xmax/2-y+a,ymax/2+x-b,15);
}
int main(void)
{
  /* request auto detection */
  int gdriver = DETECT, gmode, errorcode;
  int xmax, ymax,x,y,i;
  int r,a,b,d;
  printf("Enter the value radius and center ");
  scanf("%d%d%d",&r,&a,&b);
  clrscr();
  /* initialize graphics and local variables */
  initgraph(&gdriver, &gmode, "");

  /* read result of initialization */
  errorcode = graphresult();
  /* an error occurred */
  if (errorcode != grOk)
  {
     printf("Graphics error: %s\n", grapherrormsg(errorcode));
     printf("Press any key to halt:");
     getch();
     exit(1);
  }

  setcolor(getmaxcolor());
  xmax = getmaxx();
  ymax = getmaxy();
  x=xmax/2;
  y=ymax/2;
  d=1-r;
  for(i=0;i<xmax;i++)
   putpixel(i,y,15);
  for(i=0;i<ymax;i++)
   putpixel(x,i,15);
  x=0;
  y=r;
  while(y>=x)
   {
    if(d<0)
     {
      d+=2*x+3;
      x++;
     }
    else
     {
      d+=2*(x-y)+5;
      x++;
      y--;
     }
    plotcircle(x,y,a,b);
    delay(100);
   }
  /* clean up */
  getch();
  closegraph();
  return 0;
}

C/C++ Program to draw a line by DDA approach

//Program to draw a line by DDA approach
Source code
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
#include<dos.h>

int round(float y)
{
int y1;
float diff;
y1=y;
diff=y-y1;
if(diff>0.5)
 return(y1+1);
else
 return(y1);
}

int main(void)
{
  /* request auto detection */
  int gdriver = DETECT, gmode, errorcode;
  int xmax, ymax;
  int x,y,i,a;
  float x1,x2,y1,y2,x3,y3,m,m1,dy,dx,t=1;
  printf("Enter the value of x1,x2,y1,y2");
  scanf("%f%f%f%f",&x1,&x2,&y1,&y2);
  printf("enter 1 for left to right and 2 for right to left");
  scanf("%d",&a);
  clrscr();
  /* initialize graphics and local variables */
  initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

  /* read result of initialization */
  errorcode = graphresult();
  /* an error occurred */
  if (errorcode != grOk)
  {
     printf("Graphics error: %s\n", grapherrormsg(errorcode));
     printf("Press any key to halt:");
     getch();
     exit(1);
  }

  setcolor(getmaxcolor());
  xmax = getmaxx();
  ymax = getmaxy();
  dy=y2-y1;
  dx=x2-x1;
  m=dy/dx;
  x1=x1+xmax/2;
  x2=x2+xmax/2;
  y1=ymax/2-y1;
  y2=ymax/2-y2;
  for(i=0;i<ymax;i++)
   putpixel(xmax/2,i,15);
  for(i=0;i<xmax;i++)
   putpixel(i,ymax/2,15);
 if(a==1)
  {
   if((m<1&&m>-1))
    {
     y3=y1;
     for(x=x1;x<x2;x++)
      {
putpixel(x,round(y3),15);
delay(100);
y3=y3-m;
      }
    }
 else
  {
   x3=x1;
   m1=t/m;
   for(y=y1;y>y2;y--)
    {
     putpixel(round(x3),y,15);
     delay(100);
     x3=x3+m1;
    }
   }
 }
 else
   {
    if((m<1&&m>-1))
     {
      y3=y2;
      for(x=x2;x>x1;x--)
     {
    putpixel(x,round(y3),15);
    delay(100);
    y3=y3+m;
   }
  }
 else
  {
   x3=x2;
   m1=t/m;
   for(y=y2;y<y1;y++)
    {
     putpixel(round(x3),y,15);
     delay(100);
     x3=x3-m1;
    }
   }
 }
  /* clean up */
  getch();
  closegraph();
  return 0;
}

C/C++ Program to draw circle using second order mid point approach

//Program to draw circle using second order mid point approach
Source code
#include<dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void plotcircle(int x,int y,int a,int b)
{
int xmax=getmaxx();
int ymax=getmaxy();
putpixel(x+xmax/2+a,ymax/2-y-b,15);
putpixel(xmax/2+y+a,ymax/2-x-b,15);
putpixel(x+xmax/2+a,ymax/2+y-b,15);
putpixel(y+xmax/2+a,ymax/2+x-b,15);
putpixel(xmax/2-x+a,ymax/2-y-b,15);
putpixel(xmax/2-y+a,ymax/2-x-b,15);
putpixel(xmax/2-x+a,ymax/2+y-b,15);
putpixel(xmax/2-y+a,ymax/2+x-b,15);
}
int main(void)
{
  /* request auto detection */
  int gdriver = DETECT, gmode, errorcode;
  int xmax, ymax,x,y,i,de=3,dse;
  int r,a,b,d;
  printf("Enter the value radius and center ");
  scanf("%d%d%d",&r,&a,&b);
  dse=5-2*r;
  clrscr();
  /* initialize graphics and local variables */
  initgraph(&gdriver, &gmode, "");

  /* read result of initialization */
  errorcode = graphresult();
  /* an error occurred */
  if (errorcode != grOk)
  {
     printf("Graphics error: %s\n", grapherrormsg(errorcode));
     printf("Press any key to halt:");
     getch();
     exit(1);
  }

  setcolor(getmaxcolor());
  xmax = getmaxx();
  ymax = getmaxy();
  x=xmax/2;
  y=ymax/2;
  d=1-r;
  for(i=0;i<xmax;i++)
   putpixel(i,y,15);
  for(i=0;i<ymax;i++)
   putpixel(x,i,15);
  x=0;
  y=r;
  while(y>=x)
   {
    if(d<0)
     {
      d+=de;
      de+=2;
      dse+=2;
      x++;
     }
    else
     {
      d+=dse;
      de+=2;
      dse+=4;
      x++;
      y--;
     }
    plotcircle(x,y,a,b);
    delay(100);
   }
  /* clean up */
  getch();
  closegraph();
  return 0;
}