Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Monday, 23 January 2017

MATLAB PROGRAM TO EVALUATE QUADRATIC EQUATION(Ax^2+Bx+C=0)

prompt = 'Enter A'
A= input(prompt) % Read A from keyboard
prompt = 'Enter B'
B= input(prompt) % Read B from keyboard
prompt = 'Enter C'
C= input(prompt) % Read C from keyboard


quat=[A,B,C]      
   
x=roots(quat)  % roots is a function to evaluate polynomial

Saturday, 20 October 2012

Communication between two unrelated process using messege queue

MSGQ1.c 

#include<stdio.h>
#include<sys/msg.h>
#include<string.h>
#include<stdlib.h>
struct msgbuf
{
long mtype;
char mtext[30];
}ptr;
int main()
{
int i=0;
int id=msgget(52,0666|IPC_CREAT);
if(id>=0)
printf("Msg Q created sucessfully\n");
else
printf("Not created\n");
printf("Enter the msg type:\n");
scanf("%ld",&(ptr.mtype));

printf("Enter the msg\n");
scanf("%s",ptr.mtext);
int p=msgsnd(id,&ptr,(sizeof(ptr.mtext)),0);
if(p>=0)
printf("Msg send sucessfully\n");
else
printf("Msg not send\n");
}




MSGQ2.C



#include<stdio.h>
#include<sys/msg.h>
struct msgbuf
{
long mtype;
char mtext[30];
}ptr;
int main()
{

int id=msgget(52,0666|IPC_CREAT);
if(id>=0)
printf("Success\n");
printf("Enter the msgtype\n");
scanf("%ld",&ptr.mtype);

int p=msgrcv(id,&ptr,30,(ptr.mtype),0);
if(p>=0)
{
printf("Msg read sucessfully\n");
printf("Msg is:%s\n",ptr.mtext);
}
else
printf("Error\n");
}


Sample output


msgq1.c

Enter the msg type:
2
Enter the msg
kkk
Msg send sucessfully


msgq2.c

Success
Enter the msgtype
2
Msg read sucessfully
Msg is:kkk





Sunday, 7 October 2012

Two unrelated programs uses shared memory

First program


#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
int main()
{

int id=shmget(17,50,0666|IPC_CREAT);


if(id>=0)
{
printf("\nShared memory created with id:%d\n",id);
}
char *adr=shmat(id,NULL,0);
if(adr>=0)
printf("Memory attached\n");
printf("Enter a string\n");
char str[10];

scanf("%s",str);
strncpy(adr,str,sizeof(str));



}



Second program

#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
int main()
{

int id=shmget(17,0,0666|IPC_CREAT);
char str[10];

if(id>=0)
{
printf("\nShared memory with id:%d\n is accessed",id);
}
char *adr=shmat(id,NULL,0);
if(adr>=0)
printf("\nMemory attached\n");
printf("\nThe string is\n%s",adr);
}


Sample output:

cc first.c
./a.out

Shared memory created with id:360454
Memory attached
Enter a string
Hello

cc second.c
./a.out

Shared memory with id:360454
 is accessed
Memory attached

The string is
hello


Wednesday, 2 May 2012

Sorting of matrix elements-C++ program


#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
    int a[20][20],n,b[20],m,i,j,k=0,t;
    cout<<"Enter the order\n";
    cin>>m>>n;
    cout<<"Enter the elements\n";
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
    cin>>a[i][j];
    b[k++]=a[i][j];
     }
     }
     for(i=0;i<(m*n);i++)
     {
     t=b[i];
     j=i;
     while(j>0 && b[j-1]>=t)
     {
               b[j]=b[j-1];
               j--;
               }
               b[j]=t;
               }
             
               cout<<"\n";
               cout<<"Before sorting\n";
               for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
   
    cout<<a[i][j]<<"\t";
    cout<<"\n";
}
               cout<<"After sorting\n";
               k=0;
    for(i=0;i<m;i++)
   
    for(j=0;j<n;j++)
   
    a[i][j]=b[k++];
   
   
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
   
    cout<<a[i][j]<<"\t";

