Sei sulla pagina 1di 48

WIPRO TECHNOLOGIES

Confidential
Program Evaluation Tool Solution

CONTENTS
Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program Program 1: Write a program that concatenates two strings. 2: Write a program that reverses an integer. 3: Write a program that computes an expression 4: Write a program that prints out the first 'n' numbers in the Fibonacci sequence. 5: Write a program that accepts two numbers as command-line arguments compute their L.C.M (Least Common Multiple) and print it. 6: Write a program that prints the transpose of a matrix 7: Write a program to find the sum of series 1+x+x2+x3+x4+....+xn. 8: Write a program to print the even numbers. 9: Write a program to compute the sum of natural numbers. 10: Write a Program that prints out the sum of the integers in the file. 11: Write a Program that prints out the words in a file in reverse. 12: Write a program that sort out the words in a file lexicographically. 13: Write a program that will print all possible combination of letters in a word lexicographically. 14: Write a program to print a field spanning from 5th to 14th bit positions. 15: Write a program to display the given file contents in rank order. 16: Write a program to print the name of all the students who have passed the PRP exam from a UNIX batch. 17: Write a program to create a binary search tree. 18: Write a program to create a linked list. 19: Write a program to create a stack using linked list. 20: Write a program to create a queue using linked list 21: Write a program to create a doubly linked list and display it. 22: Write a program to convert ip address to hexadecimal. 23: Write a program to find a word in the given file that is lexicographically sorted. 24: Write a program to find missing number in Fibonacci series. 25: Write a program to delete from a linked list. 26: Write a program to insert into a linked list. 27: Write a program to find missing number in Arithmetic Progression.

Page 2

Program 28: Write a program to overwrite the existing node in a singly linked list. Program 29: Program to create stack using Link List. Program 30: Program to check whether the given string is an anagram or not. Program 31: Program to print the numbers below average marks. Program 32: Program to print square of Fibonacci series. Program 33: Program to delete a node from single link list Program 34: Write a program to add the sum of an each row with each row elements in a matrix, respectively. Program 35: Write a program to add a new node before the given node in the singly linked list. Program 36: Write a Program to count the number of occurrences of digits and alphabets in a given file without repetitions of each occurrence. Program 37: Write a Program to identify a given matrix as a Square matrix or Rectangular matrix. Program 38: String in string Program 39: Sum of either side of diagonal Program 40: Sum Of Series x-x^3/!3+x^5/!5+......... Program 41: Palindrome Program 42: Matrix addition Program 43: Adding row values and creating columns Program 44: 90 degree matrix

Page 3

Program 1: Write a program that concatenates two strings


#include<stdio.h> #include<conio.h> #include<string.h> int main(int argc,char *argv[]) { if(argc<3) { return 1; } else { strcat(argv[1],argv[2]); printf("%s",argv[1]); } return 0; }

Program 2: Write a program that reverses an integer.


#include<stdio.h> #include<conio.h> main(int argc,char *argv[]) { int num; int r,s; num=atoi(argv[1]); s=0; while(num>0) { r=num%10; num=num/10; s=s*10+r; } printf("%d",s); return 0; }

Program 3: Write a program that computes an expression


#include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]) { int a,b,c,d,f; a=atoi(argv[1]); b=atoi(argv[2]); c=atoi(argv[3]); d=atoi(argv[4]); f=2*a*5*b+c-6*d*5*c ;

Page 4

printf("%d",f); return 0; }

Program 4: Write a program that prints out the first 'n' numbers in the Fibonacci sequence.
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> int main(int argc,char *argv[]) { int prev=1,next=1,limit,i=0; limit=atoi(argv[1]); if(limit==0) { printf("\n Should be greater than 0"); return 1; } while(i<limit) { printf("%d\n",prev); next=prev+next; prev=next-prev; i++; } return 0; }

Program 5: Write a program that accepts two numbers as command-line arguments compute their L.C.M (Least Common Multiple) and print it.
#include<stdio.h> #include<stdlib.h> int gcd(int ,int ); int main(int argc,char*argv[]) { int Gcd,lcm; int n1=atoi(argv[1]); int n2=atoi(argv[2]); Gcd=gcd(n1,n2); lcm=(n1*n2)/Gcd; printf("%d",lcm); return 0; } int gcd(int m1,int m2) { int num, rem, den; if(m1>m2) { num=m1; den=m2;

Page 5

} else {

} rem=num%den; while(rem!=0) { num=den; den=rem; rem=num%den; } return den; }

num=m2; den=m1;

Program 6: Write a program that prints the transpose of a matrix


#include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]) { char ch,c[6]; int i=0,j=0,k=0,m,n,a[10][10]; FILE *f; f=fopen(argv[1],"r"); while((ch=fgetc(f))!=EOF) { if(ch=='\n'||ch==' ') { c[k]='\0'; a[i][j]=atoi(c); k=0; j++; if(ch=='\n') { j=0; i++; continue; } } else { c[k]=ch; k++; } } fclose(f); c[k]='\0'; a[i][j]=atoi(c); for(m=0;m<=i;m++) { for(n=0;n<=j;n++) { printf("\t%d",a[n][m]); }

Page 6

printf("\n"); } return 0;

Program 7: Write a program to find the sum of series 1+x+x2+x3+x4+....+xn.


