|
DINESH DANGOL
Web Administrator
Teaching Assistant
|
EDUCATION
Bachelor in Computer Engineering, Nepal Engineering College, Changunarayan
10+2, V.S. Niketan Higher Secondary School, Minbhawan
S.L.C., Siddhartha Vanasthali Institute, Balaju
TRAININGS
Cisco Certified Network Associate CCNA, Prime College, Khusibun
Computer Networking, IT College, Jamal
Computer Hardware, MicroTech, Sundhara
Human Resource Development Programme (Web-Developer), National Web College, Bagbazar
SOFTWARES/ LANGUAGES
C, C++, Visual Basic 6.0
SDCC (Embedded Systems)
PHP, HTML
MYSQL, MS-Access, Oracle
Flash Actionscript
TEACHING SUBJECTS
Year 2006/2007 (Odd Semester)
Programming in C
Numerical Methods
Year 2007 (Even Semester)
Network Programming (Elective II)
Object Oriented Programming (C++)
Image Processing and Pattern Recognition
Numerical Methods
TRAINING COURSES
Year 2007 (Even Semester)
Microcontroller Programming & Interfacing (Started from 13 June, 2007)
Programming in Visual Basic 6.0 (Starting soon)
Programming in PHP & HTML (Starting soon)
PROJECTS SUPERVISED
Year 2007 (Even Semester)
Data-Mining on Population Growth
Microcontroller based Electronic Train
Automatic Temperature Emailer
Rainfall Powered Battery Charger
VoIP
Gallery:
Miscellaneous:
Network Programming
Introduction:
This subject is related to the socket programming. Two computers in a network connected together either in LAN or Internet may communicate with each other using socket programming.
Applications:
1. A computer in central office may collect data from computers in different regional offices and use it for administrative, processing and decision making tasks.
2. Inter-process communication
It may be used for communicating between programs written in different programming languages. For e.g. a Java program can capture image using webcam and send it to Visual Basic program for processing.
Related Projects:
1. Web-based Mail Client.
2. Remote Temperature Sensor.
3. Text-based Web Client.
4. Chat Sever-Client.
Network Programming
The server terminates as soon as it serves single client.
/* server.c */
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(15000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Binding Socket\n");
listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
if (new_socket > 0)
printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
recv(new_socket,buffer,bufsize,0);
send(new_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
close(new_socket);
close(create_socket);
}
/* client.c */
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
main()
{
int create_socket;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The Socket was created\n");
address.sin_family = AF_INET;
address.sin_port = htons(15000);
inet_pton(AF_INET,"127.0.0.1",&address.sin_addr);
if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("The connection was accepted with the server %s...\n",inet_ntoa(address.sin_addr));
gets(buffer);
send(create_socket,buffer,bufsize,0);
recv(create_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
close(create_socket);
}
Network Programming
This server serves only one child and the mode of communication is not full-duplex.
The client has to wait until the server sends a message and vice-versa.
/* server.c */
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(15000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Binding Socket\n");
listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
if (new_socket > 0){
printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
}
do{
printf("Message to send: ");
gets(buffer);
send(new_socket,buffer,bufsize,0);
recv(new_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
}while(strcmp(buffer,"/q")); //user ‘q’ to quit
close(new_socket);
close(create_socket);
}
/* client.c */
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
main(int argc,char *argv[])
{
int create_socket;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The Socket was created\n");
address.sin_family = AF_INET;
address.sin_port = htons(15000);
inet_pton(AF_INET,argv[1],&address.sin_addr);
if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("The connection was accepted with the server %s...\n",inet_ntoa(address.sin_addr));
do{
recv(create_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
if (strcmp(buffer,"/q")){
printf("Message to send: ");
gets(buffer);
send(create_socket,buffer,bufsize,0);
}
}while (strcmp(buffer,"/q"));
close(create_socket);
}
Network Programming
Iterative Echo Server
This server can serve multiple clients. As soon as server finishes serving a client it waits for connection from new client.
This server cannot handle multiple clients simultaneously.
/* server.c */
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(15000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Binding Socket\n");
listen(create_socket,3);
while(1){
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
if (new_socket > 0)
printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
recv(new_socket,buffer,bufsize,0);
send(new_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
close(new_socket);
}
// close(create_socket);
}
/* client.c */
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
main()
{
int create_socket;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The Socket was created\n");
address.sin_family = AF_INET;
address.sin_port = htons(15000);
inet_pton(AF_INET,"127.0.0.1",&address.sin_addr);
if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("The connection was accepted with the server %s...\n",inet_ntoa(address.sin_addr));
gets(buffer);
send(create_socket,buffer,bufsize,0);
recv(create_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
close(create_socket);
}
Network Programming
Concurrent Server
This server can handle multiple clients simultaneously.
/*server.c*/
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<signal.h>
void sig_chld(int signo){
pid_t pid;
int stat;
pid=wait(&stat);
printf("\nchild %d terminated\n",pid);
return;
}
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
pid_t pid;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(16000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Binding Socket\n");
listen(create_socket,3);
signal(SIGCHLD,sig_chld);
while(1){
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
pid=fork();
if (new_socket > 0)
printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
if(pid==0){
close(create_socket);
recv(new_socket,buffer,bufsize,0);
send(new_socket,buffer,strlen(buffer),0);
printf("Message recieved: %s\n",buffer);
close(new_socket);
exit(0);
}
else{
close(new_socket);
}
}
}
/* client.c */
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
main()
{
int create_socket;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The Socket was created\n");
address.sin_family = AF_INET;
address.sin_port = htons(16000);
inet_pton(AF_INET,"127.0.0.1",&address.sin_addr);
if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("The connection was accepted with the server %s...\n",inet_ntoa(address.sin_addr));
printf("Enter a message:");
gets(buffer);
send(create_socket,buffer,bufsize,0);
recv(create_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
close(create_socket);
}
Network Programming
Signal - wait
The server uses signal handler function using wait.
As soon as a client terminates the signal is captured and message is displayed.
/* server.c */
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<signal.h>
void sig_chld(int signo){
pid_t pid;
int stat;
pid=wait(&stat);
printf("\nchild %d terminated\n",pid);
return;
}
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
pid_t pid;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(16000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Binding Socket\n");
listen(create_socket,3);
signal(SIGCHLD,sig_chld);
while(1){
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
pid=fork();
if (new_socket > 0)
printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
if(pid==0){
close(create_socket);
recv(new_socket,buffer,bufsize,0);
send(new_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
close(new_socket);
exit(0);
}
else{
close(new_socket);
}
}
}
Network Programming
Signal - waitp
The server uses signal handler function using waitp.
As soon as a client terminates the signal is captured and message is displayed.
/* server.c */
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<signal.h>
void sig_chld(int signo){
pid_t pid;
int stat;
while( (pid=waitpid(-1,&stat,0))>0)
printf("\nchild %d terminated\n",pid);
return;
}
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
pid_t pid;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(16000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Binding Socket\n");
listen(create_socket,3);
signal(SIGCHLD,sig_chld);
while(1){
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
pid=fork();
if (new_socket > 0)
printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
if(pid==0){
close(create_socket);
recv(new_socket,buffer,bufsize,0);
send(new_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
close(new_socket);
exit(0);
}
else{
close(new_socket);
}
}
}
Network Programming
exec
exec function can be used to load other programs in disk.
Here a child process is created which calls exec to run another program hello
/* server.c */
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<signal.h>
main()
{
pid_t pid;
char input[25];
printf("testing exec");
pid=fork();
if(pid==0){
printf("child");
execl("./hello","379",(char *)NULL);
}
else{
printf("parent");
scanf("%s",input);
printf("parent %s",input);
}
}
/* hello.c */
#include <stdio.h>
main(int argc, char *argv[])
{
char a[15];
printf("hello world!\n");
printf("Enter your name");
scanf("%s",a);
printf("\nThank you %s\n",a);
printf("%d was passed by parent process as argument.",atoi(argv[0])+1);
printf("terminated");
}
Pronounce it fast
Peter Piper picked a peck of pickled peppers.
Bitty bought a bit of butter, but the butter was bitter. So, Bitty bought another bit of butter to make bitter butter better.
Laugh please: A Sardar Singh and Racing
A sardar once sees many people running down the street. He becomes very curious and asks a man nearby.
Sardar: What's going on here. Why are they running?
Man: They are on Racing. A person who reaches the target first receives a prize.
Sardar: If it is only the first one who gets the prize then why are the rest running?
Know it: About a switch
There are two types of switch - Manageable and non-manageable.
Switch increases the collision domain and hence minimizes the chance of collision. Manageable switch
can be configured to create different VLANs. Hosts of different VLANs are in different broadcast domains.
But a router is required for the hosts of one
VLAN to communicate with that of another VLAN.
Switch maintains MAC table in its CAM (Content Addressable Memory). Whenever forwarding a packet,
it checks the destination address in MAC table to see if the packet can be forwarded to a specific port,
if not then the flooding occurs where it sends layer2 broadcast (FFFFFFFF as destination mac-address).
If any port has the destination path, it responds with its mac-address and then the switch forwards the packet
to this port. Whenever a packet comes from a new source , the source ip and corresponding port is added to the MAC table.
Latency for the switch is high than hub, but is less than router. The switch is basically a bridge with multiple ports.
Tutorials:
Programming in C: Lab Sheet 5: Solution 1
#include<stdio.h>
#include<conio.h>
void input(int*,int*,int*);
void sum(int,int,int);
void sumofsquare(int,int,int);
void product(int,int,int);
void main()
{
int a,b,c;
char ch;
do
{
clrscr();
printf("\n1 Input three numbers");
printf("\n2 Summation fo numbers");
printf("\n3 Sum of squares");
printf("\n4 Product of numbers");
printf("\n5 Exit");
printf("\nEnter your choice:");
ch=getche();
switch(ch)
{
case '1':
input(&a,&b,&c);
break;
case '2':
sum(a,b,c);
break;
case '3':
sumofsquare(a,b,c);
break;
case '4':
product(a,b,c);
break;
case '5':
printf("\nProgram terminated!");
break;
default:
printf("\nInvalid choice!");
}
getch();
}while(ch!='5');
}
void input(int *x, int *y, int *z)
{
int n1,n2,n3;
printf("\nEnter first number:");
scanf("%d",&n1);
printf("\nEnter second number:");
scanf("%d",&n2);
printf("\nEnter third number:");
scanf("%d",&n3);
*x=n1;
*y=n2;
*z=n3;
}
void sum(int x, int y, int z)
{
int total;
total=x+y+z;
printf("\nThe sum is %d",total);
}
void sumofsquare(int x, int y, int z)
{
int total;
total=x*x+y*y+z*z;
printf("\nThe sum is %d",total);
}
void product(int x, int y, int z)
{
int answer;
answer=x*y*z;
printf("\nThe sum is %d",answer);
}
Programming in C: Lab Sheet 5: Solution 2
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float volume(int);
float circumference(int);
float area(int);
float surfacearea(int);
void main()
{
char ch;
float r,ans;
do
{
clrscr();
printf("\n1 Circumference of circle");
printf("\n2 area of cross-section of a hemisphere");
printf("\n3 surface area of sphere");
printf("\n4 volume of sphere");
printf("\n5 Exit");
printf("\nEnter radius:");
scanf("%f",&r);
printf("\nEnter your choice:");
ch=getche();
switch(ch)
{
case '1':
ans=circumference(r);
printf("\nCircumference of circle is %f",ans);
break;
case '2':
ans=area(r);
printf("\nArea of cross-section of hemisphere is %f",ans);
break;
case '3':
ans=surfacearea(r);
printf("\nSurface area of sphere is %f",ans);
break;
case '4':
ans=volume(r);
printf("\nVolume of sphere is %f",ans);
break;
case '5':
printf("\nProgram terminated!");
break;
default:
printf("\nInvalid choice!");
}
getch();
}while(ch!='5');
}
float volume(int r)
{
float result;
result=(4.0/3)*3.14*r*r*r;
return (result);
}
float circumference(int r)
{
float result;
result=2*3.14*r;
return (result);
}
float area(int r)
{
float result;
result=3.14*r*r;
return (result);
}
float surfacearea(int r)
{
float result;
result=(4.0/3)*3.14*r*r;
return (result);
}
Programming in C: Lab Sheet 5: Solution 3
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float fahrenheit(float);
float sqroot(float);
void lucky(int,int);
int factorial(int);
void main()
{
char ch;
int a,b,n,fact;
float c,f,s;
do
{
clrscr();
printf("\n1 Celcius to Fahrenheit");
printf("\n2 Square root");
printf("\n3 Ten Lucky numbers");
printf("\n4 Factorial");
printf("\n5 Exit");
printf("\nEnter your choice:");
ch=getche();
switch(ch)
{
case '1':
printf("\nEnter a tempereture in celcius:");
scanf("%f",&c);
f=fahrenheit(c);
printf("\nFahrenheit equivalent of %f is %f",c,f);
break;
case '2':
printf("\nEnter a number:");
scanf("%d",&n);
s=sqroot(n);
printf("\nSquare root of %d is %f",n,s);
break;
case '3':
printf("\nEnter two numbers:");
scanf("%d%d",&a,&b);
lucky(a,b);
break;
case '4':
printf("\nEnter a number:");
scanf("%d",&n);
fact=factorial(n);
printf("\nFactorial of %d is %d",n,fact);
break;
case '5':
printf("\nProgram terminated!");
break;
default:
printf("\nInvalid choice!");
}
getch();
}while(ch!='5');
}
float fahrenheit(float c)
{
float a;
a=(180.0/100)*c+32;
return a;
}
float sqroot(float x)
{
float a;
a=sqrt(x);
return a;
}
void lucky(int x,int y)
{
int n,i;
randomize();
printf("\n10 lucky numbers are:\n");
for(i=0;i<10;i++)
{
n=x+random(y-x+1);
printf("%d ",n);
}
}
int factorial(int x)
{
int i,a;
a=1;
for(i=2;i<=x;i++)
{
a=a*i;
}
return a;
}
Programming in C: Lab Sheet 5: Solution 4
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void length(char m[20]){
int i;
for(i=0;i<strlen(m);i++)
printf("\n%s",m);
}
void reverse(char m[20]){
int i;
for(i=strlen(m)-1;i>=0;i--)
printf("%c",m[i]);
}
void block(char m[20]){
int i,j;
printf("\n");
for(i=0;i<strlen(m);i++)
{
for(j=0;j<=i;j++)
printf("%c",m[j]);
printf("\n");
}
}
void changecase(char m[20]){
int i;
for(i=0;i<strlen(m);i++)
{
if(m[i]>='a'&&m[i]<='z')
m[i]=m[i]-32;
else if(m[i]>='A'&&m[i]<='Z')
m[i]=m[i]+32;
}
printf("\n%s",m);
}
void main()
{
char ch;
char message[20];
clrscr();
printf("\nEnter a message:");
gets(message);
do{
clrscr();
printf("\n1. print message no. of time as per its length");
printf("\n2. print message in reverse order");
printf("\n3. print message in block staircase pattern");
printf("\n4. print message in capital letters or vice-versa");
printf("\n5. exit");
printf("\nEnter choice:");
ch=getch();
switch (ch){
case '1':
length(message);
break;
case '2':
reverse(message);
break;
case '3':
block(message);
break;
case '4':
changecase(message);
break;
case '5':
printf("\nprogram terminated!");
}
getch();
}while(ch!='5');
}
Programming in C: Lab Sheet 5: Solution 5
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
char a[3][3]={
{1,2,3},{4,5,6},{7,8,9}
};
int i,j,s;
void sumirow()
{
s=0;
printf("\nEnter row number i:");
scanf("%d",&i);
for(j=0;j<3;j++)
s=s+a[i][j];
printf("\nSum of %d row elements is %d",i,s);
}
void sumjcol()
{
s=0;
printf("\nEnter column number j:");
scanf("%d",&j);
for(i=0;i<3;i++)
s=s+a[i][j];
printf("\nSum of %d column elements is %d",j,s);
}
void sumdleft()
{
s=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i==j)
s=s+a[i][j];
printf("\nSum of diagonal elements from left is %d",s);
}
void sumdright()
{
s=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i+j==3-1)
s=s+a[i][j];
printf("\nSum of diagonal elements from right is %d",s);}
void sumall()
{
s=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
s=s+a[i][j];
printf("\nSum of all elements is %d",s);
}
void main()
{
char ch;
do{
clrscr();
printf("\n1. sum of i th row");
printf("\n2. sum of j th column");
printf("\n3. sum of diagonal elements from left");
printf("\n4. sum of diagonal elements from right");
printf("\n5. sum of all elements");
printf("\n6. exit");
printf("\nEnter choice:");
ch=getch();
switch (ch){
case '1':
sumirow();
break;
case '2':
sumjcolumn();
break;
case '3':
sumdleft();
break;
case '4':
sumdright();
break;
case '5':
sumall();
break;
case '6':
printf("\nprogram terminated!");
}
getch();
}while(ch!='6');
}
Programming in C: Lab Sheet 6: Solution 1
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct student{
int roll;
char name[20];
float mark;
};
void main()
{
struct student stud[20];
int i,n=20;
for(i=0;i<n;i++)
{
printf("\nEnter roll");
scanf("%d",&stud[i].roll);
printf("\nEnter name");
scanf("%s",&stud[i].name);
printf("\nEnter mark");
scanf("%f",&stud[i].mark);
}
printf("\nFailed students\n");
for(i=0;i<n;i++)
{
if(stud[i].mark<40)
{
printf("\n\nRoll=%d",stud[i].roll);
printf("\nName=%s",stud[i].name);
printf("\nMark=%f",stud[i].mark);
}
}
getch();
}
Programming in C: Lab Sheet 6: Solution 2
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct student{
int roll;
char name[20];
float mark;
};
void main()
{
struct student stud;
char ch;
FILE *fp;
fp=fopen("student.dat","r+");
if(fp==0)
fp=fopen("student.dat","w+");
do{
printf("\n1. add student");
printf("\n2. view students");
printf("\n3. view passed students");
printf("\n4. view failed students");
printf("\n5. exit");
printf("\nEnter choice");
ch=getch();
switch(ch)
{
case '1':
printf("\nEnter roll");
scanf("%d",&stud.roll);
printf("\nEnter name");
scanf("%s",&stud.name);
printf("\nEnter mark");
scanf("%f",&stud.mark);
fseek(fp,0,SEEK_END);
fwrite(&stud,sizeof(stud),1,fp);
break;
case '2':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
while(!feof(fp))
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
fread(&stud,sizeof(stud),1,fp);
}
break;
case '3':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
printf("\nPassed students\n");
while(!feof(fp))
{
if(stud.mark>=40)
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
}
fread(&stud,sizeof(stud),1,fp);
}
break;
case '4':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
printf("\nPassed students\n");
while(!feof(fp))
{
if(stud.mark<40)
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
}
fread(&stud,sizeof(stud),1,fp);
}
break;
case '5':
break;
}
getch();
}while(ch!='5');
fclose(fp);
}
Numerical Methods: Bisection Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
float x1,x2,f1,f2,x0,f0;
int count;
clrscr();
do{
printf("\nEnter the values of x1 and x2:");
scanf("%f %f",&x1,&x2);
count=0;
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
printf("Invalid guesses! Try again.");
}while(f1*f2>0);
x0=(x1+x2)/2.0;
f0=f(x0);
count++;
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
// while(fabs((x1-x2)/x2)>e){
while(fabs(f(x0))>e){
x0=(x1+x2)/2.0;
f0=f(x0);
count++;
if(f0*f1>0)
x1=x0;
else
x2=x0;
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nx=%f f(x)=%f",x0,f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Numerical Methods: Regula-Falsi Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
int count=0;
float x0,x1,x2,f0,f1,f2;
clrscr();
do{
printf("\nEnter the values of x1 and x2:");
scanf("%f %f",&x1,&x2);
count=0;
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
printf("Invalid guesses! Try again.");
}while(f1*f2>0);
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
while(fabs(f0)>e){
count++;
if((f1*f0)<0){
x1=x0;
f1=f0;
}
else{
x2=x0;
f2=f0;
}
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nRoot x=%f",x0);
printf("\nFunctional value f(x)=%f",f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Numerical Methods: Secant Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
int count=0;
float x0,x1,x2,f0,f1,f2;
clrscr();
printf("\nEnter the initial guesses x1 & x2:");
scanf("%f %f",&x1,&x2);
f1=f(x1);
f2=f(x2);
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
while(fabs(f0)>e){
count++;
x1=x2;
f1=f2;
x2=x0;
f2=f0;
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nRoot=%f",x0);
printf("\nFunctional value=%f",f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Programming in C: Lab Sheet 5: Solution 5
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
char a[3][3]={
{1,2,3},{4,5,6},{7,8,9}
};
int i,j,s;
void sumirow()
{
s=0;
printf("\nEnter row number i:");
scanf("%d",&i);
for(j=0;j<3;j++)
s=s+a[i][j];
printf("\nSum of %d row elements is %d",i,s);
}
void sumjcol()
{
s=0;
printf("\nEnter column number j:");
scanf("%d",&j);
for(i=0;i<3;i++)
s=s+a[i][j];
printf("\nSum of %d column elements is %d",j,s);
}
void sumdleft()
{
s=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i==j)
s=s+a[i][j];
printf("\nSum of diagonal elements from left is %d",s);
}
void sumdright()
{
s=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i+j==3-1)
s=s+a[i][j];
printf("\nSum of diagonal elements from right is %d",s);}
void sumall()
{
s=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
s=s+a[i][j];
printf("\nSum of all elements is %d",s);
}
void main()
{
char ch;
do{
clrscr();
printf("\n1. sum of i th row");
printf("\n2. sum of j th column");
printf("\n3. sum of diagonal elements from left");
printf("\n4. sum of diagonal elements from right");
printf("\n5. sum of all elements");
printf("\n6. exit");
printf("\nEnter choice:");
ch=getch();
switch (ch){
case '1':
sumirow();
break;
case '2':
sumjcolumn();
break;
case '3':
sumdleft();
break;
case '4':
sumdright();
break;
case '5':
sumall();
break;
case '6':
printf("\nprogram terminated!");
}
getch();
}while(ch!='6');
}
Programming in C: Lab Sheet 6: Solution 1
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct student{
int roll;
char name[20];
float mark;
};
void main()
{
struct student stud[20];
int i,n=20;
for(i=0;i<n;i++)
{
printf("\nEnter roll");
scanf("%d",&stud[i].roll);
printf("\nEnter name");
scanf("%s",&stud[i].name);
printf("\nEnter mark");
scanf("%f",&stud[i].mark);
}
printf("\nFailed students\n");
for(i=0;i<n;i++)
{
if(stud[i].mark<40)
{
printf("\n\nRoll=%d",stud[i].roll);
printf("\nName=%s",stud[i].name);
printf("\nMark=%f",stud[i].mark);
}
}
getch();
}
Programming in C: Lab Sheet 6: Solution 2
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct student{
int roll;
char name[20];
float mark;
};
void main()
{
struct student stud;
char ch;
FILE *fp;
fp=fopen("student.dat","r+");
if(fp==0)
fp=fopen("student.dat","w+");
do{
printf("\n1. add student");
printf("\n2. view students");
printf("\n3. view passed students");
printf("\n4. view failed students");
printf("\n5. exit");
printf("\nEnter choice");
ch=getch();
switch(ch)
{
case '1':
printf("\nEnter roll");
scanf("%d",&stud.roll);
printf("\nEnter name");
scanf("%s",&stud.name);
printf("\nEnter mark");
scanf("%f",&stud.mark);
fseek(fp,0,SEEK_END);
fwrite(&stud,sizeof(stud),1,fp);
break;
case '2':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
while(!feof(fp))
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
fread(&stud,sizeof(stud),1,fp);
}
break;
case '3':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
printf("\nPassed students\n");
while(!feof(fp))
{
if(stud.mark>=40)
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
}
fread(&stud,sizeof(stud),1,fp);
}
break;
case '4':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
printf("\nPassed students\n");
while(!feof(fp))
{
if(stud.mark<40)
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
}
fread(&stud,sizeof(stud),1,fp);
}
break;
case '5':
break;
}
getch();
}while(ch!='5');
fclose(fp);
}
Numerical Methods: Bisection Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
float x1,x2,f1,f2,x0,f0;
int count;
clrscr();
do{
printf("\nEnter the values of x1 and x2:");
scanf("%f %f",&x1,&x2);
count=0;
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
printf("Invalid guesses! Try again.");
}while(f1*f2>0);
x0=(x1+x2)/2.0;
f0=f(x0);
count++;
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
// while(fabs((x1-x2)/x2)>e){
while(fabs(f(x0))>e){
x0=(x1+x2)/2.0;
f0=f(x0);
count++;
if(f0*f1>0)
x1=x0;
else
x2=x0;
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nx=%f f(x)=%f",x0,f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Numerical Methods: Regula-Falsi Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
int count=0;
float x0,x1,x2,f0,f1,f2;
clrscr();
do{
printf("\nEnter the values of x1 and x2:");
scanf("%f %f",&x1,&x2);
count=0;
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
printf("Invalid guesses! Try again.");
}while(f1*f2>0);
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
while(fabs(f0)>e){
count++;
if((f1*f0)<0){
x1=x0;
f1=f0;
}
else{
x2=x0;
f2=f0;
}
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nRoot x=%f",x0);
printf("\nFunctional value f(x)=%f",f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Numerical Methods: Secant Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
int count=0;
float x0,x1,x2,f0,f1,f2;
clrscr();
printf("\nEnter the initial guesses x1 & x2:");
scanf("%f %f",&x1,&x2);
f1=f(x1);
f2=f(x2);
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
while(fabs(f0)>e){
count++;
x1=x2;
f1=f2;
x2=x0;
f2=f0;
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nRoot=%f",x0);
printf("\nFunctional value=%f",f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Programming in C: Lab Sheet 6: Solution 2
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
struct student{
int roll;
char name[20];
float mark;
};
void main()
{
struct student stud;
char ch;
FILE *fp;
fp=fopen("student.dat","r+");
if(fp==0)
fp=fopen("student.dat","w+");
do{
printf("\n1. add student");
printf("\n2. view students");
printf("\n3. view passed students");
printf("\n4. view failed students");
printf("\n5. exit");
printf("\nEnter choice");
ch=getch();
switch(ch)
{
case '1':
printf("\nEnter roll");
scanf("%d",&stud.roll);
printf("\nEnter name");
scanf("%s",&stud.name);
printf("\nEnter mark");
scanf("%f",&stud.mark);
fseek(fp,0,SEEK_END);
fwrite(&stud,sizeof(stud),1,fp);
break;
case '2':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
while(!feof(fp))
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
fread(&stud,sizeof(stud),1,fp);
}
break;
case '3':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
printf("\nPassed students\n");
while(!feof(fp))
{
if(stud.mark>=40)
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
}
fread(&stud,sizeof(stud),1,fp);
}
break;
case '4':
fseek(fp,0,SEEK_SET);
fread(&stud,sizeof(stud),1,fp);
printf("\nPassed students\n");
while(!feof(fp))
{
if(stud.mark<40)
{
printf("\n\nRoll=%d",stud.roll);
printf("\nName=%s",stud.name);
printf("\nMark=%f",stud.mark);
}
fread(&stud,sizeof(stud),1,fp);
}
break;
case '5':
break;
}
getch();
}while(ch!='5');
fclose(fp);
}
Numerical Methods: Bisection Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
float x1,x2,f1,f2,x0,f0;
int count;
clrscr();
do{
printf("\nEnter the values of x1 and x2:");
scanf("%f %f",&x1,&x2);
count=0;
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
printf("Invalid guesses! Try again.");
}while(f1*f2>0);
x0=(x1+x2)/2.0;
f0=f(x0);
count++;
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
// while(fabs((x1-x2)/x2)>e){
while(fabs(f(x0))>e){
x0=(x1+x2)/2.0;
f0=f(x0);
count++;
if(f0*f1>0)
x1=x0;
else
x2=x0;
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nx=%f f(x)=%f",x0,f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Numerical Methods: Regula-Falsi Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
int count=0;
float x0,x1,x2,f0,f1,f2;
clrscr();
do{
printf("\nEnter the values of x1 and x2:");
scanf("%f %f",&x1,&x2);
count=0;
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
printf("Invalid guesses! Try again.");
}while(f1*f2>0);
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
while(fabs(f0)>e){
count++;
if((f1*f0)<0){
x1=x0;
f1=f0;
}
else{
x2=x0;
f2=f0;
}
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nRoot x=%f",x0);
printf("\nFunctional value f(x)=%f",f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Numerical Methods: Secant Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
int count=0;
float x0,x1,x2,f0,f1,f2;
clrscr();
printf("\nEnter the initial guesses x1 & x2:");
scanf("%f %f",&x1,&x2);
f1=f(x1);
f2=f(x2);
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
while(fabs(f0)>e){
count++;
x1=x2;
f1=f2;
x2=x0;
f2=f0;
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nRoot=%f",x0);
printf("\nFunctional value=%f",f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Numerical Methods: Regula-Falsi Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
int count=0;
float x0,x1,x2,f0,f1,f2;
clrscr();
do{
printf("\nEnter the values of x1 and x2:");
scanf("%f %f",&x1,&x2);
count=0;
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
printf("Invalid guesses! Try again.");
}while(f1*f2>0);
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
while(fabs(f0)>e){
count++;
if((f1*f0)<0){
x1=x0;
f1=f0;
}
else{
x2=x0;
f2=f0;
}
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nRoot x=%f",x0);
printf("\nFunctional value f(x)=%f",f0);
printf("\nNo. of iterations=%d",count);
getch();
}
Numerical Methods: Secant Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x-4)
#define e 0.0001
void main()
{
int count=0;
float x0,x1,x2,f0,f1,f2;
clrscr();
printf("\nEnter the initial guesses x1 & x2:");
scanf("%f %f",&x1,&x2);
f1=f(x1);
f2=f(x2);
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
while(fabs(f0)>e){
count++;
x1=x2;
f1=f2;
x2=x0;
f2=f0;
x0=x2-f2*(x2-x1)/(f2-f1);
f0=f(x0);
printf("\n[%d] x=%f f(x)=%f",count,x0,f0);
}
printf("\nResult found!");
printf("\nRoot=%f",x0);
printf("\nFunctional value=%f",f0);
printf("\nNo. of iterations=%d",count);
getch();
}
SDCC: Transmit
#include <at89x51.h>
#define NULL 0
#define FALSE 0
#define TRUE 1
#define LOW 0
#define HIGH 1
void serinit(void)
{
SCON = 0xC0; // Mode 3.
TMOD = (TMOD & 0x0F) | 0x20; // Timer 1, mode 2, 8-bit reload.
TH1 = 0xFD; // 9600 baud * 2 = 19200 baud.
PCON |= 0x80; // Double baud rate.
TR1 = 1; // Timer 1 run.
REN = TRUE; // Receive characters.
PS = TRUE; // Low priority.
ES = TRUE; // Enable serial interrupt.
EA = TRUE;
}
void delay (int x)
{
int y, z;
for (y=0; y < x; y++)
for (z=0; z < 500; z++)
{
_asm
nop
_endasm;
}
}
void main()
{
char val_from_key='a';
serinit();
TI =1;
while(1)
{
val_from_key++;
delay(10);
if (TI)
{
SBUF = val_from_key;
P2 = val_from_key;
}
}//end of while
}//end of main
SDCC: Receive
#include <at89x51.h>
#define NULL 0
#define ON 1
#define OFF 0
#define FALSE 0
#define TRUE 1
#define LOW 0
#define HIGH 1
void serinit(void)
{
SCON = 0xC0; // Mode 3.
TMOD = (TMOD & 0x0F) | 0x20; // Timer 1, mode 2, 8-bit reload.
PCON |= 0x80; // Double baud rate.
TH1 = 0xFD; // 9600 baud * 2 = 19200 baud.
TR1 = 1; // Timer 1 run.
REN = TRUE; // Receive characters.
PS = TRUE; // Low priority.
ES = TRUE; // Enable serial interrupt.
EA = TRUE;
}
void main()
{
char Rxdata;
//P3_0 = 1; //for communication purpose
//P3_1 = 1;
serinit();
P2 = 0;
while(1)
{
if (RI)
{
RI = 0;
Rxdata = SBUF;
P2 = Rxdata;
}//end of switch
}//end of while
}//end of main
|