- (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.354)
#include <iostream>
using std::cout;
using std::endl;
struct Rational
{
int numerator;
int denominator;
}
int main()
{
Rational a;
Rational b;
a.numerator = 4;
a.denominator = 6;
b = a;
cout << b.denominator << 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).
// Initialize a struct (P.355)
#include <iostream>
using std::cout;
using std::endl;
struct Rational
{
int numerator;
int denominator;
};
int gcd(int a, int b)
{
if (a == 0)
{
return (b == 0 ? 0 : b);
}
else
return gcd(b % a, a);
}
int main()
{
Rational a = { 4, 6 };
Rational b;
b = a;
cout << ( b.denominator /= gcd(b.numerator, b.denominator) ) << 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.358)
#include <iostream>
using std::cout;
using std::endl;
struct Circle
{
int x;
int y;
int radius;
};
void MoveCircle(Circle& aCircle, int x, int y);
int main()
{
Circle a = { 10, 10, 5};
MoveCircle(a, 20, 30);
cout << a.x << endl;
return 0;
}
void MoveCircle(Circle& aCircle, int x, int y)
{
aCircle.x = x;
aCircle.y = y;
return;
}