Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Thursday, 3 March 2011

Each loop for a class or model instance variables


Sometimes we need to check all of instance variables in a class or model one by one. In this exaple we will check if a varaible is an Array or not:

your_model.instance_variables.each do |i|
if your_model.instance_variable_get(i).instance_of?(Array) then
#your code to do anything with your_model.instance_variable_get(i) what is a value
end
end

If you have an active record model, you can do:

@account = Account.first
Account.column_names.each do |i|
@account.instance_eval(i)
# row returns @account.name for example inside the loop, next @account.address and so on
end


Saturday, 14 August 2010

Circle drawing using Bresenhams Algorithm In c program

Without using circle function in graphics draw the circle. By using Bresenhams Algorithm draw the circle.These Circle algorithm is a variant of Bresenham’s line algorithm.The algorithm begin with circle formula .
x(2)+y(2)=r(2),x(2) repersent x square..,

See the source code

Source code C Language programming

#include

#include

#include

#include

void cplot(int,int,int,int);

void main()

{

int gd=DETECT,gm;

int x,y,p,xc,yc,r;

initgraph(&gd,&gm,"");

cleardevice();

printf("x,y,r : ");

scanf("%d%d%d",&xc,&yc,&r);

x=0;y=r;

p=1-r;

cplot(xc,yc,x,y);

while(x

{

x++;

if(p<0)

p+=2*x+1;

else

{

y--;

p+=2*(x-y)+1;

}

cplot(xc,yc,x,y);

}

getch();

}

void cplot(int xc,int yc,int x,int y)

{

putpixel(xc+x,yc+y,15);

putpixel(xc-x,yc+y,15);

putpixel(xc+x,yc-y,15);

putpixel(xc-x,yc-y,15);

putpixel(xc+y,yc+x,15);

putpixel(xc-y,yc+x,15);

putpixel(xc+y,yc-x,15);

putpixel(xc-y,yc-x,15);

}

//# Author: J.Ajai

//#Mail-id- ajay.compiler@gmail.com

//# PH:+91-9790402155




OUTPUT

Bresenhams Circle Drawing Algorithm

Enter the center point of the circle:

X = 100

Y = 200

R = 50

Bresenhams Circle Drawing Algorithm in c program | Computer Graphics  Lap:

Chat Program Using UDP Socket perform operation in Client Side CS1305- NETWORK LAB

Step by step Algorithm UDP Client
Start the UDP program
Create a socket and connect is with the server
Pass the message to server using sendto().
Receive the message from server using recvfrom()
Chat with the server until the message bye
Finally the terminate the client program
See the BASIC syntax for UDP CHAT program
Source code in C language Programming CS1305- NETWORK LAB
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
if(argc<2)
{
printf("insuffcient parameters");
exit(0);
}
struct sockaddr_in servsock;
int sockfd,size;
char msg[1024],str[1024];
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("");
exit(0);
}
printf("socket created\n");
size=sizeof(struct sockaddr);
socklen_t len=sizeof(servsock);
bzero(&servsock,size);
servsock.sin_port=htons(3000);
servsock.sin_family=AF_INET;
servsock.sin_addr.s_addr=inet_addr(argv[1]);
if(strcmp(msg,"bye")==0)
exit(0);
while(strcmp(msg,"bye")!=0)
{ printf("\n client");
scanf("%s",msg);
if((sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr*)&servsock,len))<0)
{
perror("not send");
exit(0);
}
if(strcmp(msg,"bye")==0)
exit(0);
printf("\n from server");
if((recvfrom(sockfd,str,1024,0,NULL,NULL))<0)
{
perror("not received");
exit(0);
}
printf("%s",str);
}
close(sockfd);
return 0; }

Output UDP client
[it28@localhost lapprogram]$cc chat.c
[it28@localhost lapprogram]$./a.out 192.168.1.111
Socket created
Client:lapprogram
From server:john
Client:jebrose
From server:Kevin
Client:jansi

Result
Thus the UDP chat client was performed successfully. CS1305- NETWORK LAB