VTU 3rd sem CSE/IS lab programs!!!!


Today i am posting DS/OOPS lab programs for 3rd sem ISE\CSE students as per VTU syllabus.I am uploading the '.c/.cpp' file.You can download that to your pc or smartphone and open it  with any text editor.These programs are error free and will definitely execute.The list of the programs will be updated by the time.All the programs below have been executed on Fedora OS via terminal using following commands:-
1) gedit program_name.c/cpp(C for c program and cpp for c++ programs):-This command will open the versatile text editor gedit with a blank file named "program_name.c/cpp".After typing the program save it and close the text editor and return back to terminal.

2) g++ program_name.c/cpp :-This command will compile your program and display the errors/warnings in the terminal if any,found in your program.

3)./a.out :-After your program is compiled successfully this command is used to execute the program.

Prog 6*:Design,develop, and execute a program in C++ to create a class called STRING and implement the following operations .Display the results after every operation by overloading the operator <<.
i.     STRING s1="VTU"
ii.    STRING s2="BELGAUM"
iii.   STRING s3=s1+s2;(Use copy constructor)


#include<iostream>
#include<string.h>
using namespace std;
class STRING
{
private:
        char str[50];
public:
STRING()
{
strcpy(str,"");
}
STRING(char *st)
{
strcpy(str,st);
}
STRING(const STRING &s)
{
strcpy(str,s.str);
}

STRING operator+(STRING);
friend void operator<<(ostream &,STRING);
};
STRING STRING::operator+(STRING s)
{
STRING temp;
strcpy (temp.str,str);
strcat(temp.str,s.str);
return temp;
}
void operator<<(ostream &out,STRING S)
{
out<<S.str;
}
int main()
{
STRING s1="VTU";
cout<<"The first STRING s1 is: ";
cout<<s1;
cout<<endl;
STRING s2="BELGAUM";
cout<<"The second string s2 is: ";
cout<<s2;
cout<<endl;
STRING s3=s1+s2;
cout<<"The final string s3 is: ";
cout<<s3;
cout<<endl;
}

*Note:-After compliling this program you may get the following warning:-
string.cpp: In function ‘int main()’:
string.cpp:38:12: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

string.cpp:42:12: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
But don't worry its just a warning not an error.Just ignore the warning and type"./a.out",you will definitely get the output.

Download string_program.cpp


Prog 8:Design,develop, and execute a program in C++ to create a clss called LINK(linked list) with member functions to insert an element at the front of the list as well as to delete an element from the front of the list.Demonstrate all the functions after creating a list object.

#include<iostream>
#include<string.h>
using namespace std;
class LIST
{
public:
int info;
LIST *next;
};
class LINKED_LIST
{
LIST *head;
public:
LINKED_LIST()
{
head=NULL;
}
void insertf();
void deletef();
void display_list();
};
void LINKED_LIST::insertf()
{
LIST *temp;
int item;
cout<<"enter data\n";
cin>>item;
cout<<endl;
temp=new LIST;
temp->info=item;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
temp->next=head;
head=temp;
}
}
void LINKED_LIST::deletef()
{
LIST *temp;
if(head==NULL)
cout<<"no data present";
else
{
temp=head;
head=head->next;
cout<<"the deleted data is\n"<<temp->info<<endl;
delete temp;
}
}
void LINKED_LIST::display_list()
{
if(head==NULL)
cout<<"no data"<<endl;
for(LIST *temp=head;temp!=NULL;temp=temp->next)
cout<<temp->info<<"->"<<endl;
}
int main()
{
int exit=0;
LINKED_LIST s1;
int ch=1;
while(ch)
{
cout<<"1:insert_front\n2:delete_front\n3:display_list\n4:exit\n";
cout<<"enter the choice:\n";
cin>>ch;
switch(ch)
{
case 1:s1.insertf();
break;
case 2:s1.deletef();
break;
case 3:s1.display_list();
break;
default:cout<<"program terminated!!!\n";
return 0;
}
}
return 0;

}

