Arrays in C++
An array is a collection of consecutive elements of the same type that are stored together in memory. Arrays are indexed by an integer starting at 0. For example, you might want to store a list of one thousand measurements in an array like this:
double array[100];
The size of an array must always be a constant integer expression.
To access an array element, use square brackets and an integer expression. For example, to access the 487th entry in the array declared above you would say array[100]. (Remember that array elements are indexed starting at zero.) Individual entries (a.k.a. elements) of an array can be assigned values and used for computations just like any other variable.
Struct in C++:
A struct is a user-defined type that allows you to keep multiple values together that share a similar purpose. For example, when using a three-dimensional coordinate system, you might want to declare a struct like this:
struct coordinates
{
double x;
double y;
double z;
};
Each struct is declared with the keyword struct and has a user-specified name. Once declared , the struct can be used by name just like any other type.
Input/Output:
a. Output is accomplished using cout, which opens a “stream” to the standard output device (the screen). Data is inserted into the output stream using the << (insertion) operator.
b. Input is accomplished using cin, which opens a “stream” from the standard input device (keyboard). Data is retrieved from the stream using the >> (extraction) operator.
Note: Every cin should be prefaced by a cout to prompt the user.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: