Chapter - 4
Structured Data Types : Arrays and Structures.
to understand the meaning of structure datatypes and its availability in C++. To appreciate the use and importance of Arrays in C++
to differentiate between the use and implementation of different types of Arrays To use structures as User Defined data type to write programs. To understand and use typedef
Structured Data types :
Students till now whatever data type we have used are just primitive data types like int , char , float , etc. All these datatypes are defined within the C++ compiler and that is why they are also called as primitive. We can define a length using int , a weight using float , a name using characters etc. but suppose I tell you “please define a fruit for me in program” ,then your mind starts wondering, as you can't define a fruit just with any one datatype as you did with length , weight etc. A Fruit itself is a composite entity having following attributes : color :
can be described with a name i.e. char [ ]
can be described with a name i.e. char[ ]
can be described with int i.e. 1 for summer , 2 for winter … price :
can be described as float
ans so on...
This means that to describe a fruit we need to have a collection of data-types bundled togeather so that all the attributes of the fruit can be captured. This is true for any real world thing around you say student , mobile , plant etc. So when we bundle many primitive data types together to define a real world thing then it is known as derived data type or structured data type or User defined data types.
In this chapter we would look onto two important structured data types , one is Array and the other one is Structure.
Sometimes we need to have variables in very large quantities , that too of same data type i.e. suppose we want 200 integer variables. In this situation will you declare 200 individual variables ? Absolutely not. This is because it will :
a) wastage of time as well time taking task
b) we won't be able to manage these 200 variables in our program , it will be difficult to remember the names of variable every now and then during programming. So there exist special structured data type in C++ to tackle this situation. C++ allows a programmer to bundle together all these same type of 200 variable under a same tag name called as Arrays. So we are observing that structuring data type means bundling primitive data type in some or other way so that it solves some special programming situations.
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier. For example, an array to contain 5 integer values of type int called myArr could be represented like this: myArr
where each blank panel represents an element of the array, that in this case are integer values of type int. These elements are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length.
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:
where datatype is a valid type (like int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets [ ]), specifies how many of these elements the array has to contain. Therefore, in order to declare an array called myArr as the one shown in the above diagram it is as simple as:
int myArr ;
NOTE: The elements field within brackets [ ] which represents the number of...
Please join StudyMode to read the full document