#include<stdio.h> #include<conio.h> #include<math.h> void main(int argc,char *argv[]) { int i,x,n,sum=0; x=atoi(argv[1]); n=atoi(argv[2]); for(i=0;i<=n;i++) { sum=sum+pow(x,i); } printf("%d",sum); }

Program 8: Write a program to print the even numbers.


#include<stdio.h> #include<conio.h> void main(int argc,char *argv[]) { int limit,i=2; limit=atoi(argv[1]); while(i<limit) { printf("%d ",i); i=i+2; } }

Program 9: Write a program to compute the sum of natural numbers.


#include<stdio.h> #include<conio.h> int main(int argc,char *argv[]) { int limit,sum,num; sum=0; limit=atoi(argv[1]); num=1; while(num<=limit) { sum=sum+num;

Page 7

num++; } printf("%d",sum); return 0; }

Program 10: Write a Program that prints out the sum of the integers in the file.
#include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]) { FILE *fp; int sum=0,num=0; fp=fopen(argv[1],"r"); while((fscanf(fp,"%d",&num))!=EOF) { sum+=num; } printf("%d",sum); return 0; }

Program 11: Write a Program that prints out the words in a file in reverse.
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(int argc,char *argv[]) { FILE *fp; char ch,a[1000]; int i=0,j=0; fp=fopen(argv[1],"r"); while((ch=fgetc(fp))!=EOF) { if(ch=='\n') { a[i]='\0'; i=strlen(a); for(j=i-1;j>=0;j--) printf("%c",a[j]); printf("\n"); i=0; } else { a[i]=ch; i++; } } a[i]='\0'; i=strlen(a);

Page 8

for(j=i-1;j>=0;j--) { printf("%c",a[j]); } return 0;

Program 12: Write a program that sort out the words in a file lexicographically.
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(int argc,char *argv[]) { int i,j,len=0; char *temp,t[100]; char *a[100]; FILE *fp=fopen(argv[1],"r"); while(fscanf(fp,"%s",t)!=EOF) { a[len++]=strdup(t); } for(i=0;i<len-1;++i) { for(j=0;j<len-i-1;++j) { if(stricmp(a[j],a[j+1])>0) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } else if(stricmp(a[j],a[j+1])==0) { if(strcmp(a[j],a[j+1])<0) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } for(i=0;i<len;++i) {printf("\n%s",a[i]);} return 0; }

Program 13: Write a program that will print all possible combination of letters in a word lexicographically.
#include<stdio.h> #include<stdlib.h> #include<string.h> void scramble(char *,int);

Page 9

void sort(char ch[20]); char b[10]; int l; int main(int argc,char *argv[]) { sort(argv[1]); } void sort(char ch[20]) { int i,j,len; int n; char temp; len=strlen(ch); for(i=0;i<len;i++) { for(j=0;j<=i;j++) { if(ch[i]<ch[j]) { temp=ch[i]; ch[i]=ch[j]; ch[j]=temp; } } } scramble(ch,len); } void scramble(char *str,int n){ int i,r,k=0; char x[10]; r=0; if(n==1){ for(i=0;i<l;i++) printf("%c",b[i]); printf("%c\n",str[0]); }else{ k=0; for(i=0;i<n;i++){ for(k=0,r=0;r<n;r++){ if(i!=r) x[k++]=str[r]; } b[l]=str[i]; l++; scramble(x,n-1); l--; }

Page 10

Program 14: Write a program to print a field spanning from 5th to 14th bit positions.
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> int main(int argc,char *argv[]) { char inp[20],bin[200]=" ",resbin[200]=" " ; char *p; int i,j=0,dec=0,k; if(argv[1][0]=='0'&&argv[1][1]=='x') strcpy(inp,argv[1]+2); else strcpy(inp,argv[1]); for(p=inp;*p!='\0';++p) { switch(*p) { case '0': strcat(bin,"0000"); break; case '1': strcat(bin,"0001"); break; case '2': strcat(bin,"0010"); break; case '3': strcat(bin,"0011"); break; case '4': strcat(bin,"0100"); break; case '5': strcat(bin,"0101"); break; case '6': strcat(bin,"0110"); break; case '7': strcat(bin,"0111"); break; case '8': strcat(bin,"1000"); break; case '9': strcat(bin,"1001"); break; case 'A': strcat(bin,"1010"); break;

Page 11

} } k=strlen(bin); for(i=strlen(bin)-5;i>=strlen(bin)-14;--i) resbin[j++]=bin[i]; resbin[j]='\0'; strrev(resbin); for(j=0,i=strlen(resbin)-1;i>=0;--i,++j) if(resbin[i]=='1') dec=dec+pow((double)2,(double)j); printf("\n%X",dec); return(0);

case 'B': strcat(bin,"1011"); break; case 'C': strcat(bin,"1100"); break; case 'D': strcat(bin,"1101"); break; case 'E': strcat(bin,"1110"); break; case 'F': strcat(bin,"1111"); break;

Program 15: Write a program to display the given file contents in rank order.
#include<stdio.h> #include<string.h> #include<stdlib.h> struct stud { char ch[20]; int roll; int marks; }; static int tot; int main(int argc,char*argv[]) { FILE *fp; struct stud a; struct stud s[100]; char temp[20]; int temp2=0; int temp1=0; int i=0,j=0; fp=fopen(argv[1],"r"); while(fscanf(fp,"%s%d%d",&a.ch,&a.roll,&a.marks)!=EOF) { strcpy(s[i].ch,a.ch); s[i].roll=a.roll;

Page 12

s[i].marks=a.marks; i++; } tot=i; for(i=0;i<tot;i++) { for(j=0;j<tot;j++) { if(s[i].marks>s[j].marks) { temp2=s[i].marks; s[i].marks=s[j].marks; s[j].marks=temp2; strcpy(temp,s[i].ch); strcpy(s[i].ch,s[j].ch); strcpy(s[j].ch,temp); temp1=s[i].roll; s[i].roll=s[j].roll; s[j].roll=temp1;

} }

} for(i=0;i<tot;i++) { printf("%s\t%d\t%d\n",s[i].ch,s[i].roll,s[i].marks); } return 0;

Program 16: Write a program to print the name of all the students who have passed the PRP exam from a UNIX batch.
#include<stdio.h> #include<stdlib.h> #include<string.h> int main(int argc,char *argv[]) { FILE *f,*f1; int marks[100],i=0,n,m; char nm[100][100],nm1[100][100],sub[100][100],str[200]; f=fopen(argv[1],"r"); f1=fopen(argv[2],"r"); while(fscanf(f,"%s%d",&nm[i],&marks[i])!=EOF) { i++; } fclose(f); i=0; while(fscanf(f1,"%s%s",&nm1[i],&sub[i])!=EOF) { i++; } fclose(f1); for(m=0;m<=i;m++)

Page 13

} return 0; }

if(marks[m]>=70&&strcmp(sub[m],"unix")==0&&strcmp(nm[m],nm1[m])==0) { printf("%s\n",nm[m]); }

Program 17: Write a program to create a binary search tree.


#include<stdafx.h> #include<stdio.h> #include<stdlib.h> struct SBst { int iData; struct SBst* psLeft; struct SBst* psRight; }; typedef struct SBst theNode; /* psRoot will point to the root of the Binary search tree */ theNode* theRoot=NULL; /*Function Prototypes */ void fnCreateTree(FILE*); void fnInorderTraversal(theNode*); int main(int iArgc, char* pcArgv[]) { FILE *fpFilePointer; fpFilePointer=fopen(pcArgv[1],"r"); fnCreateTree(fpFilePointer); printf("Inorder traversal of the Binary search tree is\n"); fnInorderTraversal(theRoot); return (0); }

/* fnCreateTree will read the contents of the file and create a binary search tree */ void fnCreateTree(FILE* fpFilePointer) { theNode *newNode,*temp,*temp2; int num=0,flag=0; while(fscanf(fpFilePointer,"%d",&num)!=EOF) { newNode=(theNode*)malloc(sizeof(theNode)); newNode->iData=num; newNode->psLeft=NULL; newNode->psRight=NULL; temp=theRoot; temp2=temp;

Page 14

if(theRoot==NULL) { theRoot=newNode; } continue;

while(temp) { temp2=temp; //printf("%d %d\n",temp->data,newNode->data); if(temp->iData>newNode->iData) { temp=temp->psLeft; //printf("left\n"); flag=0; } else { temp=temp->psRight; //printf("right\n"); flag=1; } } if(flag==0) {temp2->psLeft=newNode; //printf("added on left \n"); }else { temp2->psRight=newNode; //printf("added on right \n"); } } return ; } /*fnInorderTraversal will Traverse the created binary search tree in Inorder*/ void fnInorderTraversal(theNode* psNode) { if(psNode!=NULL) { fnInorderTraversal(psNode->psLeft); printf("%d ",psNode->iData); fnInorderTraversal(psNode->psRight); } return; }

Program 18: Write a program to create a linked list.


Page 15

#include #include #include #include

<stdio.h> <stdlib.h>7 <string.h> <malloc.h>

struct Node {

int info; struct Node *next;

}; typedef struct Node ll; ll *theHead=NULL; //theRoot=(tree *)malloc(sizeof(tree)); void createLL(FILE *); int main(int argc,char* argv[]) { FILE *fp; ll *p=NULL; fp=fopen(argv[1],"r"); createLL(fp); fclose(fp); printf("Created linked list is\n"); p=theHead; while(p!=NULL) { printf("%d ",p->info); p=p->next; } return 0; } void createLL(FILE *fp) { ll *nLink,*temp; int num=0; while(fscanf(fp,"%d",&num)!=EOF) { nLink=(ll*)malloc(sizeof(ll)); nLink->info=num; nLink->next=NULL; temp=theHead; if(theHead==NULL) { theHead=nLink; continue; } while(temp->next!=NULL) { temp=temp->next; } temp->next=nLink;

Page 16

} } return ;

Program 19: Write a program to create a stack using linked list.


#include #include #include #include #include <stdio.h> <stdlib.h> <conio.h> <string.h> <malloc.h>

struct Node { int info; struct Node *next; }; typedef struct Node Stack; Stack *theTop=NULL; void createStack(FILE *); int main(int argc,char* argv[]) { FILE *fp; Stack *p=NULL; fp=fopen(argv[1],"r"); createStack(fp); fclose(fp); printf("Stack contents are\n"); p=theTop; while(p!=NULL) { printf("%d ",p->info); p=p->next; } return 0;

} void createStack(FILE *fp) { Stack *nLink; int num=0; while(fscanf(fp,"%d",&num)!=EOF) { nLink=(Stack*)malloc(sizeof(Stack)); nLink->info=num; nLink->next=NULL; if(theTop==NULL) { theTop=nLink; continue; } nLink->next=theTop;

Page 17

theTop=nLink; } } return ;

Program 20: Write a program to create a queue using linked list


#include<stdafx.h> #include<stdio.h> #include<stdlib.h> struct SQueue { int iData; struct SQueue* psLink; }; typedef struct SQueue theQueue; /* theRear is a pointer at the rear end of the queue */ theQueue* theRear=NULL; /* theFront is a pointer at the front end of the queue */ theQueue* theFront=NULL; /* Function Prototypes */ void fnCreateQueue(FILE*); void fnDisplay(); void fnInsert(int); int main(int iArgc, char* pcArgv[]) { FILE* fpFile; fpFile=fopen(pcArgv[1],"r"); fnCreateQueue(fpFile); printf("Queue Before Inserting\n"); fnDisplay(); fnInsert(atoi(pcArgv[2])); printf("\nQueue After Inserting\n"); fnDisplay(); return (0); } /* fnDisplay will display the content of the queue from front to rear */ void fnDisplay() { theQueue* psTraverse; psTraverse=theFront; while(psTraverse) { printf("%d ",psTraverse->iData); psTraverse=psTraverse->psLink; }

Page 18

} /* fnCreateQueue will read the data from the file and put it in the queue */ void fnCreateQueue(FILE* fp) { theQueue *nLink; int num=0; while(fscanf(fp,"%d",&num)!=EOF) { nLink=(theQueue*)malloc(sizeof(theQueue)); nLink->iData=num; nLink->psLink=NULL; if(theFront==NULL) { theFront=nLink; theRear=nLink; continue; } theRear->psLink=nLink; theRear=nLink; } return ; } /* fnInsert will insert the data in the queue */ void fnInsert(int iInsertingData) { theQueue *nLink; nLink=(theQueue*)malloc(sizeof(theQueue)); nLink->iData=iInsertingData; nLink->psLink=NULL; theRear->psLink=nLink; theRear=nLink; return; }

Program 21: Write a program to create a doubly linked list and display it.
//Add a new node after the given node in Doubly Linked List #include<stdafx.h> #include<stdio.h> #include<stdlib.h> struct SDll { int iData; struct SDll* psLeft; struct SDll* psRight; };

Page 19

typedef struct SDll theNode; /* theHead will point to the start of the doubly linked list */ theNode* theHead=NULL; /* Function Prototypes */ void fnCreateList(FILE*); void fnDisplay(theNode*); void fnAddNode(int,int); int main(int iArgc,char* pcArgv[]) { FILE* fpFileName; fpFileName=fopen(pcArgv[1],"r"); fnCreateList(fpFileName); printf("Doubly linked list before adding is\n"); fnDisplay(theHead); fnAddNode(atoi(pcArgv[2]),atoi(pcArgv[3])); printf("\nDoubly linked list after adding is\n"); fnDisplay(theHead); return (0); } /* fnDisplay will display the contents of the doubly linked list from start to end */ void fnDisplay(theNode* theHead) { while(theHead!=NULL) { printf("%d ",theHead->iData); theHead=theHead->psRight; } } void fnCreateList(FILE *fp) { theNode *nLink,*temp; int num=0; while(fscanf(fp,"%d",&num)!=EOF) { nLink=(theNode*)malloc(sizeof(theNode)); nLink->iData=num; nLink->psLeft=NULL; nLink->psRight=NULL; temp=theHead; if(theHead==NULL) { theHead=nLink; continue; } while(temp->psRight!=NULL) { temp=temp->psRight; } temp->psRight=nLink; nLink->psLeft=temp; }

Page 20

return ; } void fnAddNode(int iAddItem,int iAfterThisNode) { theNode *nLink,*temp,*temp2=NULL; int num=0; nLink=(theNode*)malloc(sizeof(theNode)); nLink->iData=iAddItem; nLink->psLeft=NULL; nLink->psRight=NULL; temp=theHead; while(temp->iData!=iAfterThisNode) { temp2=temp; temp=temp->psRight; } temp2=temp; temp=temp->psRight; nLink->psRight=temp; nLink->psLeft=temp2; temp->psLeft=nLink; temp2->psRight=nLink; return ; } void fnSearchNode(int iSearchItem) { theNode *temp; temp=theHead; while(temp->iData!=iSearchItem) { temp=temp->psRight; } if(temp->iData==iSearchItem) { printf("YES"); } else { printf("NO"); } return; }

Program 22: Write a program to convert ip address to hexadecimal.


#include<stdio.h> #include<string.h> #include<math.h>

Page 21

#include<stdlib.h>

int main(int argc, char* argv[]) { char outbuf[3]={0}; char a[4]; int x=0,i=0,j=0,len=0; //argv[1]="255.0.0.0"; //printf("%s\n",argv[1]); len=strlen(argv[1]); while(i<=len) { if(argv[1][i]!='.' && i<len) { a[j]=argv[1][i]; j++; a[j]='\0'; } else { x=atoi(a); sprintf(outbuf,"%X", x); strcpy(outbuf,strlwr(outbuf)); printf("%s", outbuf); x=0; j=0; } i++; } return 0; }

Program 23: Write a program to find a word in the given file that is lexicographically sorted.
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(int argc,char *argv[]) { FILE *fp; char a[100]; int i=0,flag=0,len =0; fp=fopen(argv[1],"r"); i=0; while(fscanf(fp,"%s",&a)!=EOF) { len=strlen(a); while(i<len-1) { if((int)a[i]>(int)a[i+1]) {

Page 22

flag=1; } i++;

} return 0;

if(flag==0) { printf("%s\n",a); } flag=0; i=0;

Program 24: Write a program to find missing number in fibbonacci series.


#include<stdio.h> #include<stdlib.h> int main(int argc, char* argv[]) { int prev=1,next=1,num=0; FILE *fp; fp=fopen(argv[1],"r"); while(fscanf(fp,"%d",&num)!=EOF) { if(num!=prev) { printf("%d",prev); return 0; } next=prev+next; prev=next-prev; } return 0; }

Program 25: Write a program to delete from a linked list.


#include #include #include #include #include <stdio.h> <stdlib.h> <conio.h> <string.h> <malloc.h>

struct Node { int info; struct Node *next;

Page 23

}; typedef struct Node ll; ll *theHead=NULL; void createLL(FILE *); void deleteLL(int,int ); int main(int argc,char* argv[]) { FILE *fp; ll *p=NULL; fp=fopen(argv[1],"r"); createLL(fp); fclose(fp); printf("Created linked list is\n"); p=theHead; while(p!=NULL) { printf("%d ",p->info); p=p->next; } deleteLL(atoi(argv[2]) ,atoi(argv[3])); printf("\n\n\n"); p=theHead; while(p!=NULL) { printf("%d ",p->info); p=p->next; } return 0; } void createLL(FILE *fp) { ll *nLink,*temp; int num=0; while(fscanf(fp,"%d",&num)!=EOF) { nLink=(ll*)malloc(sizeof(ll)); nLink->info=num; nLink->next=NULL; temp=theHead; if(theHead==NULL) { theHead=nLink; continue; } while(temp->next!=NULL) { temp=temp->next; } temp->next=nLink;

} return ; }

Page 24

void deleteLL(int iData,int oData) { ll *temp; temp=theHead; while(temp->next!=NULL) { if(temp->info==iData) { temp->info=oData; return ; } temp=temp->next; } return ;

Program 26: Write a program to insert into a linked list.


#include #include #include #include #include #include <stdafx.h> <stdio.h> <stdlib.h> <conio.h> <string.h> <malloc.h>

struct Node { int info; struct Node *next; }; typedef struct Node ll; ll *theHead=NULL; void BinsertLL(int,int); void AinsertLL(int,int); void createLL(FILE *); int main(int argc,char* argv[]) { FILE *fp; ll *p=NULL; fp=fopen(argv[1],"r"); createLL(fp); fclose(fp); printf("Created linked list is\n"); p=theHead; while(p!=NULL) { printf("%d ",p->info); p=p->next; }

Page 25

} void createLL(FILE *fp) { ll *nLink,*temp; int num=0; while(fscanf(fp,"%d",&num)!=EOF) { nLink=(ll*)malloc(sizeof(ll)); nLink->info=num; nLink->next=NULL; temp=theHead; if(theHead==NULL) { theHead=nLink; continue; } while(temp->next!=NULL) { temp=temp->next; } temp->next=nLink; } } return ;

printf("\n\n\n"); insertLL(atoi(argv[2]),atoi(argv[3])); printf("\n\n\n"); p=theHead; while(p!=NULL) { printf("%d ",p->info); p=p->next; } return 0;

void BinsertLL(int iData,int oData) { ll *temp,*temp2,*nLink; nLink=(ll*)malloc(sizeof(ll)); nLink->info=oData; nLink->next=NULL; temp=theHead; temp2=temp; while(temp->info!=NULL) { if(temp->info==iData) { nLink->next=temp; temp2->next=nLink; return ; } temp2=temp;

Page 26

temp=temp->next; } return ; } void AinsertLL(int iData,int oData) { ll *temp,*nLink; nLink=(ll*)malloc(sizeof(ll)); nLink->info=oData; nLink->next=NULL; temp=theHead; while(temp->next!=NULL) { if(temp->info==iData) { nLink->next=temp->next; temp->next=nLink; return ; } temp=temp->next; } return ; }

Program 27: Write a program to find missing number in Arithmetic Progression.


#include<stdio.h> #include<stdlib.h> int main(int argc, char* argv[]) { int a=4,d=4,num=0,b[100],i=0; FILE *fp; fp=fopen(argv[1],"r"); while(fscanf(fp,"%d",&num)!=EOF) { b[i]=num; i++; } a=0; d=b[1]-b[0]; fseek(fp,0,0); while(fscanf(fp,"%d",&num)!=EOF) { a=a+d;

Page 27

if(a!=num) { printf("%d",a); return 0; } } fclose(fp); return 0;

Program 28: Write a program to overwrite the existing node in a singly linked list.
#include<stdio.h> #include<stdlib.h> struct SSll { int iData; struct SSll* psNext; }; typedef struct SSll theNode; /* theHead will point to the start of the singly linked list */ theNode* theHead=NULL; /*Function Prototype */ void fnCreateList(FILE*); void fnDisplay(theNode*); void fnOverwrite(int,int); int main(int iArgc,char* pcArgv[]) { FILE* fpFile; fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile); printf("Singly linked list before overwrite the node\n"); fnDisplay(theHead); fnOverwrite(atoi(pcArgv[2]),atoi(pcArgv[3])); printf("\nSingly linked list after overwrite the node\n"); fnDisplay(theHead); return (0); } /* fnDisplay will display the singly linked listed from start to end */ void fnDisplay(theNode* psTraverse) { while(psTraverse!=NULL) { printf("%d ",psTraverse->iData); psTraverse=psTraverse->psNext; } }

Page 28

/* fnCreateList will read the data from the file and create a singly linked list */ void fnCreateList(FILE* fpFile) { theNode *nLink,*temp; int num=0; while(fscanf(fpFile,"%d",&num)!=EOF) { nLink=(theNode*)malloc(sizeof(theNode)); nLink->iData=num; nLink->psNext=NULL; temp=theHead; if(theHead==NULL) { theHead=nLink; continue; } while(temp->psNext!=NULL) { temp=temp->psNext; } temp->psNext=nLink; } fclose(fpFile); return ; } /* fnOverwrite will overwrite the node based on the given node in the singly linked list */ void fnOverwrite(int iFindData, int iOverwriteData) { /*Paste your code here to overwrite the existing node in singly linked list */ theNode *temp=NULL; temp=theHead; while(temp->iData!=iFindData) { temp=temp->psNext; } temp->iData=iOverwriteData; return ; }

Program 29: Program to create stack using Link List.


#include<stdafx.h> #include<stdio.h> #include<stdlib.h> struct SStack { int iStackData;

Page 29

struct SStack* psLink; }; typedef struct SStack theStack; /*Top will point to the Top position of the Stack */ theStack* theTop=NULL; /* Function Prototypes */ void fnCreateStack(FILE*); void fnDisplayStack(void); void fnPop(int); int fnCountStack(void); int main(int iArgc, char *pcArgv[]) { /*fpFile will hold the pointer to the file where the contents to be put on the stack are saved */ FILE* fpFile; fpFile=fopen(pcArgv[1],"r"); fnCreateStack(fpFile); printf("Stack contents before popping are\n"); fnDisplayStack(); fnPop(atoi(pcArgv[2])); printf("\nStack contents after popping are\n"); fnDisplayStack(); printf("\nCount of stack after popping is %d",fnCountStack()); return (0); } /* fnDisplayStack will display the contents of the stack from top to bottom */ void fnDisplayStack() { theStack* Traverse; Traverse=theTop; while(Traverse) { printf("%d ",Traverse->iStackData); Traverse=Traverse->psLink; } }

/*fnCreate Stack will read the contents from file, create the stack dynamically with the contents from the stack*/ void fnCreateStack(FILE* fp) { theStack *nLink; int num=0; while(fscanf(fp,"%d",&num)!=EOF) { nLink=(theStack*)malloc(sizeof(theStack)); nLink->iStackData=num; nLink->psLink=NULL; if(theTop==NULL)

Page 30

} nLink->psLink=theTop; theTop=nLink; } return ; } /*fnPop will remove the node from the list till it reaches to the searching number*/ void fnPop(int ifindNo) { while(theTop->iStackData!=ifindNo) { theTop=theTop->psLink; } theTop=theTop->psLink; } /* fnCountStack will return the number of elements in the stack */ int fnCountStack() { int i=0; while(theTop!=NULL) { theTop=theTop->psLink; i++; } //printf("%d",i); return i; }

theTop=nLink; continue;

Program 30: Program to check whether the given string is a anagram or not.
#include<stdafx.h> #include<stdio.h> #include<stdlib.h> #include<string.h> char *sort(char ch[20]); int main(int argc,char *argv[]) { char a[100],b[100]; strcpy(a,sort(argv[1]));

Page 31

strcpy(b,sort(argv[2])); if(strcmp(a,b)==0) { printf("YES"); } else { printf("NO"); } return 0; } char* sort(char ch[20]) { int i,j,len; int n; char temp; len=strlen(ch); for(i=0;i<len;i++) { for(j=0;j<=i;j++) { if(ch[i]<ch[j]) { temp=ch[i]; ch[i]=ch[j]; ch[j]=temp; } } } return ch; }

Program 31: Program to print the numbers below average marks.


#include<stdio.h> int main(int argc, char* argv[]) { int num=0,sum=0,i=0,a[100],j=0,len=0; FILE *fp; fp=fopen(argv[1],"r"); while(fscanf(fp,"%d",&num)!=EOF) { sum=sum+num; //printf("%d\n",num); a[i]=num; i++; } len=i; for(i=0;i<len;i++) { for(j=0;j<=i;j++)

Page 32

if(a[i]<a[j]) {

a[i]=a[i]+a[j]; a[j]=a[i]-a[j]; a[i]=a[i]-a[j];

} } }

sum=sum/len; for(j=0;j<len &&a[j]<sum;j++) { printf("%d ",a[j]); } fclose(fp); return 0; }