Download Linked_List program.cpp

Prog 9:Design,develop, and execute aprogram in C to read a sparse matrix of integer value and to search the sparse matrix for an element specified by the user.print the result of the search appropriately.Use the triple <row,column,value> to represent an element in the sparse matrix.

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[20][20],row,col;
int m=0,i,j,flag=0,s;
printf("enter the row and column values\n");
scanf("%d\t%d",&row,&col);
printf("enter array elements\n\n");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(a[i][j]!=0)
{
b[m][0]=i;
b[m][1]=j;
b[m][2]=a[i][j];
m++;
}
printf("sparse matrix is:\n\n");
for(i=0;i<m;i++)
printf("row=%d\tcolumn=%d\tvalue=%d\n",b[i][0],b[i][1],b[i][2]);
printf("enter search element\n\n");
scanf("%d",&s);
for(i=0;i<m;i++)
if(s==b[i][2])
{
printf("successful search\n");
printf("row=%d\tcolumn=%d\tvalue=%d\n",b[i][0],b[i][1],b[i][2]);
flag=1;
}
if(!flag)
printf("unsuccessful search\n");
return 0;

}

Download Sparse matrix.c program


Prog 1:Using circular representation for a polynomial,design,develop,and execute a program in C to accept two polynimials,add them,and then print the resulting polynimial.

#include<stdio.h>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>
struct node
{
float cf,px,py;
int flag;
struct node *link;
};
typedef struct node *NODE;
NODE get_node()
{
NODE X;
X=(NODE)malloc(sizeof(struct node));
if(X==NULL)
{
printf("out of memory");
exit(0);
}
return X;
}
NODE insert(float cf,float x,float y,NODE head)
{
NODE temp,cur;
temp=get_node();
temp->cf=cf;
temp->px=x;
temp->py=y;
temp->flag=0;
cur=head->link;
while(cur->link!=head)
cur=cur->link;
cur->link=temp;
temp->link=head;
return head;
}
NODE read_poly(NODE head)
{
int i;
float cf,px,py;
printf("enter the coefficient as -999 to end of the polynomial\n\n");
for(i=1;;i++)
{
printf("enter the value \n\n");
printf("coeff=");
scanf("%f",&cf);
if(cf==-999)
break;
printf("pow x=");
scanf("%f",&px);
printf("pow y=");
scanf("%f",&py);
head =insert(cf,px,py,head);
}
return head;
}
void display(NODE head)
{
NODE temp;
if(head->link==head)
{
printf("polynomial does not exist\n\n");
return;
}
temp=head->link;
while(temp!=head)
{
printf("%5.2fx^%3.1fy^%3.1f+",temp->cf,temp->px,temp->py);
temp=temp->link;
}
printf("\n");
}
NODE add_poly(NODE h1,NODE h2,NODE h3)
{
NODE p1,p2;
int x1,x2,y1,y2,cf1,cf2,cf;
p1=h1->link;
while(p1!=h1)
{
x1=p1->px;
y1=p1->py;
cf1=p1->cf;
p2=h2->link;
while(p2!=h2)
{
x2=p2->px;
y2=p2->py;
cf2=p2->cf;
if(x1==x2&&y1==y2)
break;
p2=p2->link;
}
if(p2!=h2)
{
cf=cf1+cf2;
p2->flag=1;
if(cf!=0)
h3=insert(cf,x1,y1,h3);
}
else
h3=insert(cf1,x1,y1,h3);
p1=p1->link;
}
p2=h2->link;
while(p2!=h2)
{
if(p2->flag==0)
{
h3=insert(p2->cf,p2->px,p2->py,h3);
}
p2=p2->link;
}
return h3;
}
int main()
{
NODE h1,h2,h3;
h1=get_node();
h2=get_node();
h3=get_node();
h1->link=h1;
h2->link=h2;
h3->link=h3;
printf("enter the first polynomial\n\n");
h1=read_poly(h1);
printf("enter the second polynomial\n\n");
h2=read_poly(h2);
h3=add_poly(h1,h2,h3);
printf("the first polynomial is:\n\n");
display(h1);
printf("the second polynomial is:\n\n");
display(h2);
printf("the sum of the polynomial is:\n\n");
display(h3);
}


