- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Defining a struct (P.268)
#include <iostream>
using std::cout;
using std::endl;
struct Rectangle
{
short left;
short top;
short right;
short bottom;
};
int main()
{
Rectangle aRect;
cout << sizeof( Rectangle ) << endl;
cout << sizeof( aRect ) << endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Accessing the Members of a struct (P.269)
#include <iostream>
using std::cout;
using std::endl;
struct Rectangle
{
short left;
short top;
short right;
short bottom;
};
int area(Rectangle aRect)
{
return (aRect.right - aRect.left) * (aRect.bottom - aRect.top);
}
void changeRect(Rectangle aRect)
{
aRect.left += 50;
aRect.right -= 50;
cout << area(aRect) << endl;
}
int main()
{
Rectangle yard = { 100, 100, 300, 300 };
changeRect( yard );
cout << area( yard ) << endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Accessing Structure Members through a Pointer (P.275)
#include <iostream>
using std::cout;
using std::endl;
struct Rectangle
{
short left;
short top;
short right;
short bottom;
};
int area(Rectangle aRect)
{
return (aRect.right - aRect.left) * (aRect.bottom - aRect.top);
}
void changeRect(Rectangle* pRect)
{
*pRect.left += 50;
*pRect.right -= 50;
cout << area( *pRect ) << endl;
}
int main()
{
Rectangle yard = { 100, 100, 300, 300 };
changeRect( &yard );
cout << area( yard ) << endl;
return 0;
}