Program 32: Program to print square of Fibonacci series.


#include<stdio.h> #include<stdlib.h> int main(int argc, char* argv[]) { int prev=1,next=1,num=0,limit=0; limit=atoi(argv[1]); while(prev<limit) { printf("%d ",prev*prev); next=prev+next; prev=next-prev; } return 0; }

Program 33: Program to delete a node from single link list


void fnCreateList(FILE* fpFile) { theNode *nLink,*temp; int num=0; while(fscanf(fpFile,"%d",&num)!=EOF) { nLink=(theNode*)malloc(sizeof(theNode)); nLink->iData=num; nLink->psNext=NULL; temp=theHead; if(theHead==NULL)

Page 33

{ }

theHead=nLink; continue;

while(temp->psNext!=NULL) { temp=temp->psNext; } temp->psNext=nLink; } fclose(fpFile); return ; } void fnDeleteNode(int iDeleteData) { theNode *temp,*temp2; temp=theHead; temp2=temp; while(temp->psNext->iData!=iDeleteData) { temp2=temp; temp=temp->psNext; } temp2->psNext=temp->psNext; temp->psNext=NULL; free(temp); return; }

Program 34: Write a program to add the sum of a each row with each row elements in a matrix, respectively.
#include<stdafx.h> #include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]) { char ch,c[6]; int i=0,j=0,k=0,m,n,a[10][10],sum[10]={0,0,0,0,0,0,0,0,0,0}; FILE *f; f=fopen(argv[1],"r"); while((ch=fgetc(f))!=EOF) { if(ch=='\n'||ch==' ') { c[k]='\0'; a[i][j]=atoi(c); sum[i]=sum[i]+a[i][j]; k=0;

