While we might suspect that a character is just a byte, containing the ascii value, we do not know that this is really the case. Nothing in the definition says that this is how it is done.
Note, the size (constant integer expression) must be known at compile time.
The size should be non-negative.
const int SIZE=25; ... int myArray[SIZE];
int i; for(i=0;i<SIZE;i++) { myArray[i] = 99; }
int min, max; int ary[ARRAY_SIZE]; ... min = ary[0]; max = ary[0]; for(i=1;i<ARRAY_SIZE; i++) { if (ary[i] > max) { max = ary[i] } if (min > ary[i]) { min = ary[i]; } }
max = INT_MAX ;would be a problem if you change the type. This solution is much better. In addition, you may look for max and min values where the absolute bounds might not be known. Think of strings, the min is (perhaps) easy, but the max is (for all intense and purposes) undefined.
You can not make any assumptions about the data in the array. max = 0; assumes that the array only contains positive numbers for example.
Problem: No control over variables contents. Classes overcome this problem by "hiding" data or limiting the ability to manipulate that data through the class interface. Clients may not change the data directly, but must call a function which can validate any data change.
Problem: Code and data not linked. Classes overcome this problem by encapsulating the member functions within the class.A struct is appropriate when there is only a collection of data, no constraints on the data values and no operations.
For this problem, you will implement a class which contains the geometric object square. A square is a polygon with four sides of equal length and all angles are 90 degrees. The length of a side must be at least 0. The program to use your class will need to compute the perimeter (P) and area (A) of the square. If l is the length of a side, P = 4l and A = l2
Domain: The length of the sides of the square, which must be non-negative. Operations: Constructor: set length to be 0 Set the length of the sides to some value Return the Perimeter of the square Return the Area of the square Return the length of a side.
class SquareT { public: SquareT(); void SetSide(int i); int Perimiter(); int Area(); int Length(); private: int length; }
I would not perform I/O in any of the functions except possibly a print function. You should Prvide functions that return the values so the client can print them out in any manner they desire.
I would provide a function that allows the client to set the length, but it must validate the length before it does so.
Only the functions to set length should have a parameter. Everything else should just use the stored length.
SquareT::SquareT(){ length = 0; } void SquareT::SetSide(int i){ if (i > 0) { length = i; } return; } int SquareT::Perimiter(){ return 4*length; } int SquareT::Area(){ return length* length; } int SquareT::Lenght(){ return length; }
SquareT s; s.SetSide(4); cout << "A square with length " << s.Length() << "has area " << s.Area() << " and perimeter " << s.Perimeter() << endl;