cout<<"\n";
}

    getch();
}





Sample Output
Enter the order
 3 3
9
8
7
4
5
2
1
3
6
Before sorting
9   8   7
4   5   2
1   3   6
After sorting
1   2   3
4   5   6
7   8   9



No third variables and pointers-C++ swapping program


#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
    int a,b;
    cout<<"Enter two number:\t";
    cin>>a>>b;
    cout<<"\nBefore swapping:\t";
    cout<<a<<"\t"<<b;
   
    a=a+b;
    b=a-b;
    a=a-b;
    cout<<"\nAfter swapping:\t";
    cout<<"\t"<<a<<"\t"<<b;
    getch();
}

Monday, 26 December 2011

Helicopter- C Graphics program

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void heli();

int main()
{
    initwindow(2000,2000, "First Sample");
    heli();
    getch();
}

void heli()
    {
    int xf,yf;
    int x1,y1,x2,y2,x3,y3,x4,y4;
    int j=0,i=0;
    float t;      
    t=3.14/180;
    xf=450;
    yf=160;
    while(1)
      {
      for(i=0;i<360;i++,j++)
      {
                        setcolor(15);
                        x1=xf+(175-xf)*cos(t*i)-(150-yf)*sin(t*i);
                        y1=yf+(175-xf)*sin(t*i)+(150-yf)*cos(t*i);
                        x2=xf+(700-xf)*cos(t*i)-(150-yf)*sin(t*i);
                        y2=yf+(700-xf)*sin(t*i)+(150-yf)*cos(t*i);
                        x3=xf+(700-xf)*cos(t*i)-(175-yf)*sin(t*i);
                        y3=yf+(700-xf)*sin(t*i)+(175-yf)*cos(t*i);
                        x4=xf+(175-xf)*cos(t*i)-(175-yf)*sin(t*i);
                        y4=yf+(175-xf)*sin(t*i)+(175-yf)*cos(t*i);
                       
                       
                        line(x1+j,y1,x2+j,y2);
                        line(x2+j,y2,x3+j,y3);
                        line(x3+j,y3,x4+j,y4);
                        line(x4+j,y4,x1+j,y1);
                       
                        line(450+j,160,450+j,200);
                        line(450+j,200,250+j,215);
                        line(250+j,215,250+j,250);
                        line(250+j,225,100+j,225);
                        line(100+j,225,100+j,175);
                        line(100+j,225,100+j,235);
                        line(100+j,235,250+j,235);
                       
                       
                        line(250+j,250,425+j,300);
                        line(425+j,300,475+j,300);
                        line(450+j,300,435+j,315);
                        line(450+j,300,465+j,315);
                        line(475+j,300,650+j,250);
                       
                        line(450+j,200,650+j,215);
                        line(650+j,215,650+j,250);
                       
                       
                        setcolor(0);
                        x1=xf+(175-xf)*cos(t*i)-(150-yf)*sin(t*i);
                        y1=yf+(175-xf)*sin(t*i)+(150-yf)*cos(t*i);
                        x2=xf+(700-xf)*cos(t*i)-(150-yf)*sin(t*i);
                        y2=yf+(700-xf)*sin(t*i)+(150-yf)*cos(t*i);
                        x3=xf+(700-xf)*cos(t*i)-(175-yf)*sin(t*i);
                        y3=yf+(700-xf)*sin(t*i)+(175-yf)*cos(t*i);
                        x4=xf+(175-xf)*cos(t*i)-(175-yf)*sin(t*i);
                        y4=yf+(175-xf)*sin(t*i)+(175-yf)*cos(t*i);
                       
                        line(x1+j,y1,x2+j,y2);
                        line(x2+j,y2,x3+j,y3);
                        line(x3+j,y3,x4+j,y4);
                        line(x4+j,y4,x1+j,y1);
                       
                        line(450+j,160,450+j,200);
                        line(450+j,200,250+j,215);
                        line(250+j,215,250+j,250);
                        line(250+j,225,100+j,225);
                        line(100+j,225,100+j,175);
                        line(100+j,225,100+j,235);
                        line(100+j,235,250+j,235);
                       
                       
                        line(250+j,250,425+j,300);
                        line(425+j,300,475+j,300);
                        line(450+j,300,435+j,315);
                        line(450+j,300,465+j,315);
                        line(475+j,300,650+j,250);
                        line(450+j,200,650+j,215);
                        line(650+j,215,650+j,250);
                        }
                        }
                       
     
   
   
    
     }