j++;

Page 34

if(ch=='\n') { j=0; i++; continue; } } else { } }

c[k]=ch; k++;

fclose(f); c[k]='\0'; a[i][j]=atoi(c); sum[i]=sum[i]+a[i][j]; for(m=0;m<=i;m++) { for(n=0;n<=j;n++) { printf("%d ",a[m][n]+sum[m]); } printf("\n"); } return 0; }

Program 35: Write a program to add a new node before the given node in the singly linked list.
/* fnCreateList will read the data from the file and create a singly linked list */ void fnCreateList(FILE* fpFile) { theNode *nLink,*temp; int num=0; while(fscanf(fpFile,"%d",&num)!=EOF) { nLink=(theNode*)malloc(sizeof(theNode)); nLink->iData=num; nLink->psNext=NULL; temp=theHead; if(theHead==NULL) { theHead=nLink; continue; } while(temp->psNext!=NULL)

Page 35

temp=temp->psNext; } temp->psNext=nLink; } fclose(fpFile); return ; } /* fnAddNode will add a node before the given node in singly linked list */ void fnAddNode(int iAddItem,int iAfterThisNode) { theNode *temp=NULL,*temp2=NULL,*nLink=NULL; temp=theHead; while(temp->iData!=iAfterThisNode) { temp2=temp; temp=temp->psNext; } nLink=(theNode*)malloc(sizeof(theNode)); nLink->iData=iAddItem; nLink->psNext=temp; temp2->psNext=nLink; return ; }

