Write a C++ function int rectArea (int l en, int wid) that returns the area of a
ID: 3548893 • Letter: W
Question
Write a C++ function
int rectArea (int l en, int wid)
that returns the area of a rectangle with length l en and width wid. Test it with a main
program that inputs the length and width of a rectangle and outputs its area. Output the
value in the main program, not in the function.
Sample Input
6 10
Sample Output
The area of a 6 by 10 rectangl e is 60.
Part 2-----------
Write a recursive void function called rotateLeft that rotates the first n integers in
an array to the left. To rotate n items left, rotate the first n
Explanation / Answer
program 1
#include<iostream>
using namespace std;
int rectArea(int len,int wid);
int main()
{
int l,w,a;
cout<<"Enter the length and width of the rectangle ";
cin>>l>>w;
a = rectArea(l,w);
cout<<"The area of a "<<l<<" by "<<w<<" rectangle is "<<a<<endl;
system("pause");
}
int rectArea(int len,int wid)
{
int a;
a= len*wid;
return a;
}
program 2
#include<iostream>
using namespace std;
void rotateLeft(int *a,int m,int c);
int main()
{
int n;
cout<<"Enter the size of the array ";
cin>>n;
int a[n];
cout<<"Enter the elements of the array ";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"The elements of the array in order are ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<" Upto which array index do you want the array to be rotated? ";
int m;
cin>>m;
rotateLeft(a,m,m);
cout<<"After rotation the elements of the array are ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
system("pause");
}
void rotateLeft(int *a,int m,int c)
{
int k,l,n;
n=c;
if(m < 2)
return;
else{
k = a[m-2];
a[m-2] = a[m-1];
a[m-1] = k;
rotateLeft(a,m-1,c);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.