Arrays & Structures

Wednesday, 10 July 2013

Concepts of Arrays in C++

In this C++ Tutorial you will learn about Concepts of Arrays in C++, What is an array?, How to access an array element, Declaration of Array and How to Access Array Elements.

Concepts of Arrays in C++

In this C++ Tutorial you will learn about Concepts of Arrays in C++, What is an array?, How to access an array element, Declaration of Array and How to Access Array Elements.

What is an array?

An array is a group of elements of the same type that are placed in contiguous memory locations.

How to access an array element?

You can access an element of an array by adding an index to a unique identifier.


For example
Suppose Item is an array that has 4 integer values in it that is of int data type, then Item is internally represented as:



0

1

2

3

Item
Data Type : int







 

 

.
Declaration of Array:

Just like variables, We have to declare arrays before using them. The general syntax for declaring an array is
data type array name[number of elements];
In the above example the declaration of array Item would be written as:
int Item[4];
One of the key points when declaring arrays is the number of elements defined in the array argument must be a constant value. The size of the array must be known before its execution. This relates to the concept of dynamic memory allocation in C++ which will be covered later in the tutorial.
After declaration the next step is the initialization of array values.
Initialization of array:
If a programmer wants to initialize an array elements it can be done by specifying the values enclosed within braces { }.
For example, using the array Item, if a programmer wants to initialize integer values 10, 20, 30, 40 respectively to each of the array positions it can be written.
int Item[4] = {10,20,30,40};
So the above array would take values as below



0

1

2

3

Item
Data Type : int

10

20

30

40

 




One interesting fact is that the size of the array element can also be left blank, in which case the array takes the size of the array as the number of elements initialized within { }.
For example if the array takes the form as
int Item = {10,20,30,40,50};
Then the size of the array Item is 5 which is the number of elements initialized within { }.
Now the next step is to know how to access the array elements.


How to Access Array Elements:

Just like variable sit is possible to access any element of the array for reading or modifying.
The general format for accessing arrays:
array name[index]
For example in the above example of array initialized with
int Item[4] = {10,20,30,40};
When a programmer wants to access the 30 this can be written: 
Item[2]
Note that the array starts with index 0 and hence to access third element namely 30 one has to write array name with index 2.
Let us see the whole concept with a example:

#include <iostream.h>
int Item[ ] = { 10,20,30,40,50};
int i, outp=0;
void main()
{
   for(i=0;i<5;i++)
   {
       outp=outp+ Exfosys[i];
   }
   cout<< outp;
}

.
The output of the above program is:
150
This section covers the concept of array and single dimensional array.

What is a Multidimensional Array?

A Multidimensional array is an array of arrays.

How are Multidimensional Arrays represented in C++

Suppose a programmer wants to represent the two-dimensional array Item as an array with three rows and four columns all having integer elements. This would be represented in C++ as:
int Item[3][4];
It is represented internally as:
Item
Data Type: int



0
1
2
3
0




1




2






How to access the elements in the Multidimensional Array

Item
Data Type: int


 

0
1
2
3
0




1




2






Highlighted cell represent Item[1][2]
Based on the above two-dimensional arrays, it is possible to handle multidimensional arrays of any number of rows and columns in C++ programming language. This is all occupied in memory. Better utilization of memory must also be made.
Multidimensional Array Example: 



#include <iostream.h>
const int ROW=4;
const int COLUMN =3;
void main()
{
   int i,j;
   int Item[ROW][COLUMN];
   for(i=0;i<ROWS;i++)
   for(j=0;j<COLUMN;J++)
   {
   cout << "Enter value of Row "<<i+1;
   cout<<",Column "<<j+1<<":";
   cin>>Item[i][j];
   }
   cout<<"\n\n\n";
   cout<< " COLUMN\n";
   cout<< " 1 2 3";
   for(i=0;i<ROW;i++)
   {
   cout<<"\nROW "<<i+1;
   for(j=0;j<COLUMN;J++)
   cout<<Item[i][j];
}



The output of the above program is
Enter value of Row 1, Column 1:10
Enter value of Row 1, Column 2:20
Enter value of Row 1, Column 3:30
Enter value of Row 2, Column 1:40
Enter value of Row 2, Column 2:50
Enter value of Row 2, Column 3:60
Enter value of Row 3, Column 1:70
Enter value of Row 3, Column 2:80
Enter value of Row 3, Column 3:90
Enter value of Row 4, Column 1:100
Enter value of Row 4, Column 2:110
Enter value of Row 4, Column 3:120


         COLUMN
        1   2   3
ROW 1  10  20  30
ROW 2  40  50  60
ROW 3  70  80  90
ROW 4 100 110 120
In the above example, the keyword const (specifying constant) precedes the data type that specifies the variable ROW and COLUMN to remain unchanged in value throughout the program. This is used for defining the array Item ROW size and COLUMN size, respectively.

What is a Structure?

Structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name.

How to declare and create a Structure

Declaring a Structure:

The structure is declared by using the keyword struct followed by structure name, also called a tag. Then the structure members (variables) are defined with their type and variable names inside the open and close braces { and }. Finally, the closed braces end with a semicolon denoted as ; following the statement. The above structure declaration is also called a Structure Specifier. Example:
Three variables: custnum of type int, salary of type int, commission of type float are structure members and the structure name is Customer. This structure is declared as follows:




In the above example, it is seen that variables of different types such as int and float are grouped in a single structure name Customer.
Arrays behave in the same way, declaring structures does not mean that memory is allocated. Structure declaration gives a skeleton or template for the structure.
After declaring the structure, the next step is to define a structure variable.
How to access structure members in C++?
To access structure members, the operator used is the dot operator denoted by (.). The dot operator for accessing structure members is used thusly:
structure variable name. member name
For example:

A programmer wants to assign 2000 for the structure member salary in the above example of structure Customer with structure variable cust1 this is written as:

No comments:

Post a Comment