Answer to Question #213856 in C++ for Muhammad

Question #213856

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-06T00:32:53-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(4,6,"tirangle");
	shape(3,7,"rectangle");
	
	shape(66.2,4.9,"tirangle");
	shape(5.6,5.3,"rectangle");
	
	int intNumbers[] = {10, 5, 8, 14, 12,32,85,47,66,12};
	float floatNumbers[] = {12.6, 25.8, 36.35, 26.95, 11.41,85.12,39.62,85.12,47.95,15.74};
	cout<<"\nInteger numbers:\n";
	for(int i=0;i<10;i++){
		cout<<intNumbers[i]<<" ";
	}
	cout<<"\nValue at index 6 = "<<show_master(6,intNumbers);
	cout<<"\nFloat numbers:\n";
	for(int i=0;i<10;i++){
		cout<<floatNumbers[i]<<" ";
	}
	cout<<"\nValue at index 5 = "<<show_master(5,floatNumbers)<<"\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