Thursday, July 9, 2009

Plesae solve using C++?

A C++ program that will calculate the area and perimeter of a rectangle by using the function template template%26lt;class T%26gt;. The area and perimeter are calculated by using following formula:





area = length * width


perimeter = 2 *(length + width)





The program will read the values of length and width from the user trough keyboard and passes these values to the function calculate(T x, T y). The function calculates the area and perimeter of the rectangle and displays the result on the screen. The function returns no value.

Plesae solve using C++?
soundz lik a hw question ta mee :))





try this the function isnt goin to return a value so im assuing that the coutz are going to be in the function..im noo expert at this but o well watever





template %26lt;class T%26gt;


void functionname(T x, T y)


{


T area, perimeter;





area=x*y;


perimeter=2*(x+y);





cout%26lt;%26lt;"area: "%26lt;%26lt; area%26lt;%26lt;endl;


cout%26lt;%26lt;"perimeter: "%26lt;%26lt; perimeter%26lt;%26lt;endl;


}





now if u plz send ur checkz out to...hahahahahaha
Reply:look you need to learn the difference between the funtion and a sub-routine


a sub-routine never returns a value


while a function always retursn a value





just have a look at the program that


have you written something like void calculate (x,y) or something like that because then your function will not return anything


it will be as good as a sub - routine no return value
Reply://the program:





#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void Area(int tx , int ty)


{


int a;


a = tx * ty;


cout%26lt;%26lt;"The area is: "%26lt;%26lt;a%26lt;%26lt;"/n";


}





void Per(int tx, int ty)


{


int p;


p = 2 * (tx+ty);


cout%26lt;%26lt;"The perimeter is "%26lt;%26lt;p%26lt;%26lt;"/n";


}


int main()


{


int l,w;


cout%26lt;%26lt;" Write the Lenght and Width of a rectangle:"%26lt;%26lt;"/n";


cin%26lt;%26lt;l;


cin%26lt;%26lt;w;


Area(l,w);


Per(l,w);


return 0;


}


No comments:

Post a Comment