Program 36: Write a Program to count the number of occurances of digits and alphabets in a given file without repetitions of each occurances.
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(int argc,char *argv[]) { FILE *fp; char ch,a[10000]; int sumNo=0,sumAl=0,i=0,j=0,flag=0,n=0; fp=fopen(argv[1],"r"); while((ch=fgetc(fp))!=EOF) { n=(int)ch; if((n>=48&&n<=57)||(n>=97&&n<=122)||(n>=65&&n<=90)) { a[i]=ch; flag=0; for(j=0;j<i;j++) {

Page 36

} if(flag==0) { if((int)ch>=97 && (int)ch<=122) { sumNo++; } else { sumAl++; } } i++; } } printf("%d\n%d",sumNo,sumAl); return 0; }

if(ch==a[j]) { flag=1; break; }

Program 37: Write a Program to identify a given matrix as a Square matrix or Rectangular matrix.
#include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]) { char ch,c[6]; int i=0,j=0,k=0,m,n,a[10][10]; FILE *f; f=fopen(argv[1],"r"); while((ch=fgetc(f))!=EOF) { if(ch=='\n'||ch==' ') { c[k]='\0'; a[i][j]=atoi(c); k=0; j++; if(ch=='\n') { j=0; i++; continue; } } else

Page 37

{ } }

