0% found this document useful (0 votes)
54 views5 pages

322 Practical

The document contains code for quicksort algorithm in C++ and largest element finding program. It also contains questions and answers on virtual functions, classes and objects in C++. The quicksort code uses partition and swap functions to sort an array. The largest element program takes user input of numbers in an array and finds the largest using a for loop. Virtual functions allow dynamic binding at runtime. Classes are user-defined data types that have data members and member functions, while objects are instances of classes allocated with memory.

Uploaded by

pankaj sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views5 pages

322 Practical

The document contains code for quicksort algorithm in C++ and largest element finding program. It also contains questions and answers on virtual functions, classes and objects in C++. The quicksort code uses partition and swap functions to sort an array. The largest element program takes user input of numbers in an array and finds the largest using a for loop. Virtual functions allow dynamic binding at runtime. Classes are user-defined data types that have data members and member functions, while objects are instances of classes allocated with memory.

Uploaded by

pankaj sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ques 1.Write a program in C++ for the quick sort.

Ans.
#include<iostream>
#include<cstdlib>

using namespace std;

void swap(int *a, int *b) {


int temp;
temp = *a;
*a = *b;
*b = temp;
}

int Partition(int a[], int l, int h) {


int pivot, index, i;
index = l;
pivot = h;
for(i = l; i < h; i++) {
if(a[i] < a[pivot]) {
swap(&a[i], &a[index]);
index++;
}
}
swap(&a[pivot], &a[index]);
return index;
}
int RandomPivotPartition(int a[], int l, int h) {
int pvt, n, temp;
n = rand();
pvt = l + n%(h-l+1);
swap(&a[h], &a[pvt]);
return Partition(a, l, h);
}
int QuickSort(int a[], int l, int h) {
int pindex;
if(l < h) {
pindex = RandomPivotPartition(a, l, h);
QuickSort(a, l, pindex-1);
QuickSort(a, pindex+1, h);
}
return 0;
}
int main() {
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++) {
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
QuickSort(arr, 0, n-1);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
Ques 4.Write a C++ program to read a set of numbers in an array & to find the
largest of them.
Ans.
#include <iostream>
using namespace std;

int main()
{
int i, n;
float arr[100];

cout << "Enter total number of elements(1 to 100): ";


cin >> n;
cout << endl;

// Store number entered by the user


for(i = 0; i < n; ++i)
{
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}

// Loop to store largest number to arr[0]


for(i = 1;i < n; ++i)
{
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i])
arr[0] = arr[i];
}
cout << "Largest element = " << arr[0];

return 0;
}

Viva Questions:
Ques.What are the differences between references and pointers??
Ans.
On the surface, both references and pointers are very similar, both are used to
have one variable provide access to another. With both providing lots of the same
capabilities.

Pointers: A pointer is a variable that holds memory address of another variable. A


pointer needs to be dereferenced with * operator to access the memory location it
points to.

References : A reference variable is an alias, that is, another name for an already
existing variable. A reference, like a pointer, is also implemented by storing the
address of an object.
A reference can be thought of as a constant pointer (not to be confused with a
pointer to a constant value!) with automatic indirection, i.e the compiler will
apply the * operator for you.

int i = 3;

// A pointer to variable i (or stores


// address of i)
int *ptr = &i;
// A reference (or alias) for i.
int &ref = i;

Differences :

Initialization: A pointer can be initialized in this way:


int a = 10;
int *p = &a;
OR
int *p;
p = &a;
we can declare and initialize pointer at same step or in multiple line

While in references,

int a=10;
int &p=a; //it is correct
but
int &p;
p=a; // it is incorrect as we should declare and initialize references at
single step.

2.Reassignment: A pointer can be re-assigned. This property is useful for


implementation of data structures like linked list, tree, etc. See the following
examples:
int a = 5;
int b = 6;
int *p;
p = &a;
p = &b;

On the other hand, a reference cannot be re-assigned, and must be assigned at


initialization.

int a = 5;
int b = 6;
int &p = a;
int &p = b; //At this line it will show error as "multiple declaration is not
allowed".

However it is valid statement,


int &q=p;

Memory Address: A pointer has its own memory address and size on the stack whereas
a reference shares the same memory address (with the original variable) but also
takes up some space on the stack.
NOTE However if we want the true address of reference, then it can be found out in
turbo IDE by writing the following statement,
int &p = a;
cout << &(&p);

NULL value: Pointer can be assigned NULL directly, whereas reference cannot. The
constraints associated with references (no NULL, no reassignment) ensure that the
underlying operations do not run into exception situation.
Indirection: You can have pointers to pointers offering extra levels of
indirection. Whereas references only offer one level of indirection.I.e,
In Pointers,
int a = 10;
int *p;
int **q; //it is valid.
p = &a;
q = &p;

Whereas in references,

int &p = a;
int &&q = p; //it is reference to reference, so it is an error.

6.Arithmetic operations: Various arithmetic operations can be performed on pointers


whereas there is no such thing called Reference Arithmetic.(but you can take the
address of an object pointed by a reference and do pointer arithmetics on it as in
&obj + 5).)

Question 2.What are virtual functions � Write an example?


Ans.
A virtual function is a member function which is declared within a base class and
is re-defined(Overriden) by a derived class. When you refer to a derived class
object using a pointer or a reference to the base class, you can call a virtual
function for that object and execute the derived class�s version of the function.

Virtual functions ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for function call.
They are mainly used to achieve Runtime polymorphism
Functions are declared with a virtual keyword in base class.
The resolving of function call is done at Run-time.

Example -
#include <iostream>
using namespace std;

class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}

void show()
{
cout << "show base class" << endl;
}
};

class derived : public base {


public:
void print()
{
cout << "print derived class" << endl;
}

void show()
{
cout << "show derived class" << endl;
}
};

int main()
{
base* bptr;
derived d;
bptr = &d;

// virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();
}

Ques 3.Explain class and object in C++.


Ans.
Class: A class in C++ is the building block, that leads to Object-Oriented
programming. It is a user-defined data type, which holds its own data members and
member functions, which can be accessed and used by creating an instance of that
class. A C++ class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties like all of them
will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and
wheels, speed limits, mileage are their properties.

A Class is a user defined data-type which has data members and member functions.
Data members are the data variables and member functions are the functions used to
manipulate these variables and together these data members and member functions
defines the properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage etc
and member functions can be apply brakes, increase speed etc.

An Object is an instance of a Class. When a class is defined, no memory is


allocated but when it is instantiated (i.e. an object is created) memory is
allocated.

A class is defined in C++ using keyword class followed by the name of class. The
body of class is defined inside the curly brackets and terminated by a semicolon at
the end.

Declaring Objects: When a class is defined, only the specification for the object
is defined; no memory or storage is allocated. To use the data and access functions
defined in the class, you need to create objects.

Syntax:

ClassName ObjectName;

You might also like