Download polynomial addition.c


Prog 7:Design,develop,and execute a program in C++ to create a class called STACK using an array of integers and to implement the following operations by overloading the operators+ and -:

i. s1=s1+element;where s1 is an object of the class STACK and element is an integer to be pushed on to top of the stack.

ii.s1=s1-;where s1 is an object of the class STACK and - operator pops off the top element.

#include<iostream>
using namespace std;
#include<stdlib.h>
class stack
{
private:
int a[10];
int size,top;
public:
stack(int n)
{
top=-1;
size=n;
}
void operator+(int x);
void operator-();
friend ostream&operator<<(ostream&out,stack st);
};
void stack::operator+(int x)
{
if(top==size-1)
cout<<"stack is overflow"<<endl;
else
a[++top]=x;
}
void stack::operator-()
{
if(top==-1)
cout<<"stack is underflow"<<endl;
else
cout<<"The popped element is"<<a[top--]<<endl;
}
ostream&operator<<(ostream & out,stack st)
{
if(st.top==-1)
cout<<"Stack is underflow"<<endl;
else
for(int i=st.top;i>=0;i--)
out<<st.a[i]<<"->";
cout<<endl;
return out;
}
int main()
{
int n,ch,x;
cout<<"Enter the size of stack:"<<endl;
cin>>n;
stack st(n);
while(1)
{
cout<<"1:push\t 2:pop\t 3:display\t 4:exit"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter data to push:";
cin>>x;
st+x;
break;
case 2:
-st;
break;
case 3:
cout<<"The content of stack:";
cout<<st;
break;
default:
cout<<"program is terminated\n";
exit(0);
}
}
return 0;

}


Download stack operation(by operator overloading) prog.cpp

Prog 4: Design,develop,and execute a program in C to simulate the working of a Queue of integers using array.Provide the following operations:
a.Insert                           b.Delete                c.Display

#include<stdio.h>
#include<stdlib.h>
#define queue_size 5
int item,q[20],r,f,ch;
void insert()
{
if(r==queue_size-1)
{
printf("full queue\n");
return;
}
r=r+1;
q[r]=item;
}
void del()
{
if(f>r)
{
printf("Queue Overflow\n");
return;
}
printf("Enter the element to be deleted %d:",q[f++]);
if(f>r)
{
f=0;
r=-1;
}
}
void display()
{
int i;
printf("The queue is:\n");
for(i=f;i<=r;i++)
{
printf("%d\n",q[i]);
}
}
int main()
{
r=-1;
f=0;
for(;;)
{
printf("\n1.Insert\n 2.Delete\n 3.Display\n 4.Exit\n");
printf("Enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the elements\n");
scanf("%d",&item);
insert();
break;
case 2:del();
break;
case 3:printf("---------Display the Queue---------\n");
display();
break;
default:exit(0);
break;
}
}
return 1;

}

Download Queue program.c

Prog 2: Dsign,develop,and execute a prog in C to convert a given valid parethesized infix arithematic expression to postfix expression and then to print boyhe the expressions.The expression consists of single character operands and the binary operators+(plus),-(minus),*(multiply) and /(divide).


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
double compute(char symbol,double op1,double op2)
{
switch(symbol)
{
case'+':return op1+op2;
case'-':return op1-op2;
case'*':return op1*op2;
case'/':return op1*op2;
case'$':
case'^':return pow(op1,op2);
}
}
int main()
{
double s[20],res,op1,op2;
int i,top;
char symbol,postfix[20];
printf("\n Enter postfix expression\n");
scanf("%s",postfix);
top=-1;
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
s[++top]=symbol-'0';
else
{
op2=s[top--];
op1=s[top--];
res=compute(symbol,op1,op2);
s[++top]=res;
}
}
res=s[top--];
printf("\n Result %f\n",res);
}