c[k]=ch; k++;

fclose(f); c[k]='\0'; a[i][j]=atoi(c); if(i==j) { } else {

printf("Square matrix");

printf("Rectangular matrix"); } return 0; }

String in string
--------------------------------------------------#include "stdafx.h" #include<conio.h> #include<stdio.h> #include<stdlib.h> #include<string.h> using namespace System; void main(int argc,char * argv[]) { if(strstr(argv[1],argv[2])!=0) printf("yes"); else printf("no");

getch(); }

-----------------------------------Sum of either side of diagonal ------------------------------------#include<conio.h> #include<stdio.h> #include<stdlib.h> using namespace System;

Page 38

void main(int argc,char * argv[]) {int i=0,x,sum1=0,sum2=0; int ar[20][20]; FILE *p; p=fopen(argv[1],"r"); for(int j=0;j<=2;j++) for(int k=0;k<=2&&(fscanf(p,"%d",&x)!=EOF);k++) { ar[j][k]=x; // printf("%d",x); } for(int j=0;j<3;j++) { for(int k=0;k<3;k++) { printf("%d ",ar[j][k]); if(j<k) { sum1=sum1+ar[j][k]; } else if(j>k) { sum2=sum2+ar[j][k]; } } printf("\n"); } printf("\n sum of upper diagonal is %d ",sum1); printf("\n sum of lower diagonal is %d ",sum2); getch(); }

----------------------------------------Sum Of Series x-x^3/!3+x^5/!5+......... -----------------------------------------#include<conio.h> #include<stdio.h> #include<stdlib.h> #include<math.h> using namespace System; float fact(float); void main(int argc,char * argv[1]) { int x=atoi(argv[1]);

Page 39

int n=atoi(argv[2]); float i=1,sum=0,sign=1; while(i<=n+1) { sum=sum+sign*(pow(x,i)/fact(i)); i=i+2; sign=sign*(-1); } printf("sum is %.2f",sum); getch(); } float fact(float f) { if(f<=1) return 1; else return f*fact(f-1); }

Palindrome

#include<stdio.h> #include<conio.h> #include<string.h> void main(int argc,char* argv[]) { char b[100],str[100]; int tmp,i,j,l; FILE *fp; fp=fopen(argv[1],"r"); fscanf(fp,"%s",str); l=strlen(str); tmp=l; b[l]='\0';

Page 40

for(i=0,j=l-1;i<tmp;i++,j--) { b[j]=str[i]; } if((strcmp(str,b)==0)) printf("palindorme"); else printf("Not palindorme"); }

