Thursday, April 26, 2018

Reverse words of a string

# include <stdio.h>
# include <conio.h>
# include <string.h>
main()
{
char str[50]="students love java, c++";
char st[50];
int i,j;
clrscr();
strrev(str);
puts(str);
for(i=0,j=0;str[i]!='\0';i++)
{
st[j++]=str[i];
if(str[i]==' ')
{
st[j]=NULL;
j=0;
printf("%s",strrev(st));
}
}
st[j]=NULL;
printf(" %s",strrev(st));
}

Wednesday, April 11, 2018

Monday, April 2, 2018

NPTEL Programming Assignments [Jumping Numbers,Digits,Amit and the Taxi]


Programming Assignment 3.1 : Jumping Numbers
One day Ajit got a strange feeling of jumping from one point to another on a number line (containing positive integers). The jumping from one point to another can only be done in one dimension. He will start from point 0 from which he will do a lot of jumps. He can only jump in a specific sequence: 1-jump, 2-jump, 3-jump, 1jump, 2-jump, 3-jump, 1-jump, and so on. (1->2->3->1->2->3->1...) 1-jump means that if Ajit is at point x on the number line, he will jump to the point x+1. 2-jump means that id Ajit is at point x on the number line, he will jump to the point x+2.
3-jump means that id Ajit is at point x on the number line, he will jump to the point x+3.

Write a program in C which given the input point a outputs whether Ajit can reach this point a after some number of jumps or not.

