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
--------------------------------------------------------------------------------------------------------------------------



Wednesday, January 24, 2018

C Simple Puzzles

-------------------------------------------------------------------
How to print numbers from 1 to N without using any semicolon in C.
/ A recursive C program to print all numbers from 1
// to N without semicoolon
#include<stdio.h>
#define N 10
int main(int num)
{
   if (num <= N && printf("%d ", num) && main(num + 1))
    {
    }   
}
------------------------------------------------------------------
Find sum of two numbers without using any operator
int main()

{
printf("sum=%d",printf("%*c%*c",3,' ',4,' '));

return 0;

}
------------------------------------------------------------------
Write a one line C function to round floating point numbers
1.6
Algorithm: roundNo(num)
1. If num is positive then add 0.5.
2. Else subtract 0.5.
3. Type cast the result to int and return.

Example:
num = 1.67, (int) num + 0.5 = (int)2.17 = 2
num = -1.67, (int) num – 0.5 = -(int)2.17 = -2

Implementation:

/* Program for rounding floating point numbers */
# include<stdio.h>
int roundNo(float num)
{
    return num < 0 ? num - 0.5 : num + 0.5;
}

int main()
{
    printf("%d", roundNo(-1.777));
    getchar();
    return 0;
}
Run on IDE
Output: -2

Time complexity: O(1)
Space complexity: O(1)

Tuesday, January 23, 2018

Find Knight Propability of moves when inputing x,y positions. (Company Hakuna Matato)

/*   User input :  X and Y positions of Knight
      Out Put     :   2 steps
 */

# include <stdio.h>
# include <conio.h>
/* possible moves   total 8 moves can be possible*/
int xMove[8] = {  2, 1, -1, -2, -2, -1,  1,  2 };
int yMove[8] = {  1, 2,  2,  1, -1, -2, -2, -1 };
int prop_count(int x,int y)
{
int mcount=0;
int s;
printf("\nKnight Positions are\n");
for(s=0;s<=7;s++)
{
if( x+xMove[s]>=0 && x+xMove[s]<=7)
if( y+yMove[s]>=0 && y+yMove[s]<=7)
{
printf("\n%d,%d",x+xMove[s],y+yMove[s]);
mcount++;
}
}
return mcount;
}
main(){
int board[8][8];
int x,y;
clrscr();
printf("\nboard [0,0 is start and 7,7 is end]\nEnter X and Y position of?");
scanf("%d%d",&x,&y);
printf("\nTotal count of Moves : %d",prop_count(x,y));
}

Monday, January 22, 2018

Artificial Intelligence vs Machine Learning vs Deep Learning

First coined in 1956 by John McCarthy, AI involves machines that can perform tasks that are characteristic of human intelligence. While this is rather general, it includes things like planning, understanding language, recognizing objects and sounds, learning, and problem solving.
We can put AI in two categories, general and narrow. General AI would have all of the characteristics of human intelligence, including the capacities mentioned above. Narrow AI exhibits some facet(s) of human intelligence, and can do that facet extremely well, but is lacking in other areas. A machine that’s great at recognizing images, but nothing else, would be an example of narrow AI.
Artificial Intelligence is the broader concept of machines being able to carry out tasks in a way that we would consider “smart”.
Machine Learning is a current application of AI based around the idea that we should really just be able to give machines access to data and let them learn for themselves.
Deep learning is one of many approaches to machine learning. Other approaches include decision tree learning, inductive logic programming, clustering, reinforcement learning, and Bayesian networks, among others.
Deep learning was inspired by the structure and function of the brain, namely the interconnecting of many neurons. Artificial Neural Networks (ANNs) are algorithms that mimic the biological structure of the brain.
AI and IoT are Inextricably Intertwined
I think of the relationship between AI and IoT much like the relationship between the human brain and body.
Our bodies collect sensory input such as sight, sound, and touch. Our brains take that data and makes sense of it, turning light into recognizable objects and turning sounds into understandable speech. Our brains then make decisions, sending signals back out to the body to command movements like picking up an object or speaking.
All of the connected sensors that make up the Internet of Things are like our bodies, they provide the raw data of what’s going on in the world. Artificial intelligence is like our brain, making sense of that data and deciding what actions to perform. And the connected devices of IoT are again like our bodies, carrying out physical actions or communicating to others.
Unleashing Each Other’s Potential
The value and the promises of both AI and IoT are being realized because of the other.
Machine learning and deep learning have led to huge leaps for AI in recent years. As mentioned above, machine learning and deep learning require massive amounts of data to work, and this data is being collected by the billions of sensors that are continuing to come online in the Internet of Things. IoT makes better AI.
Improving AI will also drive adoption of the Internet of Things, creating a virtuous cycle in which both areas will accelerate drastically. That’s because AI makes IoT useful.

LG Online Code examination date : 19-1-2018

# include <stdio.H>
# include <conio.H>

/* Robot is at position 0 and move the robot to a destination x(x>0).
Robot moves in 1,2,3,4,5 steps.Find the minimum number of steps to reach
the destination.
Ex:destination x=12
output: 3 steps     */

main(){
int i,dist=13;
int steps[5]={1,2,3,4,5};
int moves;
clrscr();
for( i=sizeof(steps)/2-1 ; i>=0;i--)
{
if(dist%steps[i]==0)
{
printf("\n%d step of %d moves",steps[i],dist/steps[i]);
break;
}
else
{
moves=dist/steps[i];
dist=dist%steps[i];
if(moves!=0)
printf("\n%d step of %d moves",steps[i],moves);
}
}
}

Sunday, January 21, 2018

LG soft Online Coding question : Dt 19-12-2018

/* Assign 1 to 26 for alpphabets then multiply  each char of each string and do sum for s1 and s2 . if the result is equal print CHOOSEN other wise NOT CHOOSEN

s1="AB"  , s2="AB"
o/p CHOOSEN
 x=1*2 ->2
y=1*2->2
if x==y    print CHOOSEN other wise  "NOT CHOOSEN"
*/

# include <stdio.h>
void solution(char*S,char*T)    {
int x=1,y=1
for(i=0;S[i]!=0;i++)
{
x*=S[i]-64;
}
for(i=0;T[i]!=0;i++)
{
y*=T[i]-64;
}

if((x%47)==(y%47))
printf("CHOOSEN");
else
printf("NOT CHOOSEN");

}

main(){
char s1[50],s2[50];
solution("ABC","ABC");
}

Friday, January 19, 2018

Coding test Question : read start and end numbers and print nos whose digits are unique

/*  i/p  :   (120,130)    o/p :  120 123 124 125 126 127 128 129 130 */

#include<stdio.h>
void main()
{
 int temp,sr,er,i,a[5],j,k,l,flag=0;
 clrscr();
 scanf("(%d,%d)",&sr,&er);
 for(i=sr;i<=er;i++)
 {
  temp=i;
  j=flag=0;
  while(temp!=0)
  {
   a[j]=temp%10;
   temp=temp/10;
   j++;
  }
  for(k=0;k<j;k++)
  {
   for(l=k+1;l<j;l++)
   {
    if(a[k]==a[l])
    {
     flag=1;
     break;
    }
   }
   if(flag==1)
   {
    break;
   }
  }
  if(flag!=1)
   printf("%d ",i);
 }
}