Friday, 23 December 2011

Vehicles moving on road- C graphics program

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
 int main()
{
    int i,j,k,l,m,n,p;
    initwindow(600, 400, "First Sample");

  line(0,160,700,160);
  line(0,275,700,275);
    for(i=0,j=500;i<500;i=i+2,j=j-3)
    {               
                     setcolor(15);
                      rectangle(25+j,225,75+j,245);
                      line(25+j,235,75+j,235);
                      circle(38+j,250,5);
                      circle(68+j,250,5);
                      delay(5);
                      setcolor(0);
                      rectangle(25+j,225,75+j,245);
                      line(25+j,235,75+j,235);
                      circle(38+j,250,5);
                      circle(68+j,250,5);
                      setcolor(15);
                      rectangle(25+i,165,75+i,200);
                      rectangle(75+i,175,85+i,200);
                      circle(38+i,205,5);
                      circle(68+i,205,5);
                      delay(5);
                      setcolor(0);
                      rectangle(25+i,165,75+i,200);
                      rectangle(75+i,175,85+i,200);
                      circle(38+i,205,5);
                      circle(68+i,205,5);
                      }
                      setcolor(15);
                      rectangle(25+i,165,75+i,200);
                      rectangle(75+i,175,85+i,200);
                      circle(38+i,205,5);
                      circle(68+i,205,5);
 getch();
}


Man walking on a road -C++ program

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void man();
int main()
{
   
    initwindow(1050,1050 , "First Sample");
    man();
   
    getch();
}
void man()
{
     float t=3.14/180,j=0.0;
     int i,p,k=0,l,x1,y1,x2,y2,xf,yf,x,y,m=0; xf=201;yf=399;
     while(1)
     {
     for(i=0,j=0;(i<70||j<45);i++,j=j+1,k++)
     {
     cleardevice();
     line(0,400,1050,400);
     line(0,600,1050,600);
     line(200+k,400,200+k,200);
     circle(200+k,170,30);
     circle(225+k,160,3);
     x=199;y=249;
     x2=x+(280-x)*sin(t*j)-(300-y)*cos(t*j);
     y2=y+(280-x)*cos(t*j)+(300-y)*sin(t*j);
     line(200+k,250,x2+k,y2);
     x1=x+(280-x)*cos(t*j)-(300-y)*sin(t*j);
     y1=y+(280-x)*sin(t*j)+(300-y)*cos(t*j);
     line(200+k,250,x1+k,y1);
     x2=xf+(280-xf)*sin(t*i)-(500-yf)*cos(t*i);
     y2=yf+(280-xf)*cos(t*i)+(500-yf)*sin(t*i);
     line(200+k,400,x2+k,y2);
     x1=xf+(280-xf)*cos(t*i)-(500-yf)*sin(t*i);
     y1=yf+(280-xf)*sin(t*i)+(500-yf)*cos(t*i);
     line(200+k,400,x1+k,y1);
     delay(3);
     }
     }
     }

Wednesday, 7 December 2011

Simple Analog Clock- C program