-----------------------------------------------------------------------

20.Matrix addition input from two files

#include<stdio.h> #include<string.h> #include<stdlib.h>

void readmat(char* fa,int mat[][10],int*r,int*c) { char s [100]; FILE *fp=fopen(fa,"r"); int i=0,j=0; char *p; while(fgets(s,100,fp)!=NULL) { Page 41

p=strtok(s," "); mat[i][j]=atoi(p); while((p=strtok(NULL," "))!=NULL) { ++j; mat[i][j]=atoi(p); } ++i; *c=j+1; j=0; } *r=i; }

int main(int argc,char* argv[]) { int a[10][10],b[10][10],r,c,i,j; //FILE *fp1=fopen(argv[3],"w"); readmat(argv[1],a,&r,&c); readmat(argv[2],b,&r,&c); printf("First Matrix read from file1\n"); for(i=0;i<c;i++) { for(j=0;j<r;j++) { printf("%d\t",a[i][j]);

Page 42

} printf("\n"); } printf("Second Matrix read from file2 \n"); for(i=0;i<c;i++) { for(j=0;j<r;j++) { printf("%d\t",b[i][j]); } printf("\n"); } printf("Resultant Matrix printing to another file "); printf("Sum is \n"); for(i=0;i<c;i++) {

for(j=0;j<r;j++) { printf("%d\t",(a[i][j]+b[i][j])); } printf("\n"); }

