Thursday, September 20, 2012

Initializing Structures and Array of Structures Part I

What is interesting here is the use of constants. Why is good to use constants? Well is gonna provide you with more efficient code. If you use a constant you can easily change  the size of your array, you just assign a new value to your constant instead of changing one by one the size of the arrays. Use of constants = better code.
How to use a constant? constant THECONSTANTNAME = a value;
The name of a constant is all capitals by convention. Just in case, I think you provably know it, you gotta put the word constant before the constant name ;)
  •  Write a declaration for a variable salesAnnual of type Money (a structured type with two int fields, dollars and cents) and initialize it to represent $1,325,023.47.


struct Money{
int dollars, cents;};
Money salesAnnual;
salesAnnual.dollars = 1325023;
salesAnnual.cents = 47;


  •  Write a declaration for these variables: salesQuarter1, salesQuarter2, salesQuarter3, and salesQuarter4, all of type Money (a structured type with two int fields, dollars and cents) and initialize these variables to $350,249.22, $381,100.00, $291,527.06, and $142,447.38 respectively.


struct Money{
int dollars, cents;};
Money salesQuarter1;
Money salesQuarter2;
Money salesQuarter3;
Money salesQuarter4;
salesQuarter1.dollars=350249;
salesQuarter1.cents = 22;
salesQuarter2.dollars=381100;
salesQuarter2.cents = 0;
salesQuarter3.dollars=291527;
salesQuarter3.cents = 6;
salesQuarter4.dollars=142447;
salesQuarter4.cents = 38;



  •  Given a type Money that is a structured type with two int fields, dollars and cents declare an array monthlySales with 12 elements of type Money.

Money monthlySales[12];

  •  Assume a structured type, PrecinctReport, with these fields, address (a string), felonies, murders, and robberies. Declare an array named allPrecincts with NPRECINCTS elements, each of type PrecinctReport. (NPRECINCTS is a pre-declared constant.)
PrecinctReport allPrecincts[NPRECINCTS];




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

C++ Programming Lab STRUCTURES

I'm gonna be posting every day solutions. I've decided to start in order with C++. Beginning with structures. Here you have some exercises from the Pearson's Starting out with C++ chapter 11. This is an intermediate to advance book, although the first 10 chapters are the basics of the language.


  • Define a new type called  POINT that is a structure consisting of two fields,  x and  y , both of type  double . 
struct POINT
{
double x;
double y;
};

  •  Define a new type called  POINT3D that is a structure consisting of three fields,  x ,  y and  z , each of type  double .
struct POINT3D
{double x,y,z;};

  • Define a new type called  ADDRESS that is a structure consisting of seven fields,  suiteNumber ,  streetNumber ,  street ,  city ,  province ,  postalCode ,  country , all of type  string .
struct ADDRESS
{
string suiteNumber,streetNumber, street, city, province, postalCode,country;
};

  •  Define a new type called  DATE that is a structure consisting of three  int fields,  dayOfMonth ,  month and  year .
struct DATE{
int dayOfMonth,month,year;};

  • Assume that the type  NAME has already been defined. Define a new type,  SREC that is a structure consisting of two fields:  name (a NAME ) and  grade (a  string ). 
struct SREC
{
NAME name;
string grade;
};

  • Declare a structure whose tag name is Book and that contains exactly three fields (or members), each of type int. The first field is nCopies, the second field is nPages and the third field is nAuthors.
struct Book
{
int nCopies, nPages, nAuthors;
};

  •  Declare a structure whose tag name is DrawnLine and that contains exactly two fields (or members). The first field is letter and is of type char, the second field is number and is of type int.
struct DrawnLine
{
char letter;
int number;
};

  •  Declare a structure whose tag name is Money and that contains exactly two int fields, dollars and cents. 

struct Money
{
int dollars, cents;
};

  •  Declare a structure whose tag name is Server and that contains the following fields: manufacturer, a string, model and serialnum, both also strings, year, an int, clockSpeed, a double, cores, an int, ram, an int and finally storage an int.

struct Server
{
string manufacturer, model, serialnum;
int year,cores,ram,storage;
double clockSpeed;
};

  •  Given the declaration of a structure whose tag is Book write the declaration of a Book variable, bestSeller.
Book bestSeller;

  •  Given the declaration of a structure whose tag is ADDRESS, write the declaration of two ADDRESS variables, billing and shipping.
ADDRESS billing;
ADDRESS shipping;

  •  Given the declaration of a structure whose tag is DATE write the declaration of the following variables enrolled_on, paid_on, and completed_on, each of which is of type DATE..
DATE enrolled_on, paid_on,completed_on;

  • Given a structured type Money declare the following variables of that type: revenue, expenses, and profit. 
Money revenue, expenses, profit;

Programming Solutions Introduction

Hello fellow programmers:
Today I've decided to start a blog: "Programming Solutions", where you can find the answers to some programming exercises. Many websites like Chegg charge us for the solutions to exercises in our textbooks. It is up to every programmer the decision of copying or not. Sometimes we have a small mistake that it is working against  our code, maybe is a missing bracket or a capitalization, that is why I'm creating this blog, so you can check proven code with your solution and correct yourself. For those who are just copying the homework..... well it is up to you. Remember, the only way you are going to learn how to program is by doing it.
Cheers!!!!!!, hope you will find this helpful. :D
you van fallow me @mackenzieblog