SE DSL Lab Manual Computer Engineering SPPUPVGnASHIK
SE DSL Lab Manual Computer Engineering SPPUPVGnASHIK
LAB MANUAL
DATA STRUCTURE
LABORATORY
Prepared by
PROF. ANAND GHARU
2019 - 20
Downloaded by Ravindra Mule ([email protected])
lOMoARcPSD|23769222
GROUP - B
Department of Computer Engineering has student's club
named 'Pinnacle Club'. Students of Second, third and final
year of department can be granted membership on
request. Similarly one may cancel the membership of
club. First node is reserved for president of club and last
node is reserved for secretary of club. Write C++ program
to maintain club member‘s information using singly
5
linked list. Store student PRN and Name. Write functions
to a) Add and delete the members as well as president or
even secretary.
b) Compute total number of members of club
c) Display members
d) Display list in reverse order using recursion
e) Two linked lists exists for two divisions. Concatenate.
class _______ Sem ____ Roll no. ____ has completed the term work satisfactorily in the
GROUP - A
GROUP - A
Downloaded by Ravindra Mule ([email protected])
lOMoARcPSD|23769222
The instructor‘s manual is to be developed as a hands-on resource and reference. The instructor's
manual need to include prologue (about University/program/ institute/ department/foreword/
preface etc), University syllabus, conduction & Assessment guidelines, topics under consideration-
concept, objectives, outcomes, set of typical applications/practicals/ guidelines, and references.
The laboratory practicals are to be submitted by student in the form of journal. Journal consists of
prologue, Certificate, table of contents, and handwritten write-up of each practical (Title,
Objectives, Problem Statement, Outcomes, software & Hardware requirements, Date of
Completion, Assessment grade/marks and assessor's sign, Theory- Concept in brief, algorithm,
flowchart, test cases, conclusion/analysis. Program codes with sample output of all perform
practical’s are to be submitted as softcopy.
As a conscious effort and little contribution towards Green IT and environment awareness,
attaching printed papers as part of write-ups and program listing to journal may be avoided. Use of
DVD containing students programs maintained by lab In-charge is highly encouraged. For reference
one or two journals may be maintained with program prints at Laboratory.
Continuous assessment of laboratory work is done based on overall performance and lab practicals
performance of student. Each lab practical assessment will assign grade/marks based on parameters
with appropriate weightage. Suggested parameters for overall assessment as well as each lab
practical assessment include- timely completion, performance, innovation, efficient codes,
punctuality and neatness.
Both internal and external examiners should jointly set problem statements. During practical
assessment, the expert evaluator should give the maximum weightage to the satisfactory
implementation of the problem statement. The supplementary and relevant questions may be asked
at the time of evaluation to test the student‘s for advanced learning, understanding of the
fundamentals, effective and efficient implementation. So encouraging efforts, transparent evaluation
and fair approach of the evaluator will not create any uncertainty or doubt in the minds of the
students. So adhering to these principles will consummate our team efforts to the promising start of
the student's academics.
The instructor is expected to frame the practicals by understanding the prerequisites, technological
aspects, utility and recent trends related to the topic. The practical framing policy need to address
the average students and inclusive of an element to attract and promote the intelligent students. The
instructor may set multiple sets of practicals and distribute among batches of students. It is
appreciated if the practicals are based on real world problems/applications. Encourage students for
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
In addition to these, instructor may assign one real life application in the form of a mini- project
based on the concepts learned. Instructor may also set one practical or mini-project that is suitable
to respective branch beyond the scope of syllabus.
Set of suggested practical list is provided in groups- A, B, C, D, and E. Each student must perform
at least 13 practicals as at least 3 from group A, 3 from group B, 2 from group C, 2 from group D
and 3 from group E.
64- bit Open source Linux or its derivative
Operating System recommended :- Open Source C++ Programming tool like G++/GCC
Programming tools recommended: -
INTRODUCTION
This manual is organized in such a way that the students can directly use it in the laboratory. Each
laboratory exercise comprises of
1. Statement of the problem
2. Algorithm
3. C code
4. Results
5. Conclusion
You must follow this sequence to conduct any data structures experiment in the computer laboratory
and write your report accordingly. Using the algorithms given in this section one can implement in
any language at a later time. For example, to implement the stack program in C++ or Java (of
course now you must use C language) simply follow the same algorithmic steps and use additional
language features, if any. This is the reason why the students must write the algorithm first and then
the C code.
INTRODUCTION TO C
C was brought to us by Dennis Ritchie and Brian at Bell Labs. AT&T people had themselves this
Operating System called UNIX that needed a programming language. They made it, and since the
previous version was called B, they decided to call it C for obvious reasons. There was never an A
programming language. The B in B stood for Bell. C is a computer programming language. That
means that you can use C to create lists of instructions for a computer to follow. C is one of
thousands of programming languages currently in use. C has been around for several decades and
has won widespread acceptance because it gives programmers maximum control and efficiency. C
is what is called a compiled language. This means that once you write your C program, you must
run it through a C compiler to turn your program into an executable that the computer can run
(execute). The C program is the human-readable form, while the executablethat comes out of the
compiler is the machine-readable and executable form. What this means is that to write and run a C
program, you must have access to a C compiler. If you are using a UNIX machine (for example, if
you are writing CGI scripts in C on your host's UNIX computer, or if you are a student working on
a lab's UNIX machine), the C compiler is available for free. It is called either "cc" or "gcc" and is
available on the command line.
Modularity:
Ability to breakdown a large module into manageable sub modules called as modularity that is an
important feature of structured programming languages.
Advantages:
1. Projects can be completed in time.
2. Debugging will be easier and faster.
Portability:
The ability to port i.e. to install the software in different platform is called portability. Highest
degree of portability: ‘C’ language offers highest degree of portability i.e., percentage of changes to
be made to the sources code is at minimum when the software is to be loaded in another platform.
Extendability:
Ability to extend the existing software by adding new features is called as extendability.
SPEED:
‘C’ is also called as middle level language because programs written in ‘c’ language run at the
speeds matching to that of the same programs written in assembly language so ‘c’ language has
both the merits of high level and middle level language and because if this feature it is mainly used
in developing system software.
AREAS OF APPLICATIONS
The C programming language is used in many different areas of application, but the most
prolific area is UNIX operating system applications. The C language is also used in
computer games:
• UNIX operating system
• Computer games
DATA STRUCTURES
Software Requirements
Hardware Requirements
GROUP - B
Department of Computer Engineering has student's club named 'Pinnacle Club'.
Students of Second, third and final year of department can be granted membership
on request. Similarly one may cancel the membership of club. First node is reserved
for president of club and last node is reserved for secretary of club. Write C++
program to maintain club member‘s information using singly linked list. Store
5 student PRN and Name. Write functions to
a) Add and delete the members as well as president or even secretary.
b) Compute total number of members of club
c) Display members
d) Display list in reverse order using recursion
e) Two linked lists exists for two divisions. Concatenate two lists.
Write C++ program for storing binary number using doubly linked lists. Write
6 functions a) to compute 1‘s and 2‘s complement b) add two binary numbers
Write C++ program to store set of negative and positive numbers using linked
list. Write functions to
a) Insert numbers
b) Delete nodes with negative numbers
7 c) Create two more linked lists using this list, one containing all positive
numbers and
other containing negative numbers
d) For two lists that are sorted; Merge these two lists into third resultant list
that is sorted
GROUP - C
In any language program mostly syntax error occurs due to unbalancing
delimiter such as
8 (),{},[]. Write C++ program using stack to check whether given expression is
well
parenthesized or not.
9 Implement C++ program for expression conversion as infix to postfix and its
evaluation
using stack based on given conditions
i. Operands and operator, both must be single character.
ii. Input Postfix expression must be in a desired format.
iii. Only '+', '-', '*' and '/ ' operators are expected.
GROUP - D
Queues are frequently used in computer programming, and a typical example
is the creation of a job queue by an operating system. If the operating system
10 does not use priorities, then the jobs are processed in the order they enter the
system. Write C++ program for simulating job queue. Write functions to add
job and delete job from queue.
A double-ended queue(deque) is a linear list in which additions and deletions
may be made at either end. Obtain a data representation mapping a deque into
11 a one-dimensional array. Write C++ program to simulate deque with functions
to add and delete elements from either end of the deque.
GROUP - E
Write C++ program to store first year percentage of students in array. Write
12 function for sorting array of floating point numbers in ascending order using
a) Selection Sort b) Bubble sort and display top five scores.
Write C++ program to store first year percentage of students in array. Sort
array of floating point numbers in ascending order using quick sort and display
13 top five scores
.
Write C++ program to store second year percentage of students in array. Write
14 function for sorting array of floating point numbers in ascending order using
a) Insertion sort b) Shell Sort and display top five scores
Prerequisite:
• C++ Programming
Objectives:
To understand the use functions for N students record.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the number of elements
required by an array as follows −
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero
and type can be any valid C data type. For example, to declare a 10-element array called balance of
type double, use this statement −
double balance[10];
Here balance is a variable array which is sufficient to hold up to 10 double numbers.
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
Initializing Arrays
You can initialize an array in C either one by one or using a single statement as follows −
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
Functions Used :
Write algorithm/pseudo code for each function.
1. The average score of class
2. Highest score and lowest score of class
3. Marks scored by most of the students
4. list of students who were absent for the test
Algorithm:
1. Start
2. Accept number of students from user.
3. Accept roll number and marks of student.
4. Write function to calculate average marks
5. Write function to calculate highest marks
6. Write function to calculate lowest marks
7. Display roll number of absent student.
8. Stop.
Flowchart :
Here, draw flowchart of above algorithm.
Conclusion:
By this way, we can store the marks of N students sucessfully.
A P J Total
Dated Sign
3 4 3 10
Questions:
1. Basics of C++ Programming.
2. What are functions?
3. What are the features of C++ Programming?
4. What are array?
Program :
#include<iostream>
using namespace std;
int main()
{
int n,max=0,min,i,a[20],sum=0,j;
float avg;
cout<<"Enter the Number of Student : ";
cin>>n;
cout<<"Enter the marks of Student :";
cout<<"\nRollno"<<"\t"<<"Marks";
for(int i=0;i<n;i++)
{
cout<<"\n"<< i+1<<" :\t";
cin>>a[i];
sum=sum+a[i];
}
avg=sum/n;
cout<<"Average of total student is :";
cout<<avg<<"\n";
//MAx value
for(i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
j=i+1;
}
}
//min value
for(i=0;i<n;i++)
{
if(a[i]>=0)
{
min=a[i];
break;
}
}
for(i=0;i<n;i++)
{
if(a[i]>0)
{
if(a[i]<min)
{
min=a[i];
j=i+1;
}
}
}
cout<<"lowest mark is :"<< j<<"\t"<<min<<"\n";
//above 75 mark
cout<<"Who score more that 75 marks :";
for(i=0;i<n;i++)
{
if(a[i]>75)
{
cout<<"\n"<<i+1<<"\t"<<a[i]<<"\n";
}
}
//absent no of student
cout<<"\nRollno of Absent student are :";
int c=0;
for(i=0;i<n;i++)
{
if(a[i]==-1)
{
cout<<"\n"<<i+1;
c=c+1;
}
}
cout<<"\nTotal Absent student are : "<<c;
}
Practical No : 2(A)
Practical Title: Write C/C++ program to generate all possible passwords.
Aim : Set A=(1,3, a, s, t, i} represent alphanumeric characters permitted to set the password of
length 4. Write C/C++ program to generate all possible passwords
Pre-requisite:
• Knowledge of representing looping in C/C++
• Knowledge of permutation for possible password
Objectives:
• Generate 4 digit password
• Generate password for alphanumeric character .
Input:
Set of alphanumberic character
Outcome :
Generate 4 digit password
Theory :
Definition.
Permutations are the different ways in which a collection of items can be arranged.
For example: The different ways in which the alphabets A, B and C can be grouped together, taken all
at a time, are ABC, ACB, BCA, CBA, CAB, BAC.
Question:
I need all of the 4 digit combinations using the numbers 0-9?
you can repeat the digits the NUMBER of 4 digit combinations is relatively easy to calculate.
Suppose that you are going to write one of these 4 digit combinations. You have 10 choices for
the first digit. Once you have chosen the first digit you have 10 choices for the second digit.
Thus you have 10x10 = 100 choices for the first two digits. For each choice of the first two
digits you have 10 choices for the third digit. Thus you have 10x10x10 = 1000 choices for the
first three digits. Finally you have 10 choices for the fourth digit and thus there are
I am not going to list all 10 000 combinations but I will show you a way to do so in a "natural" order.
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
Algorithms :
1. Start
2. Take input as set of alphanumeric character.
3. Write function to generate password
- Define nos of for loop as per requirement.(nested loop)
- Repeat for loop till displaying all password
4. Display password
5. End
Flowchart :
Conclusion :
A P J Total
Dated Sign
3 4 3 10
Program :
#include<iostream>
using namespace std;
class password
{
char seta[6]={'1','2','a','s','t','i'};
int count=0;
public:
void combination();
};
void password::combination()
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
for(int k=0;k<6;k++)
{
for(int l=0;l<6;l++)
{
cout<<seta[i]<<seta[j]<<seta[k]<<seta[l];
cout<<"\t";
count++;
}
}
}
}
cout<<"\nTOTAL COMBINATION ARE:"<<count;
}
int main()
{
password p;
p.combination();
}
////OUTPUT :
1111 1112 111a 111s 111t 111i 1121 1122 112a 112s 112t 112i 11a1 11a2
11aa 11as 11at 11ai 11s1 11s2 11sa 11ss 11st 11si 11t1 11t2 11ta
11ts 11tt 11ti 11i1 11i2 11ia 11is 11it 11ii 1211 1212 121a 121s
121t 121i 1221 1222 122a 122s 122t 122i 12a1 12a2 12aa 12as 12at
12ai 12s1 12s2 12sa 12ss 12st 12si 12t1 12t2 12ta 12ts 12tt 12ti
12i1 12i2 12ia 12is 12it 12ii 1a11 1a12 1a1a 1a1s 1a1t 1a1i 1a21
1a22 1a2a 1a2s 1a2t 1a2i 1aa1 1aa2 1aaa 1aas 1aat 1aai 1as1 1as2
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
Pre-requisite:
• Knowledge of representing matrix in C/C++
• Knowledge of different operations that can be performed on matrix
Objectives:
• Check whether the the matrix is upper triangular or not
• Compute the transpose and summation of diagonal elements of matrix
• Perform addition , subtraction and multiplication of two matrices.
Input:
• Number of rows and columns of two matrices
• Elements of both the matrices
Outcome:
• Transpose of a matrix
• Whether the matrix is upper triangular
• Result of addition , subtraction and multiplication of both matrices.
Theory:
2-dimension array –
- Addition
- Substraction
- Multiplication
- Transpose of matrix
Algorithm:
1. Start
2. Input number of rows and columns of first matrix.
3. Input elements of first matrix.
4. Input number of rows and columns of second matrix.
5. Input elements of Second matrix.
6. Check the elements of first matrix, if all the entries below the main diagonal are zero then
output that it is upper triangular matrix.
7. Write function to compute summation of diagonal elements of both the matrices.
8. Function to transpose first matrix i.e. the element at row r column c in the original is placed at
row c column r of the transpose.
9. Fuction to add, subtract and multiply two matrices.
Flowchart :
Draw flowchart for above algorithm
Conclusion:
By this way, we can perform various operations on matrix successfully.
A P J Total
Dated Sign
3 4 3 10
Questions:
1. What is upper triangular matrix and lower triangular matrix?
2. How to find transpose of a matrix.
3. Different operations on matrix.
Program :
#include<iostream>
using namespace std;
int main()
{ char chr;
int a[10][10],b[10][10],c[10][10],row,col,i,j,ch,m=0,p=0,q=0;
do{
cout<<"\t"<<"*************************"<<endl<<endl;
cout<<"\t"<<"ENTER YOUR CHOICE NUMBER"<<endl<<endl<<endl;
cout<<"\t"<<"_______MENU_______"<<endl<<endl;
cout<<"\t"<<"1).MATRIX ADDITION"<<endl;
cout<<"\t"<<"2).MATRIX SUBSTRACTION"<<endl;
cout<<"\t"<<"3).MATRIX MULTIPLICATION"<<endl;
cout<<"\t"<<"4).MATRIX TRANSPOSE"<<endl;
cout<<"\t"<<"5).SUMMATION OF DIAGONAL ELEMENT"<<endl;
cout<<"\t"<<"6).CHECK UPPER TRIANGULAR MATRIX"<<endl;
cout<<"\t"<<"7).EXIT"<<endl;
cout<<"\t"<<"*************************"<<endl<<endl;
switch(ch)
{
case 1: //addition of matrix
// matrix a
cout<<"ENTER MATRIX 'A' "<<endl;
cout<<"ENTER NO OF ROWS OF MATRIX A"<<endl;
cin>>row;
cout<<"ENTER NO OF COLOUMNS OF MATRIX A"<<endl;
cin>>col;
cout<<"ENTER MATRIX ELEMENTS :"<<endl;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<"\t"<<"a["<<i<<"]["<<j<<"]\t";
cin>>a[i][j];
}
}
cout<<endl<<endl<<"YOUR MATRIX A IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"||"<<endl;
}
//matrix b
cout<<"ENTER MATRIX 'B' "<<endl;
cout<<"ENTER NO OF ROWS OF MATRIX B"<<endl;
cin>>row;
cout<<"ENTER NO OF COLOUMNS OF MATRIX B"<<endl;
cin>>col;
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
}
cout<<endl<<endl<<"YOUR MATRIX B IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<b[i][j]<<"\t";
}
cout<<"||"<<endl;
}
//ADDITION OF MATRIX A & B .
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<endl<<endl<<"YOUR MATRIX ADDITION IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<"||"<<endl;
}
break;
}
cout<<endl<<endl<<"YOUR MATRIX A IS :"<<endl;
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
}
cout<<endl<<endl<<"YOUR MATRIX B IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<b[i][j]<<"\t";
}
cout<<"||"<<endl;
}
//substraction OF MATRIX A & B .
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
cout<<endl<<endl<<"YOUR MATRIX SUBSTRACTION IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<"||"<<endl;
}
break;
}
cout<<endl<<endl<<"YOUR MATRIX A IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"||"<<endl;
}
//matrix b
cout<<"ENTER MATRIX 'B' "<<endl;
cout<<"ENTER NO OF ROWS OF MATRIX B"<<endl;
cin>>row;
cout<<"ENTER NO OF COLOUMNS OF MATRIX B"<<endl;
cin>>col;
cout<<"ENTER MATRIX ELEMENTS :"<<endl;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<"\t"<<"b["<<i<<"]["<<j<<"]\t";
cin>>b[i][j];
}
}
cout<<endl<<endl<<"YOUR MATRIX B IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<b[i][j]<<"\t";
}
cout<<"||"<<endl;
}
//MULTIPLICATION OF MATRIX A & B .
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
for(k=0;k<col;k++)
{
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
}
cout<<endl<<endl<<"YOUR MATRIX A IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"||"<<endl;
}
//transpose of matrix
cout<<"TRANSPOSE OF MATRIX IS :"<<endl<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<"||"<<endl;
}
break;
}
cout<<endl<<endl<<"YOUR MATRIX A IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"||"<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
m=a[i][j]+m;
}
}
}
cout<<endl<<endl<<"YOUR MATRIX A IS :"<<endl;
for(i=0;i<row;i++)
{ cout<<"||"<<"\t";
for(j=0;j<col;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"||"<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
c[p][q]=a[i][j];
p++;
q++;
}
}
}
for(p=0;p<row-1;p++)
{
for(q=0;q<col-1;q++)
{
if(c[p][q]==0)
{
c[p][q]=a[i][j];
p++;
q++;
}
}
default : break;
Practical No : 04(A)
Practical Title: Write a C++ program to perform String operations.
Aim: Write C++ program for string operations- copy, concatenate, check substring, equal, reverse
and length.
Pre-requisite:
• Basics of String operations.
Objectives:
To understand the use standard library functions for stirng opeations.
To perform the string operations.
Input: One or Two Strings
Theory :
Algorithms :
1. Start
2. Accept two string from user i.e. str1 and str2
3. Write function to copy str2 to str1
4. Perform concatenation of two string
5. Write function to find length of string
6. Function to check given string are equal or not
7. Function to find subtring
8. Write function to reverse string
9. Display all string operation
10. End
Flowchart :
Draw flowchart for above algorithm
Conclusion:
By this way, we can perform string operations successfully.
A P J Total
Dated Sign
3 4 3 10
Program :-
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int i, j; // INDEXING VARIABLES.
int choice; // VARIABLE USED TO SELECT CASE FROM SWITCH CASE.
int len1=0, len2=0; //VARIABLES TO STORE LENGTHS OF STRING.
char str_1[100], str_2[100], cpy[100], concn[200], revers[100]; //VARIABLS TO STORE THE STRINGS .
char prob; // VARIABLE USED TO CONTINUE SWITCH CASE.
int count1 = 0, count2 = 0, flag; // FOR INITIALIZING THE VALUES OF 'IF' OR'IF ELSE' STATEMENTS.
do{
do{
// DRIVEN MENU.
cout<<"ENTER THE ACTION NO YOU WANT TO PERFORM:"<<endl<<endl<<endl;
cout<<"*********************"<<endl<<endl;
cout<<"***MENU***"<<endl;
cout<<"1).STRING LENGTH"<<endl;
cout<<"2).STRING COPY"<<endl;
cout<<"3).STRING COMPARE"<<endl;
cout<<"4).STRING CONCANATE"<<endl;
cout<<"5).STRING REVERSE"<<endl;
cout<<"6).SUB-STRING "<<endl;
cout<<"7).EXIT"<<endl<<endl;
cout<<"*********************"<<endl<<endl;
// SELECTION CASE.
cin>>choice;
if(choice<1||choice>7)
{
cout<<"ENTER A VALID INPUT\n\n";
}
}while(choice<1||choice>7);
switch(choice)
{
case 1: //STRING LENGTH.
// PRINT OUTPUT .
cout<<"the length of string is ::"<<len1<<endl;
break;
// PRINT OUTPUT.
cout<<"the copied second string ::"<<str_2<<endl<<endl;
break;
// PRINT OUTPUT.
if(flag==0)
cout<<"the strings are identical"<<endl<<endl;
else
cout<<"the strings are not identical"<<endl<<endl;
break;
// PRINT OUTPUT.
cout<<str_1<<endl;
break;
// PRINT OUTPUT.
cout<<endl<<endl<<"the reverse string is:\t";
for(i=0;i<len1;i++)
{
cout<<str_2[i];
}
cout<<endl<<endl;
break;
// PRINT OUTPUT.
if (flag == 1)
default : break;
}
// TO CONTINUE THE PROGRAM
cout<<"do you want to continue (yes/ no).....";
cin>>prob;
}while(prob=='y'||prob=='Y');
/* output
*********************
***MENU***
1).STRING LENGTH
2).STRING COPY
3).STRING COMPARE
4).STRING CONCANATE
5).STRING REVERSE
6).SUB-STRING
7).EXIT
*********************
1
enter the string :
pvgcoe
the length of string is ::6
do you want to continue (yes/ no).....y
ENTER THE ACTION NO YOU WANT TO PERFORM:
*********************
***MENU***
1).STRING LENGTH
2).STRING COPY
3).STRING COMPARE
4).STRING CONCANATE
5).STRING REVERSE
6).SUB-STRING
7).EXIT
*********************
2
enter the first string
copiying in process............
*********************
***MENU***
1).STRING LENGTH
2).STRING COPY
3).STRING COMPARE
4).STRING CONCANATE
5).STRING REVERSE
6).SUB-STRING
7).EXIT
*********************
3
enter the first string :
pvgcoe
enter the second string :
pvgcoe
the strings are identical
*********************
***MENU***
1).STRING LENGTH
2).STRING COPY
3).STRING COMPARE
4).STRING CONCANATE
5).STRING REVERSE
6).SUB-STRING
7).EXIT
*********************
3
enter the first string :
pvgcoe
enter the second string :
pvgcoen
the strings are not identical
*********************
*********************
4
enter the first string :
pvgcoe
enter the second string :
_nashik
the concaniated string is :
pvgcoe_nashik
do you want to continue (yes/ no).....y
ENTER THE ACTION NO YOU WANT TO PERFORM:
*********************
***MENU***
1).STRING LENGTH
2).STRING COPY
3).STRING COMPARE
4).STRING CONCANATE
5).STRING REVERSE
6).SUB-STRING
7).EXIT
*********************
5
enter the string
pvgcoe
*********************
***MENU***
1).STRING LENGTH
2).STRING COPY
3).STRING COMPARE
4).STRING CONCANATE
5).STRING REVERSE
6).SUB-STRING
7).EXIT
*********************
3
enter the first string :
*********************
***MENU***
1).STRING LENGTH
2).STRING COPY
3).STRING COMPARE
4).STRING CONCANATE
5).STRING REVERSE
6).SUB-STRING
7).EXIT
*********************
6
enter the main string :
PVGCOE
enter a string :
PVG
IT IS A SUB STRING OF MAIN STRING
*********************
***MENU***
1).STRING LENGTH
2).STRING COPY
3).STRING COMPARE
4).STRING CONCANATE
5).STRING REVERSE
6).SUB-STRING
7).EXIT
*********************
6
enter the main string :
PVGCOE
enter a string :
GVC
IT IS NOT A SUB STRING OF MAIN STRING
GROUP - B
GROUP - B
Downloaded by Ravindra Mule ([email protected])
lOMoARcPSD|23769222
Practical No : 05(B)
Assignmet Title: Write C++ program to maintain club member‘s information using singly linked
list.
Aim: Department of Computer Engineering has student's 1/8 club named 'Pinnacle Club'. Students
of Second, third and final year of department can be granted membership on request. Similarly one
may cancel the membership of club. First node is reserved for president of club and last node is
reserved for secretary of club. Write C++ program to maintain club member‘s information using
singly linked list. Store student PRN and Name. Write functions to
a) Add and delete the members as well as president or even secretary.
b)Compute total number of members of club
c) Display members
d) Display list in reverse order using recursion
e) Two linked lists exists for two divisions. Concatenate two lists
Theory:
Linked List : (write linked list definition and sinly linked list theory)
- Definition :
- Types of linked list
- Singly linked list (definition, concepts, advantages, disadvantages)
- Singly linked list as an ADT (Write pseudo code for each ADT)
- Algorithm
- Flowchart :
(draw your own flowchart for your algorithms)
Conclusion:
By this way, we can maintain club member‘s information using singly linked list.
A P J Total
Dated Sign
3 4 3 10
Questions:
Program :
/*Code for Program to maintain club member‘s information using singly linked list.*/
#include<iostream>
using namespace std;
typedef struct stud
{
char PRN[20];
char name[20];
}stud;
node *createnode()
{
int i,n,cnt=0;
node *p,*head,*t,*st;
head=NULL;
cout<<"\n Enter the no of nodes:";
cin>>n;
for(i=0;i<n-1;i++)
{
p=new node;
if (head==NULL)
{
cout<<"\n Enter the data for President";
cout<<"\n\n Enter PRN number:";
cin>>p->s.PRN;
cout<<"\n Enter the name:";
cin>>p->s.name;
p->next=NULL;
head=p;
st=new node;
cout<<"\n Enter the data for Secretary";
cout<<"\n\n Enter PRN number:";
cin>>st->s.PRN;
cout<<"\n Enter the name:";
cin>>st->s.name;
t=head;
while(t->next!=NULL)
t=t->next;
t->next=st;
return head;
}
}
for(q=head;q->next!=NULL;q=q->next);
q->next=p;
return head;
}
//node *insertl(node *head,int loc)
//{
{
node *p,*q;
int i;
if (head==NULL)
{
cout<<"list is empty";
return head;
}
if(loc==1)
{
p=head;
head=head->next;
p->next=NULL;
delete p;
return head;
}
q=head;
for(i=1;i<loc-1;i++)
if(q->next!=NULL)
q=q->next;
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
return p;
}
void concat(node *head1,node *head2)
{
node *p,*q;
p=head1;
q=head2;
while(p->next!=NULL)
p=p->next;
p->next=head2;
print(head1);
}
int main()
{
node *head, *head1, *head2;
head= NULL;
int ch,ele,loc;
head=NULL;
head1=head2=NULL;
while(ch!=11)
{
Pre-requisite:
Objective:
Input:
Two Binary numbers
Outcome:
• 1's and 2's complement of a binary number.
• Result of addition of two binary numbers.
Theory :
Doubly linked list : (write DLL theory in details i.e. definition, concepts, advantages,
disadvantages.)
- Doubly linked list as an ADT : (write pseudo code for each operation)
- Algorithms :
- Flowchart :
Conclusion:
By this way, we can store binary number using 8/8 doubly linked lists.
A P J Total
Dated Sign
3 4 3 10
Questions:
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
Program :
#include<iostream.h>
#include<stdlib.h>
//using namespace std;
struct node *create();
void display(struct node *);
struct node *reverse(struct node *);
struct node *comp1(struct node *);
struct node *comp2(struct node *);
struct node * add(struct node *, struct node *);
struct node
{
int data;
struct node *next;
struct node *prev;
}*B1=NULL,*B2=NULL,*C1=NULL,*C2=NULL,*A=NULL;
int main()
{
int ch;
cout<<"Enter First Binary no.:\n";
B1=create();
cout<<"\n First Binary no. \n";
display(B1);
cout<<"\nEnter the Second Binary no.:\n";
B2=create();
cout<<"\n Second Binary no. \n";
display(B2);
while(1)
{
cout<<"\n\nMenu:";
cout<<"\n1.1's Complement and 2's complement of First Binary number.";
cout<<"\n2.1's Complement and 2's complement of Second Binary
number."; cout<<"\n3.Addition of two binary nos."; cout<<"\n4.Exit.";
cout<<"\n\nEnter choice:";
cin>>ch;
switch(ch)
{
case 1:
break; case 3:
break;
case 2:
A=add(B1,B2);
display(A);
break;
return 0;
{
char ch;
struct node *new_node,*current,*start=NULL;
do
{
new_node=(struct node*)malloc(sizeof(struct node));
label:
cout<<"\nEnter Binary digit: ";
cin>>new_node->data;
new_node->next=NULL;
new_node->prev=NULL;
if(new_node->data!=0 && new_node->data!=1)
{
cout<<"\nEnter Valid binary digit.\n\n";
goto label;
}
else
{
if(start==NULL)
start=new_node;
current=new_node;
else
current->next=new_node;
new_node->prev=current;
current=new_node;
}
}
cin>>ch;
}while(ch!='n');
return start;
}
while(new_node!=NULL)
cout<<new_node->data<<"\t";
new_node=new_node->next;
cout<<"\n";
}
struct node *comp1(struct node *new_node)
{
struct node *n_node,*current,*start=NULL;
while(new_node!=NULL)
{
n_node=(struct node*)malloc(sizeof(struct
node)); if(new_node->data==1)
{
n_node->data=0;
}
else
{
n_node->data=1;
}
start=n_node;
current=n_node;
else
current->next=n_node;
n_node->prev=current;
current=n_node;
}
new_node=new_node->next;
}
return start;
}
struct node *comp2(struct node *temp)
{
struct node *n1,*c,*start=NULL;
c=comp1(temp);
n1=(struct node*)malloc(sizeof(struct node));
n1->data=1;
n1->next=NULL;
n1->prev=NULL;
start=add(c,n1);
return start;
}
struct node * add(struct node *start, struct node *start1)
{
struct node *n, *c,*r=NULL;
int rem=0;
while(start->next!=NULL)
start=start->next;
while(start1->next!=NULL)
start1=start1->next;
while(start!=NULL && start1!=NULL)
{
n=(struct node*)malloc(sizeof(struct node));
n->data=(start->data+start1->data+rem)%2;
rem=(start->data+start1->data+rem)/2;
n->next=NULL;
n->prev=NULL;
if(r==NULL)
r=c=n;
else
{
c->prev=n;
n->next=c;
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
Practical No:07(B)
Practical Title: Write C++ program to store set of negative and positive numbers using linked list
Aim: Write C++ program to store set of negative and positive numbers using linked list. Write
functions
a) Insert numbers
b) Delete nodes with negative numbers
c) To create two more linked lists using this list, one containing all positive numbers and other
containing negative numbers
d) For two lists that are sorted; Merge these two lists into third resultant list that is sort
Pre-requisite:
Objective:
• To store set of positive and negative numbers using linked list.
Input:
Number of Elements
Positive and negative numbers
Outcome:
• List of positive numbers
• List of negative numbers
• Result of Sort and merge operations on linked list
•
Theory : write short theory of linked list.
Explain logic/algorithms for positive and negative number
Explain logic/algorithms for sorting and merging linked list.
Algorithms:
(Write your algorithms for your program.)
Flowchart :
(Draw flowchart for above algorithms.)
Conclusion:
By this way, we can store set of negative and positive numbers using linked list
A P J Total
Dated Sign
3 4 3 10
Question Bank:
GROUP - C
GROUP - C
Downloaded by Ravindra Mule ([email protected])
lOMoARcPSD|23769222
Practical No:08(C)
Practical Title: Write C++ program to check well formedness of parenthesis using stack.
Aim: In any language program mostly syntax error occurs due to unbalancing delimiter such as (),
{},[]. Write C++ program using stack to check whether given expression is well parenthesized or
not.
Pre-requisite:
• Basics of stack.
• Different operations that can be performed on stack
Objective:
• To check whether the given expression is well parenthesized or not.
Input:
Expresstion using {},(),[].
Outcome:
• Result of checking well formedness of parenthesis.
Theory :
- Write short theory of stack.
- Write concept of well form parenthesis.
- Example of well form parenthesis.
Algorithms :
Write your own algorithms
Flowchart :
Draw flowchart for above algorithms.
Conclusion:
By this way, we can check well formedness of parenthesis using stack.
A P J Total
Dated Sign
3 4 3 10
Question Bank:
1. What is Stack?
2. Which are the different operations that can be performed on stack?
3. Explain PUSH, POP operations on stack
4. What are the applications of stack?
#include <iostream>
using namespace std;
#define size 10
class stackexp
{
int top;
char stk[size];
public:
stackexp()
{
top=-1;
}
void push(char);
char pop();
int isfull();
int isempty();
};
void stackexp::push(char x)
{
top=top+1;
stk[top]=x;
}
char stackexp::pop()
{
char s;
s=stk[top];
top=top-1;
return s;
}
int stackexp::isfull()
{
if(top==size)
return 1;
else
return 0;
}
int stackexp::isempty()
{
if(top==-1)
return 1;
else
return 0;
}
int main()
{
stackexp s1;
char exp[20],ch;
int i=0;
Invalid Expression.....
Practical No:09(C)
Practical Title: Write a C++ program for expression conversion as infix to postfix and its
evaluation using stack
Aim: Implement C++ program for expression conversion as infix to postfix and its evaluation
using stack based on given conditions
i. Operands and operator, both must be single character.
ii. Input Postfix expression must be in a desired format.
iii. Only '+', '-', '*' and '/ ' operators are expected
Pre-requisite:
• Basics of stack.
• Different operations that can be performed on stack
Objective:
• To convert the expression from infix to postfix
• Evaluate the expression
Input:
Infix expression
Outcome:
• Equivalent postfix expression
• Result of evaluation of an expression.
Theory :
- Write short theory for stack.
- Explain infix to postfix expression
- Example infix to postfix conversion
Algorithms :
Write your own algorithms
Flowchart :
Draw flowchart for above algorithms
Conclusion:
By this way, we can perform expression conversion as infix to postfix and its evaluation using stack
A P J Total
Dated Sign
3 4 3 10
Question Bank:
1. What is Stack?
2. Which are the different operations that can be performed on stack?
3. Explain PUSH, POP operations on stack
4. What are the applications of stack?
5. What is infix, postfix and prefix expression?
6. Conversion – infix to postfix, infix to prefix , etc.
7. Evaluation of infix, postfix and prefix expression
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
//using namespace std;
void infix_postfix(char infix[20]);
int evaluate(char x,int op1,int op2);
void eval(char postfix[]);
int pre(char x);
struct stack
{
int a[20];
int top;
};
typedef struct stack stack;
void init(stack *s)
{
s->top=-1;
}
int isfull(stack *s)
{
if(s->top==20)
return 1;
else
return 0;
}
int isempty(stack *s)
{
if(s->top==-1)
return 1;
else
}
return 0;
}
int pre(char x)
{
postfix[j]='\0'; cout<<postfix; eval(postfix);
if(x=='(') return 0;
else if(x=='+' || x=='-')
return 1;
else if(x=='/' || x=='*' || x=='%')
return 2;
else
return 3;
}
void eval(char postfix[])
{
stack s;
char x;
int op1,op2,val,i;
init(&s);
for(i=0;postfix[i]!='\0';i++)
{
x=postfix[i];
if(isalpha(x))
{
cout<<"\nEnter the value of:"<<x;
cin>>val;
push(&s,val);
}
else
{
op2=pop(&s);
op1=pop(&s);
val=evaluate(x,op1,op2);
push(&s,val);
}
}
val=pop(&s);
cout<<"\nValue of expression:"<<val;
}
GROUP - D
GROUP - D
Downloaded by Ravindra Mule ([email protected])
lOMoARcPSD|23769222
Practical No:10(D)
Practical Title: Perform different operations on Queue.
Aim: Queues are frequently used in computer programming, and a typical example is the creation
of a job queue by an operating system. If the operating system does not use priorities, then the jobs
are processed in the order they enter the system. Write C++ program for simulating job queue.Write
functions to add job and delete job from queue.
Pre-requisite:
• Basics of Queue
• Different operations that can be performed on queue
Objective:
• To perform addition and deletion operations on queue.
Input:
Size of queue
Elements in quque
Outcome:
• Result of addition of job operation on queue.
• Result of deletion of job operation on queue.
Theory :
- Write theory of queue (definition, concepts, types, advantages, disadvantages)
- Explain queue as an ADT. (write pseudo code)
Algorithms :
Write your own algorithms
Flowchart :
Draw flowchart for above algorithms
Conclusion:
By this way, we can perform different operations on queue
A P J Total
Dated Sign
3 4 3 10
Question Bank:
1. What is Queue?
2. What are the different operations that can be performed on queue?
3. Explain all the operations on queue
4. Which are different types of queues , Explain.
Program :
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
#include<iostream.h>
#include<conio.h>
void init();
void insert();
int full();
int isempty();
void display();
int a[5],f,r;
void init()
{
f=-1;
r=-1;
}
void insert()
{
int x;
if(f==-1)
{
f=0;
}
if(!full())
{
cout<<"Enter data:";
cin>>x;
r=r+1;
a[r]=x;
display();
}
else
{
cout<<"Queue is full\n";
}
}
int del()
{
int x;
if(!isempty())
{
x=a[f];
f=f+1;
display();
}
else
{
cout<<"Queue is empty\n";
}
Practical No:11(D)
Practical Title: Perform operations on Double ended queue.
Aim: A double-ended queue(deque) is a linear list in which additions and deletions may be made at
either end. Obtain a data representation mapping a deque into a one-dimensional array. Write C++
program to simulate deque with functions to add and delete elements from either end of the deque.
Pre-requisite:
• Knowledge of Queue
• Types of queue
• Knowledge of double ended queue and different operations that can be performed on it
Objective:
• To simulate deque with functions to add and delete elements from either end of the deque.
Input:
Size of array
Elements in the queue
Outcome:
• Result of deque with functions to add and delete elements from either end of the deque.
Theory :
Double-Ended Queue
A double-ended queue is an abstract data type similar to an simple queue, it allows you to insert and
delete from both sides means items can be added or deleted from the front or rear end.
q[rear]=no;
[Set rear and front pointer]
if rear=0
rear=1;
if front=0
front=1;
Step-3: return
void add_item_rear()
{
int num;
printf("\n Enter Item to insert : ");
scanf("%d",&num);
if(rear==MAX)
{
printf("\n Queue is Overflow");
return;
}
else
{
rear++;
q[rear]=num;
if(rear==0)
rear=1;
if(front==0)
front=1;
}
}
void add_item_front()
{
int num;
printf("\n Enter item to insert:");
scanf("%d",&num);
if(front<=1)
{
printf("\n Cannot add item at front end");
return;
}
else
{
front--;
q[front]=num;
}
}
void delete_item_front()
{
int num;
if(front==0)
{
printf("\n Queue is Underflow\n");
return;
}
else
{
num=q[front];
printf("\n Deleted item is %d\n",num);
if(front==rear)
{
front=0;
rear=0;
}
else
{
front++;
}
}
void delete_item_rear()
{
int num;
if(rear==0)
{
printf("\n Cannot delete item at rear end\n");
return;
}
else
{
num=q[rear];
if(front==rear)
{
front=0;
rear=0;
}
else
{
rear--;
printf("\n Deleted item is %d\n",num);
}
}
}
Algorithms :
Write your own algorithms
Flowchart :
Draw flowchart for above algorithms
Conclusion:
By this way, we can perform operations on double ended queue
A P J Total
Dated Sign
3 4 3 10
Question Bank:
1. What is queue?
2. Types of queue
3. What is double ended queue?
4. How to insert the new node in Doubly Link List?
5. How to delete the node from front of Doubly Link List ?
6. How to delete the node from end of Doubly Link List ?
7. How to delete the node in between of Doubly Link List
Program :
#include<iostream.h>
#include<conio.h>
void init();
void insert_r();
void insert_f();
int del_r();
int del_f();
int full_r();
int full_f();
int isempty();
void display();
int a[5],f,r,size=5;
void init()
{
f=-1;
r=-1;
}
void insert_r()
{
int x;
if(!full_r())
{
cout<<"Enter data:";
cin>>x;
r=r+1;
a[r]=x;
if(f==-1)
{
f=0;
}
display();
}
else
{
cout<<"Queue is full\n";
}
}
void insert_f()
{
int x;
if(!full_f())
{
cout<<"Enter data:";
cin>>x;
if(f==-1)
{
f=r=0;
}
else
{
f=f-1;
}
a[f]=x;
display();
}
else
{
cout<<"Queue is full\n";
}
}
int del_f()
{
int x;
if(!isempty())
{
x=a[f];
cout<<"\n Element deleted = "<<x;
if(f==r)
{
f=-1;
r=-1;
}
else
{
f=f+1;
display();
}
}
else
{
cout<<"Queue is empty\n";
}
return x;
}
int del_r()
{
int x;
if(!isempty())
{
x=a[r];
cout<<"\n Element deleted = "<<x;
if(r==f)
{
f=-1;
r=-1;
}
else
{
r=r-1;
display();
}
}
else
{
cout<<"Queue is empty\n";
}
return x;
}
int full_f()
{
if(f==0)
return 1;
return 0;
}
int full_r()
{
if(r==size-1)
return 1;
return 0;
}
int isempty()
{
if(f==-1)
return 1;
return 0;
}
void display()
{
int i;
cout<<"\nQueue=";
for(i=f;i!=r;i=i+1)
{
cout<<"\t"<<a[i];
}
cout<<"\t"<<a[i];
}
int main()
{
clrscr();
int ch,x;
init();
while(1)
{
cout<<"\n\nMenu\n1.Insert at Front\n2.Insert at Rear"
"\n3.Delete at Front\n4.Delete at rear"
"\n5.Exit\nEnter choice:";
cin>>ch;
switch(ch)
{
case 1:
insert_f();
break;
case 2:
insert_r();
break;
case 3:
del_f();
break;
case 4:
del_r();
break;
case 5:
return 0;
}
}
}
GROUP - E
GROUP - E
Downloaded by Ravindra Mule ([email protected])
lOMoARcPSD|23769222
Practical No:12(E)
Practical Title: Sorting of an array using selection and bubble sort.
Aim: Write C++ program to store first year percentage of students in array. Write function for
sorting array of floating point numbers in ascending order using a) Selection Sort b) Bubble sort
and display top five scores.
Pre-requisite:
Objective:
• To sort array of floating point numbers in ascending order using a) Selection Sort b) Bubble
sort and display top five scores.
Input:
Size of array Elements of array
Theory :
- Write short theory of sorting with its advantages and disadvantages.
- Explain selection and bubble sort with example
Algorithm:
def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
alist = [54,26,93,17,77,31,44,55,20]
bubbleSort(alist)
print(alist)
def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1):
positionOfMax=0
for location in range(1,fillslot+1):
if alist[location]>alist[positionOfMax]:
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
Flowchart :
Conclusion:
By this way, we can perform sorting of an array using selection and bubble sort.
A P J Total
Dated Sign
3 4 3 10
Question Bank:
Program :
Program:
#include<stdio.h>
void selection(float[],int);
void bubble(float[],int);
int main()
{
int n,i,op;
float a[30];
do
{
printf("\n 1)Bubble sort \n 2)Selection sort \n
3)Quit"); printf("\n Enter your choice :"); scanf("%d",
&op);
if(op==1)
{
printf("\n Enter no. of elements :");
scanf("%d",&n);
printf("\n Enter array elements :");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
bubble(a,n);
printf("\n Sorted array is :");
for(i=0;i<n;i++)
printf("%7.2f",a[i]);
}
if(op==2)
{
printf("\n Enter no. of elements :");
scanf("%d",&n);
printf("\n Enter array elements :");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
selection(a,n);
printf("\n Sorted array is :");
for(i=0;i<n;i++)
printf("%7.2f",a[i]);
}
}while(op!=3);
}
}
}
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(a[j]<a[k])
k=j;
if(k !=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
Practical No:13(E)
Practical Title: Sorting array of floating point numbers in ascending order using quick sort.
Aim: Write C++ program to store first year percentage of students in array. Write function for
sorting array of floating point numbers in ascending order using quick sort and display top five
scores.
Pre-requisite:
Objective:
• To sort array using quick sort
Input:
Size of array
First year percentage of students.
Outcome:
• To sort array using quick sort
• To display top five scores.
Theory :
- Explain concept of Quick sort in details.
- Advantages and disadvantages
- Example of quick sort.
- Time complexity.
Algorithm:
def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)
def quickSortHelper(alist,first,last):
if first<last:
splitpoint = partition(alist,first,last)
quickSortHelper(alist,first,splitpoint-1)
quickSortHelper(alist,splitpoint+1,last)
def partition(alist,first,last):
pivotvalue = alist[first]
leftmark = first+1
rightmark = last
done = False
while not done:
while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
leftmark = leftmark + 1
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
Flowchart :
Conclusion:
By this way, we can perform sorting array of floating point numbers in ascending order using quick
sort.
A P J Total
Dated Sign
3 4 3 10
Question Bank:
1. Explain the sorting?
2. What are the different types of sorts in data structures?
3. Define the quick sort?
5.How many passes are required in quick sort?
6.What is the time complexity of quick sort?
Program :
#include<iostream.h>
#include<conio.h>
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
{ int q;
if (p<r)
{ q= partition(arr,p,r);
quicksort(arr,p,q-1);
quicksort(arr,q+1,r);
{ int t, k,y,i,x;
x=arr[r];
i=p-1;
for(j=p;j<r;j++)
{ if(arr[j]<=x)
{ i=i+1;
t=arr[i];
arr[i]=arr[j];
arr[j]= t;
k=arr[i+1];
arr[i+1]=arr[r];
arr[r]=k; y
= i+1;
return y ; }
void main()
int arr[8],i;
for(i=0;i<8 ;i++)
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
for(i=0;i<8;i++)
{ cout<<arr[i]; }
quicksort(arr,1,8);
cout<< arr[i];
getch();
Aim: Write C++ program to store second year percentage of students in array. Write function for
sorting array of floating point numbers in ascending order using
a) Insertion sort
b) Shell Sort and display top five scores.
Pre-requisite:
Objective:
• To sort array of floating point numbers in ascending order using a) Insertion Sort b) Shell
sort and display top five scores.
Input:
Size of array Elements of array
Theory:
- Write short theory of sorting.
- Explain insertion and shell sort with example
Algorithm:
def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1):
positionOfMax=0
for location in range(1,fillslot+1):
if alist[location]>alist[positionOfMax]:
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
def shellSort(alist):
sublistcount = len(alist)//2
sublistcount = sublistcount // 2
def gapInsertionSort(alist,start,gap):
for i in range(start+gap,len(alist),gap):
currentvalue = alist[i]
position = i
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
shellSort(alist)
print(alist)
Flowchart :
Conclusion:
By this way, we can sort percentage of students in array using insertion sort and shell sort.
A P J Total
Dated Sign
3 4 3 10
Question Bank:
1. Explain the sorting?
2. What are the different types of sorts in data structures
3.Define the insertion sort?
4. Define the shell sort?
5. How many passes are required in insertion and shell sort?
6.What is the time complexity of insertion and shell sort?
Program :
#include<iostream>
using namespace std;
void in(float maks[20],in n))
{
in i,j;
float temp;
for(i=1;i<n;i++)
{
temp=maks[i];
for(j=i-1;j>=0&& maks[j]>temp;j--)
marks[j+1]=marks[j];
marks[j+1]=temp;
}
cout<<"\n Tiop five scores are:"
for(i=n-1;i>=n;i--)
cout<<"\t"<<marks[i];
}
int main()
{
float marks[20];
int i,n,ch;
cout<<"\n Enter the total no.of students:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n Ener the percentage marks o second year student"<<i+1<<"::"; cin>>marks[i];
while(ch!=3)
{
Pune vidyarthi griha’s College of Engineering,
Downloaded nashik.
by Ravindra Mule ([email protected]) Prof. Anand N. Gharu
lOMoARcPSD|23769222
THANKS..!
PROF. ANAND NANDLAL GHARU
ASSISTANT PROFESSOR
PVGCOE, NASHIK
Blog : anandgharu.wordpress.com