Techtopz

(@techtopz)

Techtopz is a blog on programming and tech stuff.
Techtopz is now on UpHub.
Follow us for more updates.

    


C++ | Introduction to Arrays

The post will teach you arrays. The program below prints the numbers saved in arrays. The program is extendable, just look at the ‘extending it’ section below. Go enjoy the program. Lets begin…

What are arrays?
A array is collection of variables of same type i.e int,char,float (whatever is your requirement). The array is declared by :- []; i.e int a[10];
The advantage of array is that it helps the program, The programmer does not have to think of new names of variables every time and remember those names for further programming .

Program to print numbers using arrays

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a[10],i;
//Input the two numbers save them in arrays.
//Here for loop is used to input multiple numbers in array.
for(i=0;i<5;i++)
{
cout<<"Enter the no."<<endl;
cin>>a[i];
}
//show the output arrays
for(i=0;i<5;i++)
cout<<a[i]<<" ";
//get character
getch();
}


Output

Enter the no.
1
Enter the no.
2
Enter the no.
3
Enter the no.
4
Enter the no.
5
1 2 3 4 5

How does it work
1. You enter the number.
2. The number is saved in respective array.
3. The numbers are printed.

Extending it
The program can be extended by using char in place of int.

Explanation.
1. Include ‘iostream.h’ and ‘conio.h’ files.
2. Add void main.
3. Start program by first clearing the screen.
4. Declare the variables as int (name them as you want.)
5. Add the cout and cin of array.
6. Print the array.

At the end
You learnt creating the C++ program of Printing numbers using arrays. So now enjoy the program.


Login to like the post