return 0;

Page 43

43 .adding rows value and creating new column*/ #include<stdio.h> #include<string.h> #include<stdlib.h>

void readmat(char* fa,int mat[][10],int*r,int*c) { char s [100]; int k; FILE *fp=fopen(fa,"r"); int i=0,j=0; char *p; int sum[10]; for(k=0;k<10;k++) { } while(fgets(s,100,fp)!=NULL) { p=strtok(s," "); mat[i][j]=atoi(p); while((p=strtok(NULL," "))!=NULL) { ++j; Page 44 sum[k]=0;

mat[i][j]=atoi(p);

} ++i; *c=j+1; j=0; } *r=i; for(int u=0;u<*r;u++) for(int v=0;v<*c;v++) sum[u]=sum[u]+ mat[u][v];

for(i=0;i<*r;i++) { for(j=0;j<*c;j++) { printf("%d\t",mat[i][j]); } printf("%d\t",sum[i]); printf("\n"); }

int main(int argc,char* argv[])

Page 45

{ int a[10][10],b[10][10],r,c,i,j; printf("First Matrix read from file1\n"); readmat(argv[1],a,&r,&c); printf("Second Matrix read from file2 \n"); readmat(argv[2],b,&r,&c); printf("Resultant Matrix printing to another file "); printf("Sum is \n"); for(i=0;i<c;i++) { for(j=0;j<r;j++) { printf("%d\t",(a[i][j]+b[i][j])); } printf("\n"); } return 0; } -------------------------------------------------------------------------------------------------24.90 degree matrix*/

#include<stdio.h> #include<conio.h> int main() { int a[3][3],b[3][3];

Page 46

int m=2,n=2,i,j; int row=m, col=m-n; for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("The given marix is : \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { } printf("\n"); } for(i=0;i<3;i++) for(j=0;j<3;j++) b[i][j]=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=a[row][col]; row--; } row=m; col=col+1; } printf("\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t",a[i][j]);

Page 47

{ }

printf("%d\t",b[i][j]);

printf("\n"); } return 0; }

Page 48

Potrebbero piacerti anche