CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[C++] Practical C++ Part 6 : Arrays


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author Rae On 2007-04-29 11:15:27




View and vote on the article here: Practical C++ Part 6 : Arrays


Practical C++ Part 6 : Arrays

Category
C++
Summary
Body
Practical C++ Part 6 : Arrays


In this part of the series, we take a close look at arrays and strings.

An "array" is a group of variables having the same data type. The array grouping has one name, but can store logically related information in itself. Let us see, how we define a single-dimension array of integer type in C++,
int a[5];
The above statement makes an array of integer data type, that is, the elements stored in the array will be of integer type. The number of elements in the above array is 5,
a[0], a[1], a[2], a[3], a[4]
Let us see, how to input data into this array and then display it.
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int a[5];
    cout<<"Enter the elements of the array"<<endl;
    for(int i=0;i<=4;i++)
    cin>>a[i];    //Input of data[code]

[code]    clrscr();
    cout<<"The elements of the array are"<<endl;
    for(i=0;i<=4;i++)
    cout<<a[i]<<endl;	//Output of data
    getch();
}
Note that, if you declare an array of five elements, but supply only four initializing values, the fifth element isn't initialized, and so contains some garbage value.

Arrays do not have to be only single-dimension. They can be of any order, as long as you can program them. Let us take the example of a 2-D array, which is commonly used in representing matrices.
int mat[3][3];
Now this statement represents a 3x3 matrix, where the first element is mat[0][0] and the last element is mat[2][2]. Now we must remember that we may visualize them as a matrix with the first subscript representing rows and the second one columns, but in the memory of the computer they are stored as linear lists only.

We now focus our attention to strings. Strings ar not specific data-types as in Java, but are single-dimension character arrays. Take a look at the code snippet given below :-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
    clrscr();
    char name[10];
    cout<<"Enter your name"<<endl;
    gets(name);
    clrscr();
    cout<<"Hello "<<name<<endl;
    getch();
}
The 'gets' function is used for entering strings. Here we can also use the 'cin' statement. Outputting strings is done using the 'cout' statement.

References :-

http://www.programmersheaven.com

Using C++ by Rob McGregor

Let Us C++ by Yashvant Kanetkar

Written by Rae (January 23 2004)

Rae is a member of Knowledge Bank Tutorial Writers


This article was imported from the CyberArmy University site. (original author: rae)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02675 seconds


About Us | Privacy Policy | Mission Statement | Help