Thursday, September 20, 2012

C++ Accessing Structures Members Part I

How do you access a structure member? Well.... simple, structure tag + variable.structure member
The dot operator is very important you need it in order to access the structure member.


  • Given a variable, mets, of type Team-- a structured type with a field named pitcher (a string), write a statement that assigns "Acosta" to that field (pitcher) of that variable (mets).
struct Team
{
string pitcher;
};
mets.pitcher="Acosta";



  • The "origin" of the cartesian plane in math is the point where x and y are both zero. Given a variable, origin of type Point-- a structured type with two fields, x and y, both of type double, write one or two statements that make this variable's field's values consistent with the mathematical notion of "origin".

struct Point
{
double x, y;
};
origin.x=0;
origin.y=0;

  •  Assume you have a variable price1 of type Money where the latter is a structured type with two int fields, dollars and cents. Assign values to the fields of price1 so that it represents $29.95.
struct Money
{
int dollars, cents;
};
price1.dollars = 29;
price1.cents = 95;

  •  Assume you have a variable sales of type Money where the latter is a structured type with two int fields, dollars and cents. Write a statement that reads two integers from standard input, a dollar amount followed by a cents amount, and store these values in the appropriate fields in sales.
struct Money
{
int dollars,cents;
};

int x,y;
cin>>x>>y;
sales.dollars= x;
sales.cents= y;

  •  Assume two variables p1 and p2 of type POINT, with two fields, x and y, both of type double, have been declared. Write a statement that reads values for p1 and p2 in that order. Assume that values for x always precede y
struct POINT{
double x, y;};

double a,b,c,d;
cin>>a>>b>>c>>d;
p1.x= a;
p1.y=b;
p2.x= c;
p2.y=d;

  •  Assume that a new type called  POINT has been defined-- it is a structure consisting of two fields,  x and  y , both of type  double . Assume two variables  p1 and  p2 of type  POINT have been declared. Assume that  p1 has already been initialized. Write some code that makes  p2 the reflection of  p1 : in other words, give p2's  x field the value of p1's  y field, and give p2's  y field, the value of p1's  x field.
p2.x = p1.y;
p2.y = p1.x;

  •  In mathematics, "quadrant I" of the cartesian plane is the part of the plane where x and y are both positive. Given a variable, p that is of type POINT-- a structured type with two fields, x and y, both of type double-- write and expression that is true if and only the point represented by p is in "quadrant I".
p.x >0&&
p.y >0

Will continue tomorrow with the rest, busy tonight

No comments:

Post a Comment