Prog 3: Design,develop, and execute a prog in C  to evaluate a valid postfix expression using stack.Assume that the postfix expression is read as a single line consisting of non-negative single digit operands and binary arithematic operators.The arithematic operators are +(add),-(substract),*(multiply),/(divide).


#include<stdio.h>
#include<string.h>
int f(char symbol)
{
switch(symbol)
{
case'+':
case'-':return 2;
case'*':
case'/':return 4;
case'^':
case'$':return 5;
case'(':return 0;
case'#':return-1;
default:return 8;
}
}
int g(char symbol)
{
switch(symbol)
{
case'+':
case'-':return 1;
case'*':
case'/':return 3;
case'$':return 6;
case'(':return 9;
case')':return 0;
default:return 7;
}
}
int infix_postfix(char infix[],char postfix[])
{
int top,i,j;
char s[20],symbol;
top=-1;
s[++top]='#';
j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(f(s[top])>g(symbol))
{
postfix[j]=s[top--];
j++;
}
if(f(s[top])!=g(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
}
int main()
{
char infix[20],postfix[20];
printf("\n Enter infix expression\n");
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("\n The postfix:-\n");
printf("\n%s\n",postfix);
return 0;
}

Download infix to postfix conversion prog.c

Prog 5: Design,develop,and execute a program in C++ based on the following requirements:
An EMPLOYEE class is tocontain the following data members and member functions:
Data members:Employee_Number(an integer),Employee_Name(a string of characters),Basic_Salary(An integer),All_Allowances(an integer),IT(an integer),Net_Salary(an integer).
Member functions:to read the data of an employee,to calculate Net_Salary and to print the values of all data members.
(All_Allowances=123% of Basic; Income Tax(IT)=30% of the gross salary(=basic_salary_All_Allowances);Net_Salary=Basic_Salary+All_Allowances-IT)

#include<iostream>
using namespace std;
class employee
{
private:
int employee_no;
int basic_salary;
int all_allowances;
int IT;
int net_salary;
char employee_name[20];
int calc_salary();
public:
void read();
void display();
};
void employee::read()
{
cout<<"Enter the employee's no.\n";
cin>>employee_no;
cout<<"Enter the Name\n";
cin>>employee_name;
cout<<"Enter Basic salary\n";
cin>>basic_salary;
calc_salary();
}
int employee::calc_salary()
{
all_allowances=int(1.23*basic_salary);
IT=int(0.3*(basic_salary+all_allowances));
net_salary=(basic_salary+all_allowances)-IT;
}
void employee::display()
{
cout<<"--------------------------------\n";
cout<<"      Employee's details        \n";
cout<<"--------------------------------\n";
cout<<"Employee's number: "<<employee_no<<"\n";
cout<<"Employee's name: "<<employee_name<<"\n";
cout<<"Employee's basic salary: "<<basic_salary<<"\n";
cout<<"All allowances: "<<all_allowances<<"\n";
cout<<"income Tax: "<<IT<<"\n";
cout<<"Net Salary: "<<net_salary<<"\n";
}
int main()
{
employee a[10];
int n;
cout<<"Enter the no. of employee\n";
cin>>n;
for(int i=0;i<n;i++)
a[i].read();
for(int i=0;i<n;i++)
a[i].display();
return 0;
}

Download EMPLOYEE prog.cpp

If the above download links are not working,please inform me,or comment in the post.I will update it as soon as possible.




1 comment:

Here You can post all your suggetions,reviews,doubts,problems etc.......!