Answer to Question #295597 in C++ for Janvi

Question #295597

Write a C++ Program using Class and Object concept.


Function Type :- No Return Type No arguments


Perform the operations on array. Create three functions for


the following operations.


• 1


st Function to take an array.


• 2


nd Function to print an array.


• 3


rd Funciton to sort array elements and print them.

1
Expert's answer
2022-02-09T08:04:54-0500
#include <iostream>




template<typename T>
class MyArray
{


private:
	T* arr = nullptr;
	int size = 0;
public:
	void st()
	{
		std::cout << "Enter size of array : ";
		std::cin >> size;
		arr = new T[size];
		for (int i = 0; i < size; i++)
		{
			std::cout<<i + 1 << ".Enter value : ";
			std::cin >> arr[i];
		}
	}
	void nd()
	{
		for (int i = 0; i < size; i++)
		{
			std::cout << arr[i] << " ";
		}
		std::cout<<std::endl;
	}
	void rd()
	{
		for (int i = 0; i < size - 1; i++)
		{
			for (int j = 0; j < size - i - 1; j++)
			{
				if (arr[j ] > arr[j+1])
				{
					std::swap(arr[j], arr[j + 1]);
				}
			}
		}
		nd();
	}
};










int main()
{
	MyArray<int> arr;
	arr.st();
	std::cout << "Array values : \n";
	arr.nd();
	std::cout << "Array values after sort : \n";
	arr.rd();


	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog