VECTOR

 

USING VECTOR CLASS

Note: std::cout, std::cin, std::endl are used to resolve compilation error, you can use cout, cin, endl instead of that.

#include <iostream>
#include <vector>
using namespace std;

void display(vector<int> &v){
	
	std::cout<<"Displaying the vector: ";
	for (int i=0;i< v.size();i++)
	{
		std::cout << v[i] <<"  ";
	// std::cout << v.at(i) <<" ";
	}
	std::cout << "" << std::endl;
}

int main()
{
	std::cout<<std::endl<<"\t\tZero length vector"<<std::endl;
	vector<int> vec1; 
	int element,size=5; 	//cplusplus vector methods
	
	std::cout << "Enter the size of your vector: ";
	std::cin>>size;

	std::cout<<std::endl<<"\t\tpush_back in vector"<<std::endl;
	for(int i=0; i<size; i++)
	{
		std::cout << "Enter an element to add to the vector: ";
		std::cin>>element;
		vec1.push_back(element);
	}

	std::cout << "" << std::endl;
	display(vec1);

	std::cout<<std::endl<<"\t\tpop_back in vector"<<std::endl;
	vec1.pop_back();
	display(vec1);

	std::cout<<std::endl<<"\t\titerator in vector"<<std::endl;
	vector<int> :: iterator iter=vec1.begin();
	vec1.insert(iter, 566); // oth 
	//vec1.insert(iter+1, 566); 1st position
	//vec1.insert(iter+1,5, 566); 5 for copy 5copy
	display(vec1);

	std::cout<<std::endl<<"\t\t4 element int vector"<<std::endl;
	vector<int> vec2(4); 
	//vector<char> vec2(4); //  4 element Char vector
	vec2.push_back(5);
	display(vec2);

	std::cout<<std::endl<<"\t\tvector from vector"<<std::endl;
	vector<int> vec3(vec2); // vector from vec3
	display(vec3);
	
	std::cout<<std::endl<<"\t\trepeat value in vector"<<std::endl;
	vector<int> vec4(6,3); // 6 element vector (repeat, value)
	display(vec4);

	return 0;
}

INPUT / OUTPUT :

		Zero length vector
Enter the size of your vector: 2

		push_back in vector
Enter an element to add to the vector: 1
Enter an element to add to the vector: 2

Displaying the vector: 1  2  

		pop_back in vector
Displaying the vector: 1  

		iterator in vector
Displaying the vector: 566  1  

		4 element int vector
Displaying the vector: 0  0  0  0  5  

		vector from vector
Displaying the vector: 0  0  0  0  5  

		repeat value in vector
Displaying the vector: 3  3  3  3  3  3  


CREATE OWN VECTOR:

Create own vector class to represent the vector (series of float value) include member functions to perform the following:
a) To create the vector
b) To modify the vector of the given element.
c) To multiply by a scalar.
d) Display vector in the form(10, 20, 30, …)

PROGRAM:

#include <iostream>
#include <stdlib.h>

class vector{
	
	int a[3], i;
	public:
	void create(){
	std::cout<<"Enter 3 value into array: "<<std::endl;
	for(i=0;i<3;i++)
	{
		std::cout<<"Enter value: ";
		std::cin>>a[i];
	}
	}
	
	void modify()
	{
		std::cout<<"Do you want to modify the Vector?(Y/N): ";
		char c;
		std::cin>>c;
		if(c=='Y'||c=='y'){
			std::cout<<"Enter the position of the value you want to modify: ";
			int n;
			std::cin>>n;
			std::cout<<"Enter the new value: ";
			int k; std::cin>>k;
			a[n-1]=k;
		}

	}

	void mult()
	{
		std::cout<<"Enter the scalar value: ";
		int s;
		std::cin>>s;
		for(i=0; i<3; i++)
		{
			a[i]=a[i]*s;
		}
	}
	
	void display(void){
		std::cout<<"The vector is: " <<std::endl;
		std::cout<<"( ";
		for(int i=0;i<3;i++){
			if (i<2)
				std::cout<<a[i]<< ", ";
			else	
				std::cout<<a[i];
				}

		std::cout<<" )";
	}

};

int main()
{
	vector v;
	char b;
	int x;
	v.create();
	do{

	std::cout<<std::endl;
	std::cout<<"1. Modify the vector"<<std::endl;
	std::cout<<"2. Multiply the vector"<<std::endl;
	std::cout<<"3. Display the vector"<<std::endl;
	std::cout<<"4. Exit"<<std::endl;
	std::cout<<"Enter your choice: "<<std::endl;
	std::cin>>x;
	switch(x){
	case 1:
		v.modify();
		v.display();
		break;
	case 2:
		v.mult();
	case 3:
		v.display();  break;
	case 4:
		exit(1); 
	default:
		std::cout<<"Invalid Input"<<std::endl;
	}
	std::cout<<std::endl<<"Do you want to continue? (Y/N): "; std::cin>>b;

	}while(b=='y'||b=='Y');

return 0;
}

INPUT / OUTPUT :

Enter 3 value into array: 
Enter value: 1
Enter value: 2
Enter value: 3

1. Modify the vector
2. Multiply the vector
3. Display the vector
4. Exit
Enter your choice: 
3
The vector is: 
( 1, 2, 3 )
Do you want to continue? (Y/N): y

1. Modify the vector
2. Multiply the vector
3. Display the vector
4. Exit
Enter your choice: 
2
Enter the scalar value: 5
The vector is: 
( 5, 10, 15 )
Do you want to continue? (Y/N): y

1. Modify the vector
2. Multiply the vector
3. Display the vector
4. Exit
Enter your choice: 
1
Do you want to modify the Vector?(Y/N): y
Enter the position of the value you want to modify: 2
Enter the new value: 4
The vector is: 
( 5, 4, 15 )
Do you want to continue? (Y/N): n

Programming skills are very important for Computer related courses. Starting from beginning to an expert, Shout Coders helps you to build your career in computational fields. Coder's do some shout in comments.

Comments

Popular posts from this blog

C PROGRAMMING NOTE

CONSTRUCTOR & DESTRUCTOR

MATRIX MULTIPLICATION