This is very simple program for simple clock.Here the use of cleardevice() is eliminated because cleardevice() cause blinking of screen.Several steps of program is repeated with the color black -which is similar to clearsdevice() and avoids blinking.
       

                                                                    ANALOG CLOCK


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void circles();
 int main()
{
    int xf,yf,x1,y1,x2,y2,k=0,x3,y3,x4,y4;
    float t,i=0.0,j=0.0;
    initwindow(1050, 1050, "First Sample");
       xf=249;
       yf=251;
       t=3.14/180;
       for(;;i=i+0.2,j=j+0.6,k=k+6)
        {
            setcolor(0);
            circles();
                
                
        x1=xf+(250-xf)*cos((i-0.2)*t)-(250-yf)*sin((i-0.2)*t);
        y1=yf+(250-xf)*sin((i-0.2)*t)+(250-yf)*cos((i-0.2)*t);
        x2=xf+(250-xf)*cos((i-0.2)*t)-(350-yf)*sin((i-0.2)*t);
        y2=yf+(250-xf)*sin((i-0.2)*t)+(350-yf)*cos((i-0.2)*t);
       
        line(x1,y1,x2,y2);
       
        x1=xf+(250-xf)*cos((j-0.6)*t)-(250-yf)*sin((j-0.6)*t);
        y1=yf+(250-xf)*sin((j-0.6)*t)+(250-yf)*cos((j-0.6)*t);
        x2=xf+(250-xf)*cos((j-0.6)*t)-(400-yf)*sin((j-0.6)*t);
        y2=yf+(250-xf)*sin((j-0.6)*t)+(400-yf)*cos((j-0.6)*t);
       
        line(x1,y1,x2,y2);
       
       
        x1=xf+(250-xf)*cos((k-6)*t)-(250-yf)*sin((k-6)*t);
        y1=yf+(250-xf)*sin((k-6)*t)+(250-yf)*cos((k-6)*t);
        x2=xf+(250-xf)*cos((k-6)*t)-(450-yf)*sin((k-6)*t);
        y2=yf+(250-xf)*sin((k-6)*t)+(450-yf)*cos((k-6)*t);
       
        line(x1,y1,x2,y2);
       
        setcolor(12);
        circles();
       
        x1=xf+(250-xf)*cos(i*t)-(250-yf)*sin(i*t);
        y1=yf+(250-xf)*sin(i*t)+(250-yf)*cos(i*t);
        x2=xf+(250-xf)*cos(i*t)-(350-yf)*sin(i*t);
        y2=yf+(250-xf)*sin(i*t)+(350-yf)*cos(i*t);
       
        line(x1,y1,x2,y2);
       
        x1=xf+(250-xf)*cos(j*t)-(250-yf)*sin(j*t);
        y1=yf+(250-xf)*sin(j*t)+(250-yf)*cos(j*t);
        x2=xf+(250-xf)*cos(j*t)-(400-yf)*sin(j*t);
        y2=yf+(250-xf)*sin(j*t)+(400-yf)*cos(j*t);
       
        line(x1,y1,x2,y2);
       
        x1=xf+(250-xf)*cos(k*t)-(250-yf)*sin(k*t);
        y1=yf+(250-xf)*sin(k*t)+(250-yf)*cos(k*t);
        x2=xf+(250-xf)*cos(k*t)-(450-yf)*sin(k*t);
        y2=yf+(250-xf)*sin(k*t)+(450-yf)*cos(k*t);
       
        line(x1,y1,x2,y2);
       delay(850);
       
       
        }
getch();
}
 void circles()
 {
                 circle(250,250,1.5);
                 circle(250,250,2);
                 circle(250,250,3);
                 circle(250,250,4);
                 circle(250,250,225);
                 circle(30,250,4);
                 circle(471,250,4);
                 circle(250,29,4);
                 circle(250,471,4);
 }   

Tuesday, 6 December 2011

Simple Fan implementation-C program

