Answer to Question #213912 in C++ for Drack

Question #213912

You have been developing a small commercial Software for numeric operations. You are working on the calculation of areas and volume of different shapes. Based on your software complete these tasks:

a)     Create a function template named as shape() that accepts three arguments in such a way that first and second will be the dimension like base and height and the third will be the name of the shape. The shape() function displays the dimension values and the area of the shape. The function returns the area to the calling program; the area is the same data type as the parameter. [Note: Design a main program such that, it input the values of only those shapes whose area is computable on the bases of base and height only]

b)     Create another function template named as show_master()that accepts two arguments. The first is index and the second argument is an array of 10 values. The show_master() function will returns the specified index value.



1
Expert's answer
2021-07-05T13:28:55-0400
#include <iostream>
#include <string>


using namespace std;




template <typename T>
T shape(T base,T height, string name){
	T area=0;
	if(name=="tirangle"){
		area=0.5*base*height;
	}


	if(name=="rectangle"){
		area=base*height;
	}
	
	cout<<"Base is: "<<base<<"\n";
	cout<<"Height is: "<<height<<"\n";
	cout<<"The area of the shape "<<name<<" is: "<<area<<"\n";
	return area;
}


template <typename T>
T show_master(int index,T values[]){
	return values[index];
}


int main () {


	shape(2,10,"tirangle");
	shape(10,6,"rectangle");
	
	shape(45.8,7.8,"tirangle");
	shape(4.4,3.8,"rectangle");
	
	int integers[] = {45, 45, 78, 25, 10,45,56,89,3,56};
	double doubles[] = {45.5, 45.45, 78.45, 25.78, 10.8,45.78,55.78,12.42,3.23,78.56};
	cout<<"\n";
	for(int i=0;i<10;i++){
		cout<<integers[i]<<" ";
	}
	cout<<"\nValue at index 5 = "<<show_master(5,integers);
	cout<<"\n";
	for(int i=0;i<10;i++){
		cout<<doubles[i]<<" ";
	}
	cout<<"\nValue at index 8 = "<<show_master(8,doubles);
	cout<<"\n\n";
	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
APPROVED BY CLIENTS