Home >Education >Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad

Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad

Date post:07-May-2015
Category:
View:367 times
Download:0 times
Share this document with a friend
Description:
C and Data Structures Lab Solutions, JNTU Hyderabad, Srinivas Reddy Amedapu, 09490 456 987, 08220 172 182, A.S.Reddy
Transcript:
  • [email protected] Computer Programming Lab Solutions (as per JNTU Hyderabad Syllabus)Prepared by: Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADUDate: January 04, 2013 Email: [email protected], Phone: 08220 172 182 (TN), 09490 456 987 (AP)Week I (a) Write a program to find the sum of individual digits of a positive integer.#includevoid main(){ int num,d,sum,temp;printf("nEnter a positive integer : ");scanf("%d",&num);temp=num;sum=0;while(num){d=num%10;sum=sum+d;num/=10;}printf("nSum of digits of %d is %d",temp,sum);}Enter a positive integer : 1234Sum of digits of 1234 is 10Week I (b) A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a Cprogram to generate the first n terms of the sequence.#includevoid main(){ int f1,f2,f3,n,i;printf("nHow many Fibonacci Sequence numbers you want? ");scanf("%d",&n);f1=0, f2=1;printf("nThe follwing are %d Fibonacci Numbersn",n);Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU1

2. [email protected](i=1;i=loc;i--) str[i+n2]=str[i];Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU15 16. [email protected]=0;while(sub[j]){ str[loc+j]=sub[j]; j++;}printf("nMain string: %s",str);printf("nSub string: %s",sub);}INPUT/OUTPUTEnter main string: JntuHyderabadEnter sub string: ASReddyEnter position: 4Main string: JntuHyderabadSub string: ASReddyMain string: JntuASReddyHyderabadSub string: ASReddyEnter main string: AravindReddyJNTUHEnter sub string: 12011U0502Enter position: 12Main string: AravindReddyJNTUHSub string: 12011U0502Main string: AravindReddy12011U0502JNTUHSub string: 12011U0502To delete n Characters from a given position in a given string.#includevoid main(){ char str[500]; int n,i,loc;printf("nEnter a string: ");scanf("%s",str);Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU16 17. [email protected]("nEnter number of characters to be deleted: ");scanf("%d",&n);printf("nEnter position from which deletion should be done: ");scanf("%d",&loc);if(loc+n>strlen(str)){printf("nDeletion not possible");printf("nToo many characters from the given location");exit(0);}i=loc;while(str[i+n]){str[i]=str[i+n];i++;}str[i]=str[i+n];printf("nString after deletion: %s",str);}INPUT/OUTPUTEnter a string: AravindReddyJNTUEnter number of characters to be deleted: 5Enter position from which deletion should be done: 7String after deletion: AravindJNTUEnter a string: AravindReddyNarmetta12011U0502Enter number of characters to be deleted: 8Enter position from which deletion should be done: 12String after deletion: AravindReddy12011U0502Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU17 18. [email protected]) Write a C program to determine if the given string is a palindrome or not#include#includevoid main(){ char str[500]; int n,i;printf("nEnter a string: ");scanf("%s",str);n=strlen(str);for(i=0;ivoid main(){ char str[500],sub[100]; int n1,n2,i,j,loc;printf("nEnter main string: ");scanf("%s",str);printf("nEnter sub string: ");scanf("%s",sub);i=0;while(str[i]){j=0;while(str[i+j]&&sub[j]&&str[i+j]==sub[j])j++;if(sub[j]==NULL)break;i=i+1;}if(sub[j]==NULL)printf("nSub string available at %d location",i+1);elseprintf("nSub String not available : %d",-1);}INPUT/OUTPUTEnter main string: AravindReddyEnter sub string: ReddySub string available at 8 locationEnter main string: AravindJNTUcseEnter sub string: JNTUSub string available at 8 locationEnter main string: SrinivasReddyEnter sub string: vasSub string available at 6 locationEnter main string: FirstYearEnter sub string: irtSub String not available : -1Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU19 20. [email protected] 8 (a) Write a program to generate Pascals triangle.#includevoid main(){ int x[50][50],i,j,spaces,l,n;printf("nThis is a program to generate Pascal triangle");printf("nHow many lines you want : ");scanf("%d",&n);for(i=0;ivoid main(){ int i,j,l,n,sp;printf("nHow many lines you want ? ");scanf("%d",&n);sp=35;for(i=1;idir jnt*.*Volume in drive C has no label.Volume Serial Number is 60A8-805BDirectory of C:TCBIN01/05/2013 11:49 PM485 JNTUH.C 1 File(s) 485 bytes 0 Dir(s) 36,109,844,480 bytes freeC:TCBIN>exitb) Write a C program to reverse the first n characters in a file.(Note: The file name and n are specified on the command line.)Week 13a) Write a C programme to display the contents of a file.#includevoid main(int argc,char* argv[]){ FILE *fp; char ch;if(argc!=2){printf("nUse Command Properly");printf("nCommand FileName");exit(0);}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU27 28. [email protected]=fopen(argv[1],"r");if(fp==NULL){ printf("nSource File Not Existing"); exit(1);}ch=fgetc(fp);while(ch!=EOF){printf("%c",ch);ch=fgetc(fp);}fclose(fp);}C:TCBIN>asrtype yy.cSource File Not ExistingC:TCBIN>asrtype simple.cvoid main(){ printf("nWelcome to JNTU--Aravind Reddy CSE");}C:TCBIN>asrtype simple.c asrtype.cUse Command ProperlyCommand FileNameC:TCBIN>exitb) Write a C programme to merge two files into a third file ( i.e., the contents of the first file followedby those of the second are put in the third file)#includevoid main(int argc,char* argv[]){ FILE *fp1,*fp2,*fp3; char ch;if(argc!=4){printf("nUse Command Properly");printf("nCommand SourceFileName1 SourceFileName2 TargetFileName");exit(0);}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU28 29. [email protected]=fopen(argv[1],"r");if(fp1==NULL){ printf("nSource File1 Not Existing"); exit(1);}fp3=fopen(argv[3],"w");ch=fgetc(fp1);while(ch!=EOF){fputc(ch,fp3);ch=fgetc(fp1);}fclose(fp1);fp2=fopen(argv[2],"r");if(fp2==NULL){ printf("nSource File 2 Not Existing"); fclose(fp3); exit(2);}ch=fgetc(fp2);while(ch!=EOF){fputc(ch,fp3);ch=fgetc(fp2);}fclose(fp2);fclose(fp3);}INPUT/OUTPUTC:TCBIN>asrmerg simple.c hworld.c aravind.cC:TCBIN>type simple.cvoid main(){printf("nWelcome to JNTU--Aravind Reddy CSE");}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU29 30. [email protected]:TCBIN>type hworld.c#includevoid main(){ printf("nHello World!"); printf("nThis is Aravind Reddy"); printf("nRoll No. 12011U0502, CSE");}C:TCBIN>type aravind.cvoid main(){ printf("nWelcome to JNTU--Aravind Reddy CSE");}#includevoid main(){ printf("nHello World!"); printf("nThis is Aravind Reddy"); printf("nRoll No. 12011U0502, CSE");}C:TCBIN>exitWeek 14Write a C program that uses functions to perform the following operations on singly linked list.:i) Creation ii) Insertion iii) Deletion iv) Traversal#include#includestruct node{int data;struct node *link;};typedef struct node* nodeptr;void main(){int val,pos;nodeptr list;nodeptr create();void print(),insertnth(nodeptr*,int,int);int deletenth(nodeptr*,int);Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU30 31. [email protected]=create();print(list);printf("nEnter Element to be inserted and position : ");scanf("%d%d",&val,&pos);insertnth(&list,val,pos);print(list);printf("nWhich position node you want to delete? ");scanf("%d",&pos);val=deletenth(&list,pos);printf("nDeleted node value is %d",val);print(list);}int deletenth(nodeptr *lp,int pos){int val=-1;nodeptr p,q,r;if(*lp==NULL)printf("nLinked List is empty, delete not possible");else{p=*lp;if(pos==1){*lp=p->link;val=p->data;free(p);}else{pos=pos-2;while(p&&pos){ p=p->link; pos=pos-1;}if(p){ q=p->link; p->link=q->link; val=q->data; free(q);}else printf("nLess nodes than given node number");}}return val;}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 31 32. [email protected] insertnth(nodeptr *lp,int val,int pos){ nodeptr p,q,r; nodeptr getnode();q=getnode();q->data=val;q->link=NULL;if(*lp==NULL) *lp=q;else{ p=*lp; pos=pos-1; while(p&&pos) {r=p;p=p->link;pos=pos-1; } q->link=r->link; r->link=q;}}nodeptr create(){ int val; nodeptr p,q,r; nodeptr getnode();printf("nEnter 0 (zero) to stopn");scanf("%d",&val);p=getnode();r=p;while(val){ q=getnode(); q->data=val; p->link=q; p=q; scanf("%d",&val);}p->link=NULL;p=r->link;free(r);return p;}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU32 33. [email protected] print(nodeptr p){ printf("nContents of Linked List aren"); while(p) { printf(" %d",p->data); p=p->link; }}nodeptr getnode(){ nodeptr p;p=(nodeptr)malloc(sizeof(struct node));return p;}INPUT/OUTPUTEnter 0 (zero) to stop11 22 33 44 55 66 77 88 99 0Contents of Linked List are 11 22 33 44 55 66 77 88 99Enter Element to be inserted and position : 678 6Contents of Linked List are 11 22 33 44 55 678 66 77 88 99Which position node you want to delete? 3Deleted node value is 33Contents of Linked List are 11 22 44 55 678 66 77 88 99Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU33 34. [email protected] 15Write C programs that implement stack (its operations) usingi) Arrays ii) PointersArrays#include#define MAX 100void main(){int stack[MAX],top=-1,val,op;void push(),display(),options();int pop(); clrscr(); options(); while(1) { printf("nSelect option : "); scanf("%d",&op); switch(op) { case 1: printf("nEnter value to be pushed : "); scanf("%d",&val); push(stack,&top,val); break; case 2: val=pop(stack,&top); printf("nValue poped is %d",val); break; case 3: display(stack,top); break; case 4: exit(0); break; //optional ie, non reachable code default:options(); break; //optional, last case } }}void push(int stk[],int *tp,int val){ if(*tp==MAX-1) printf("nStack full, %d not inserted",val); else { *tp=*tp+1; stk[*tp]=val; }}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 34 35. [email protected] pop(int stk[],int *tp){int val=-1;if(*tp==-1)printf("nStack empty, no value deleted");else{val=stk[*tp];*tp=*tp-1;}return val;}void display(int stk[],int top){ int i;printf("nContents of Stack aren");for(i=top;i>=0;i--)printf("ntt%4d",stk[i]);}void options(){ printf("nAvailable optons aren"); printf("ntt0. Options"); printf("ntt1. Push"); printf("ntt2. Pop"); printf("ntt3. Display"); printf("ntt4. Exit");}INPUT/OUTPUTAvailable optons are 0. Options 1. Push 2. Pop 3. Display 4. ExitSelect option : 1Enter value to be pushed : 11Select option : 1Enter value to be pushed : 99Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU35 36. [email protected] option : 1Enter value to be pushed : 88Select option : 3Contents of Stack are 88 99 11Select option : 2Value poped is 88Select option : 2Value poped is 99Select option : 2Value poped is 11Select option : 2Stack empty, no value deletedValue poped is -1Select option : 4Pointers#includestruct node{int data;struct node *link;};typedef struct node* nodeptr;void main(){int op,val;nodeptr sp=NULL;void push(),display(),options();int pop();clrscr();options();Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU36 37. [email protected](1){ printf("nSelect option : "); scanf("%d",&op); switch(op) { case 1: printf("nEnter value to be pushed : "); scanf("%d",&val); push(&sp,val); break; case 2: val=pop(&sp); printf("nValue poped is %d",val); break; case 3: display(sp); break; case 4: exit(0); break; //optional ie, non reachable code default:options(); break; //optional, last case }}}void push(nodeptr *spp,int val){ nodeptr p; nodeptr getnode();p=getnode();p->data=val;p->link=*spp;*spp=p;}int pop(nodeptr *spp){int val=-1;nodeptr p;if(*spp==NULL)printf("nStack Empty");else{p=*spp;*spp=p->link;val=p->data;free(p);}return val;}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 37 38. [email protected] display(nodeptr p){printf("nContents of Stack aren");while(p){printf("nt%4d",p->data);p=p->link;}}void options(){ printf("nAvailable optons aren"); printf("nt0. Options"); printf("nt1. Push"); printf("nt2. Pop"); printf("nt3. Display"); printf("nt4. Exit");}nodeptr getnode(){ nodeptr p;p=(nodeptr)malloc(sizeof(struct node));return p;}INPUT/OUTPUTAvailable optons are 0. Options 1. Push 2. Pop 3. Display 4. ExitSelect option : 1Enter value to be pushed : 11Select option : 1Enter value to be pushed : 22Select option : 1Enter value to be pushed : 44Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU38 39. [email protected] option : 2Value poped is 44Select option : 3Contents of Stack are2211Select option : 2Value poped is 22Select option : 2Value poped is 11Select option : 2Stack EmptyValue poped is -1Select option : 4Week 16Write C programs that implement Queue (its operations) usingi) Arrays ii) PointersArrays#include#define MAX 100void main(){int val,op;int queue[MAX],front=0,rear=-1;int delet();void insert(),display(),options(); options(); while(1) {printf("nSelect option: ");scanf("%d",&op);switch(op){case 1: printf("nEnter value to be inserted: ");scanf("%d",&val);insert(queue,&rear,val);break;Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 39 40. [email protected] case 2: val=delet(queue,&front,rear); printf("nDeleted value is %d",val); break; case 3: display(queue,front,rear); break; case 4: exit(0); break; default: options(); break; }}}int delet(int q[],int *fp,int r){int val=-1;if(*fp>r)printf("nQueue empty");else{val=q[*fp];*fp=*fp+1;}return val;}void insert(int q[],int *rp,int val){ if(*rp==MAX-1)printf("nQueue full"); else {*rp=*rp+1;q[*rp]=val; }}void display(int q[],int f,int r){ int i;printf("nContents of queue are:n");for(i=f;idata;*fp=p->link;if(*fp==NULL)*rp=NULL;}return val;}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU42 43. [email protected] insert(nodeptr *fp,nodeptr *rp,int val){ nodeptr p; nodeptr getnode();p=getnode();p->data=val;p->link=NULL;if(*rp==NULL) *fp=*rp=p;else{ (*rp)->link=p; *rp=p;}}void display(nodeptr p){ printf("nContents of queue are:n"); while(p) { printf(" %d",p->data); p=p->link; }}void options(){ printf("nAvailable operations"); printf("nt1. Insert"); printf("nt2. Delete"); printf("nt3. Display"); printf("nt4. Exit");}nodeptr getnode(){ nodeptr p;p=(nodeptr)malloc(sizeof(struct node));return p;}Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU43 44. [email protected] operations 1. Insert 2. Delete 3. Display 4. ExitSelect option: 1Enter value to be inserted: 11Select option: 1Enter value to be inserted: 12Select option: 1Enter value to be inserted: 14Select option: 2Deleted value is 11Select option: 3Contents of queue are:12 14Select option: 2Deleted value is 12Select option: 2Deleted value is 14Select option: 2Queue is EmptyDeleted value is -1Select option: 3Contents of queue are:Select option: 4Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU44 45. [email protected] 18Write a C program that implements the following sorting methods to sort a given list of integers inascendingorderi) Bubble sortii) Selection sortBubble Sort#include#define MAX 100void main(){ int arr[MAX],n,temp,i,j; void read1D(),print1D(),bubble();printf("nEnter number of terms: ");scanf("%d",&n);read1D(arr,n);bubble(arr,n);print1D(arr,n);}void bubble(int a[],int n){ int i,j,temp;for(i=0;ia[j+1]){temp=a[j];a[j]=a[j+1];a[j+1]=temp;}}}void read1D(int arr[],int n){ int i;printf("nEnter %d valuesn",n);for(i=0;ivoid main(){ int arr[100],n,val,loc; void read1D(); int lsearch();printf("nHowmany elements you want to enter? ");scanf("%d",&n);read1D(arr,n);printf("nEnter element to be searched: ");scanf("%d",&val);Srinivas Reddy Amedapu, Full Time Research Scholar, CSE DepartmentNATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU46 47. [email protected]=lsearch(arr,n,val);printf("n%d availabel at %d location",val,loc);}int lsearch(int arr[],int n,int val){int i;for(i=0;ivoid main(){ int arr[100],n,val,loc; void read1D(); int bsearch();printf("nHowmany elements you want to enter? ");scanf("%d",&n);read1D(arr,n);printf("nEnter element to be searched: ");scanf("%d",&val);loc=bsearch(arr,0,n-1,val);printf("n%d availabel at %d location",val,loc);}int bsearch(int arr[],int lb,int ub,int val){int mid;while(lb

of 55/55
[email protected] Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 1 Computer Programming Lab Solutions (as per JNTU Hyderabad Syllabus) Prepared by: Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU Date: January 04, 2013 Email: [email protected] , Phone: 08220 172 182 (TN), 09490 456 987 (AP) Week I (a) Write a program to find the sum of individual digits of a positive integer. #include<stdio.h> void main() { int num,d,sum,temp; printf("\nEnter a positive integer : "); scanf("%d",&num); temp=num; sum=0; while(num) { d=num%10; sum=sum+d; num/=10; } printf("\nSum of digits of %d is %d",temp,sum); } Enter a positive integer : 1234 Sum of digits of 1234 is 10 Week I (b) A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence. #include<stdio.h> void main() { int f1,f2,f3,n,i; printf("\nHow many Fibonacci Sequence numbers you want? "); scanf("%d",&n); f1=0, f2=1; printf("\nThe follwing are %d Fibonacci Numbers\n",n);
Embed Size (px)
Recommended