Input: The first line contains a single integer a denoting the point Ajit asks about.
Output: Output "YES" without a quotes if Ajit can arrive at point a or "NO" without a quotes otherwise. Example1: Input: 3 Output: YES He can take jump sequence (1­>2) to reach point 3.
Input: 2 Output: NO From 0 he can take 1 jump to reach point 1 but after that he can take only 2 jumps which will lead him to point 3. Jump sequence (1­>2).
Constraints:  0<=a<=1018
# include <stdio.h>
int main()
{
  int i=0,j[3]={1,2,3};
  int n,x=0;
  scanf("%d",&n);
  while(x<=n)
  {
    x+=j[i];

    if(x==n)
    {
      printf("YES");
      break;
    }
    if(i==2)
      i=0;
      else
      i++;
    }
  if(x!=n)
      printf("NO");

}
Programming Assignment 3.3 : Digits
You are provided with a number N containing only digits "1" and "0". The idea is to make the number to have all the digits same. For that, you can only change exactly one digit, either "0" to "1" or "1" to "0". If it is possible to make all the digits same by flipping exactly one digit then print "YES" else "NO". Input: A number made up of only digits "1" and "0". Output: Print "YES" if it is possible to make all the digits same else "NO" without quotes. Example: Input: 101 Output: YES Input: 11 Output: NO
# include <stdio.h>
int main()
{
  int n,d,o=0,z=0;
  scanf("%d",&n);
   
   while(n>0)
  {
       
     if(d=n%10==0)
       z++;
    else
      o++;
    
     n/=10;
  }
  if(z==1 || o==1)
  {
    printf("YES");
  }
  else
  {
    printf("NO");
  }
  -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Programming Assignment 3.4: Amit and the Taxi
Amit is working in a company such that he has to travel to some restaurant every day which is at N meters distance from his home. Amit has two choices, either to take the restaurant's taxi (where he wants to visit) or he can walk to the restaurant. Whenever he walks, he walks with a velocity of V1 m/s. The taxi, on the other hand, moves with a velocity of V2 m/s. Whenever he calls for the taxi, the taxi first travels a distance of N meters from the restaurant to his home and then from home, it travels back to the restaurant. The taxi crosses a total distance of N meters while going from home to the restaurant, on the other hand, he has to cover a distance of sqrt(2)*N (because of some staircases) when he walks. Write a program in C to help Amit to decide whether he should use the taxi or he should walk so that his travel time is minimized. Input: Three space-separated integers N, V1, V2 Output: Output a string either "Taxi" or "Walk" depending on the answer Example: Input: 5 10 15 Output: Taxi Constraints: 1 <= N, V1, V2 <= 100
# include <stdio.h>
# include <math.h>
int main(int argc,char*argv[])
{
  int N,V1,V2;
  float w,c;
  scanf("%d%d%d",&N,&V1,&V2);
 
  w=((float)N/V1)+sqrt(2)*N;
  c=((float)N/V2);
    if(w>c)
    printf("Walk");
    else
    printf("Taxi");
   
}


Wednesday, March 28, 2018

Finding the length of the longest increasing subsequence in arr[] of size n in ascending order ( NPTEL Q )

# include <stdio.h>
# include <conio.h>
/* lis() returns the length of the longest increasing
  subsequence in arr[] of size n
array[5] elements are  3,10,2,1,30

the longest order order is    3,10,30
 */
int lis( int arr[], int n )
{
    int *lis, i, j, max = 0;
    lis = (int*) malloc ( sizeof( int ) * n );

    /* Initialize LIS values for all indexes */
    for (i = 0; i < n; i++ )
lis[i] = 1;

    /* Compute optimized LIS values in bottom up manner */
    for (i = 1; i < n; i++ )
for (j = 0; j < i; j++ )
    if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;

    /* Pick maximum of all LIS values */
    for (i = 0; i < n; i++ )
if (max < lis[i])
    max = lis[i];

    /* Free memory to avoid memory leak */
    free(lis);

    return max;
}

main()
{
int A[]={3,10,2,1,30};
clrscr();

printf("\n%d",lis(A,5));

}

Thursday, March 22, 2018

NPTEL(C & DS ) : ROBOT CAN MOVE STEP 1,2 OR 3 AND PRINT YES OR NO , THAT THE ROBOT CAN MOVE THE GIVEN DISTANCE

/* ROBOT CAN MOVE STEP 1,2 OR 3 AND PRINT YES OR NO , THAT THE
ROBOT CAN MOVE THE GIVEN DISTANCE */

# include <stdio.h>
main()
{
  int i=0,j[3]={1,2,3};
  long int n,x=0;
  scanf("%ld",&n);
  while(x<=n)
  {
    x+=j[i];

    if(x==n)
    {
      printf("YES");
      break;
    }
    if(i==2)
      i=0;
      else
      i++;
    }
  if(x!=n)
      printf("NO");

}

Friday, February 16, 2018

Maching Ball Game By R.Venkata Subbaiah.

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <time.h>
#include <stdlib.h>

/* This program shows how to pick up the scan codes from a keyboard */
/* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/

#define PAGE_UP     73
#define HOME        71
#define END         79
#define PAGE_DOWN   81
#define UP_ARROW    72
#define LEFT_ARROW  75
#define DOWN_ARROW  80
#define RIGHT_ARROW 77
#define F1          59
#define F2          60
#define F3          61
#define F4          62
#define F5          63
#define F6          64
#define F7          65
#define F8          66
#define F9          67
#define F10         68






void main()
{
 char msg[100];
 char KeyStroke;
  time_t t;
 int gd = DETECT, gm;
 int i, x, y, flag=0;
 int r=20;

 int VerSpeed=1;
 int HorSpeed=1;

 initgraph(&gd, &gm, "C:\\Turboc3\\BGI");  /* init graph */
 /* get mid positions in x and y-axis */
 x = getmaxx()/2;
 y = getmaxy()/2;


clock_t startTime = clock();

do
{
     setcolor(RED);
     setfillstyle(SOLID_FILL, RED);
     //floodfill(x, y, RED);
     circle(x, y, r);


     if(x==50 && y==50)
     {
     printf("\nPerfect Matched, Game Over \nGame Time %.2lf Secs.\nPres ESC to close..",clock() / (double) CLOCKS_PER_SEC);
     }

     /* target circle */
     setcolor(WHITE);
     circle(50, 50, r);

     /*line */
     setlinestyle(SOLID_LINE, 2, 1);

     line(0,350,700,350);
     /* move the C.P. to the center of the screen */
      moveto(getmaxx()-200,getmaxy()-100);

      sprintf(msg,"Speed Ver:%d Hor:%d",VerSpeed,HorSpeed);
      outtext(msg);
      moveto(getmaxx()-200,getmaxy()-85);
      outtext("PgUp   : Inc Ver Speed");
      moveto(getmaxx()-200,getmaxy()-70);
      outtext("PgDown : Dec Ver Speed");

      moveto(getmaxx()-200,getmaxy()-55);
      outtext("Home   : Inc Hor Speed");
      moveto(getmaxx()-200,getmaxy()-40);
      outtext("End    : Dec Hor Speed");

      moveto(getmaxx()-200,getmaxy()-25);
      time(&t);
      sprintf(msg,"Date : %s",ctime(&t));
      outtext(msg);

      moveto(getmaxx()-200,getmaxy()-10);
      sprintf(msg,"Colck Secs : %.2lf",clock() / (double) CLOCKS_PER_SEC);
      outtext(msg);


KeyStroke = getch();
if (KeyStroke == 0)
{
KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke
/* clears screen */
cleardevice();
switch (KeyStroke)
{
case UP_ARROW:
y-=VerSpeed;
circle(x, y, r);
break;
case DOWN_ARROW:
y+=VerSpeed;
circle(x, y, r);
break;
case LEFT_ARROW:
x-=HorSpeed;
circle(x, y, r);
break;
case RIGHT_ARROW:
x+=HorSpeed;
circle(x, y, r);
break;
case PAGE_UP:
VerSpeed+=1;
break;
case PAGE_DOWN:
VerSpeed-=1;
if(VerSpeed==-1)
VerSpeed=0;

break;
case HOME:
HorSpeed+=1;
break;
case END:
HorSpeed-=1;
if(HorSpeed==-1)
HorSpeed=0;
break;

}
}
else
//moveto(getmaxx()-100,getmaxy()-70);
cprintf("Use Arrow KeyStroke");

}
while (KeyStroke != 27); // 27 = Escape key

/* timee display */
/*
clock_t endTime = clock();
clock_t clockTicksTaken = endTime - startTime;
double timeInSeconds = clockTicksTaken / (double) CLOCKS_PER_SEC;
printf("\n%lf",timeInSeconds);
*/

getch();
closegraph();
}
--------------------------------------------------------------------------------------------------------------------------
Output Screens
--------------------------------------------------------------------------------------------------------------------------