Wednesday, September 21

Check a Number in parent process and pass result to Child Process

#include<stdio.h>
#include<unistd.h>



main()
{
int fd[2],f,i,num,flag=1;
pipe(fd);
f=fork();

if(f>0)
{
printf("PARENT PROCESS..");
printf("\nEnter a number : ");
scanf("%d",&num);



if(num==1)flag=0;for(i=2;i<=(num/2);i++)
if(num%i==0)
{
flag=0;
break;
}

close(fd[0]);
write(fd[1],&flag,sizeof(flag));



}
else if (f==0)
{

printf("\n\nCHILD PROCESS.");close(fd[1]);
read(fd[0],&flag,sizeof(flag
));

if(flag==1)printf(" \n\tIt is a prime number..\n");
else printf("\n\t It is not a prime number..\n");
}



}

Monday, September 19

Code to Access GPS in Android Phones

GET !!! SET !! GO..!!!!!!!!

Location Based Services (LBS) have become very popular in past few years after the advent of GPS enabled smartphone devices . In this post, we shall see how a LBS application for Android works.

Simply speaking map navigation is possible in  following ways:
  1. Using the GPS device in built in mobile-    Easier and precise.
  2. Using the ID of the Cell that the user is currently served by



How to code?
We can give dummy values as GPS co-ordiantes for a location(i.e., Longitude and Latitude) .Another way to emulate location is to including .gpx files. Although it is not very flexible , it can give better performance.

Pre-requisite

First include this code fragment in AndroidManifest.xml to access GPS:

 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
<uses-permission android:name="android.permission.INTERNET"/>




Application Code


 
import com.GPS_project.android.lbs.R;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class gps extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

protected LocationManager locationManager;

protected Button retrieveLocationButton;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

retrieveLocationButton = (Button)findViewById(R.id.retrieve_location_button);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

 locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);

retrieveLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});

}

protected void showCurrentLocation() {

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());

Toast.makeText(gps.this, message,
Toast.LENGTH_LONG).show();
}
if (location == null)
{
Toast.makeText(gps.this, "No Location Available ", Toast.LENGTH_LONG).show();
}

}

private class MyLocationListener implements LocationListener {

public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(gps.this, message, Toast.LENGTH_LONG).show();
}

public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(gps.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}

public void onProviderDisabled(String s) {
Toast.makeText(gps.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}

public void onProviderEnabled(String s) {
Toast.makeText(gps.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}

}

}



Saturday, September 17

Print a string without using any semicolon

Write a program to print a  string without using any semicolon on the program:


#include<stdio.h>
main()
{
      if(printf("Hello.."))
      {
      }

}




I want to know how this program can modified to print any string accepted through Keyboard..!!

If anyone know, feel free to comment it here..!!  :):):)

Letter Pattern

Write a program to print the following
         A
       B B
      CC CC
   DDD DDD





#include<stdio.h>
main()
{
      int i,j,n;
      printf("Enter Limit..  :  ");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
      for(j=0;j<(n-i);j++)
      printf(" ");
      if(i==0)putchar(65);
      for(j=0;j<i;j++)putchar(65+i);      printf(" ");
        for(j=0;j<i;j++)putchar(65+i);
        printf("\n");
        }
        getch();
        }
     

C program to print a Number pattern

Write a program to print the following pattern:
0

1 1
2 3 5
8 13 21 34



Hi buddy, Jst analyse the no.s in order, 0,1,1,2,3,5,8,13,21,34....
Yess!!!! this is a Fibonacci series.

0+1=1
1+1=2
1+2=3
2+3=5... and so on

Next requirement is to generate pattern. First line has 1 element, second line has 2 elements and so on. And total number of elements in whole pattern will be N*(N+1)/2 , if there are N number of lines.





#include<stdio.h>
main()
{
      int n,i,j,k=0;
      int a[20];
      printf("Enter the lmit..:   ");
      scanf("%d",&n);
      a[0]=0;
      a[1]=1;
      for(i=2;(i<n*(n+1)/2);i++)
      a[i]=a[i-1]+a[i-2];
      for(j=0;j<=n;j++)
     {
      for(i=0;i<j;i++)
      printf("%d\t",a[k+i]);
      k+=i;
      printf("\n");
      }
      getch();
}

C program to Print a number table

Write a program to display the following format:
Nosum
11
23
36
410
515

Logic:

First column contains natural numbers serially..
second column contains  ( Current first column element+ previous second column element)
2+1=3
3+3=6
4+6=10
5+10=15




#include<stdio.h>
main()
{
      int j=0,z,i,n;
      printf("Enter the Limit...  :  ");
      scanf("%d",&n);
      printf("\n----------------\n
printf("no\tsum");
printf("\n------------------\n");
     
      for(i=1;i<=n;i++)
      {
      printf("\n%d",i);
      j+=i;
      printf("\t%d",j);
      }
      getch();
      }
     
     
     





Reverse words in a string using Recursion

Write a program to perform the following to any input string:
     
       Input : Structure is a derived datatype
      output: erutcurtS si a devired epytatad

My Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char line[20],word[10];
    int i,j=0,len;
    printf("Entera string :  ");
    gets(line);
    len=strlen(line);
    for(i=0;i<len;i++)
    {
    while(line[i]!=' '&&i<len)
    word[j++]=line[i++];
    for(j--;j>=0;j--)
    printf("%c",word[j]);
    printf(" ");

    j=0;
    }
    getch();
    return 1;
}


I hope this code was useful for u!!
Have a better program for ths prblm??
Feel free to comment it here..!!  :):):)