This is simple program that implements a fan.
It just gives an idea,you can modify by filling color  and additional  drawings etc.
This graphics progam is written  in Dev C++ so it needs slight modifications to  run in Turbo C++ or C etc.



                                                                    FAN
                 


 #include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>

void fan(int );

int main()
{
    initwindow(500, 500, "First Sample");
    fan(100);
    getch();
}

void fan(int p)
    {
    int xf,yf;
    int x1,y1,x2,y2,x3,y3,x4,y4;
    int j=0,i;
    float t;
      
       setcolor(12);
       xf=150;                        //Center of circle
       yf=150;
       t=3.14/180;
       while(j==0 )
           {   
                    for(;;i++)
       {
           cleardevice();
           circle(150,150,25);
           circle(150,150,1);
           line(150,126,150,100);
          
       x1=xf+(175-xf)*cos(120*t+i)-(130-yf)*sin(120*t+i);
       y1=yf+(175-xf)*sin(120*t+i)+(130-yf)*cos(120*t+i);
       x2=xf+(275-xf)*cos(120*t+i)-(130-yf)*sin(120*t+i);
       y2=yf+(275-xf)*sin(120*t+i)+(130-yf)*cos(120*t+i);
       x3=xf+(175-xf)*cos(120*t+i)-(150-yf)*sin(120*t+i);
       y3=yf+(175-xf)*sin(120*t+i)+(150-yf)*cos(120*t+i);
       x4=xf+(275-xf)*cos(120*t+i)-(150-yf)*sin(120*t+i);
       y4=yf+(275-xf)*sin(120*t+i)+(150-yf)*cos(120*t+i);
                         line(x1,y1,x2,y2);
                         line(x2,y2,x4,y4);
                         line(x4,y4,x3,y3);
                         line(x3,y3,x1,y1);
       x1=xf+(175-xf)*cos(240*t+i)-(130-yf)*sin(240*t+i);
       y1=yf+(175-xf)*sin(240*t+i)+(130-yf)*cos(240*t+i);
       x2=xf+(275-xf)*cos(240*t+i)-(130-yf)*sin(240*t+i);
       y2=yf+(275-xf)*sin(240*t+i)+(130-yf)*cos(240*t+i);
       x3=xf+(175-xf)*cos(240*t+i)-(150-yf)*sin(240*t+i);
       y3=yf+(175-xf)*sin(240*t+i)+(150-yf)*cos(240*t+i);
       x4=xf+(275-xf)*cos(240*t+i)-(150-yf)*sin(240*t+i);
       y4=yf+(275-xf)*sin(240*t+i)+(150-yf)*cos(240*t+i);
                         line(x1,y1,x2,y2);
                         line(x2,y2,x4,y4);
                         line(x4,y4,x3,y3);
                         line(x3,y3,x1,y1);
       x1=xf+(175-xf)*cos(360*t+i)-(130-yf)*sin(360*t+i);
       y1=yf+(175-xf)*sin(360*t+i)+(130-yf)*cos(360*t+i);
       x2=xf+(275-xf)*cos(360*t+i)-(130-yf)*sin(360*t+i);
       y2=yf+(275-xf)*sin(360*t+i)+(130-yf)*cos(360*t+i);
       x4=xf+(175-xf)*cos(360*t+i)-(150-yf)*sin(360*t+i);
       y4=yf+(175-xf)*sin(360*t+i)+(150-yf)*cos(360*t+i);
       x3=xf+(275-xf)*cos(360*t+i)-(150-yf)*sin(360*t+i);
       y3=yf+(275-xf)*sin(360*t+i)+(150-yf)*cos(360*t+i);
                         line(x1,y1,x2,y2);
                         line(x2,y2,x3,y3);
                         line(x3,y3,x4,y4);
                         line(x4,y4,x1,y1);
       delay(p);
    }
    }
    }

Twitter Delicious Facebook Digg Stumbleupon Favorites More