0% found this document useful (0 votes)
7 views3 pages

Practical 7

The document demonstrates the use of a generic vector class template in C++. It shows creating a vector of integers of a given size, initializing its elements, modifying an element, and multiplying the entire vector by a scalar value.

Uploaded by

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

Practical 7

The document demonstrates the use of a generic vector class template in C++. It shows creating a vector of integers of a given size, initializing its elements, modifying an element, and multiplying the entire vector by a scalar value.

Uploaded by

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

Practical 7

Name: Saurabh Asnare (SYCOA 276)

#include <iostream> cin >> vec[i];


using namespace std; }
}
// # Class Template // # Modify Vector
template <class T> void modify()
class Vector {
{ int pos;
private: cout << "\n# Modify Vector: " << endl;
// 'vec' is an array cout << " Previous Vector: ";
T *vec; display(1);
int size; up:
cout << "Enter position (0-" << size - 1
<< ") to make changes: ";
public:
cin >> pos;
// # Constructor
if (pos >= size)
Vector(int m)
{
{
cout << "Please enter correct
size = m;
position..!" << endl;
vec = new T[size];
goto up;
for (int i = 0; i < size; i++)
}
vec[i] = 0;
cout << "Enter new vector value: ";
}
cin >> vec[pos];
// # Create Vector
cout << " New Vector: ";
void create()
}
{
// # Multiply By Scalar
cout << "\n# Create Vector: " << endl;
void multiply()
for (int i = 0; i < size; i++)
{
{
T sc;
cout << " ";
cout << "\n# Multiply By Scalar: " <<
cout << "vec[" << i << "] = "; endl;
cout << " Previous Vector: "; cout << "\n# Enter size of the Vector: ";
display(1); cin >> size;
cout << "Enter scaler number to multiply // Creating an Integer Vector
with vector: ";
Vector<int> vec(size); //.... 'vec' is an object
cin >> sc; of class template 'Vector'
for (int i = 0; i < size; i++) vec.display(0);
vec[i] = vec[i] * sc; vec.create();
cout << " New Vector: "; vec.display(0);
} vec.modify();
// # Display Vector vec.display(1);
void display(int n) vec.multiply();
{ vec.display(1);
int i; cout << "\n";
if (n == 0) return 0;
{
cout << "\n# Display Vector: " <<
endl;
cout << " Vector: ";
}
cout << "(";
for (i = 0; i < size - 1; i++)
{
cout << vec[i] << ",";
}
cout << vec[i] << ")\n";
}
};

// # Main Function
int main()
{
int size;
cout << "\n# Generic Vector #\n";
output:
# Generic Vector #

# Enter size of the Vector: 3

# Display Vector:
Vector: (0,0,0)

# Create Vector:
vec[0] = 10
vec[1] = 20
vec[2] = 30

# Display Vector:
Vector: (10,20,30)

# Modify Vector:
Previous Vector: (10,20,30)
Enter position (0-2) to make changes: 1
Enter new vector value: 15
New Vector: (10,15,30)

# Multiply By Scalar:
Previous Vector: (10,15,30)
Enter scaler number to multiply with vector: 2
New Vector: (20,30,60)

You might also like