C Plus Plus Programming

You can learn all c++ programming in this website easilly.

Responsive Ads Here

Adds

Monday, January 21, 2019

How to creat array in c++


Arrays
An Array is a group of consecutive memory location with same name and type. Simple variable is a single memory location with a unique name and type.But an array is a collection of different adjacant memory location. All these memory locations have one collective name and type.The Memory locations in the array is called length.
Each element in the array is accessed with reference to its position of location in the array.This position is called index or subscript.Each element in the array has a unique index.The index of first element is 0 and the index of last element is length -1. The value of the index is written in brackets along with the name of array

Example:

int marks[5]={40,55,63,17,22,68,89,97,89};


Array are used to store a large amount of simmilar kind of data.Suppose the user wants to store the marks of 100 students and declares 100 variables. it is time consuming proccess to use these varriabless individually. This proccess can be samplified by using array. 

Programme 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int arr[5];
cout<<"Enter Five Integers:"<<endl;
cin>>arr[0];
cin>>arr[1];
cin>>arr[2];
cin>>arr[3];
cin>>arr[4];
cin>>arr[5];
cout<<"The values in array are:"<<endl;
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
cout<<arr[4]<<endl;
cout<<arr[5]<<endl;
getch();
}

Using For Loop


#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int arr[5],i;
for(i=0;i<5;i++)
{
cout<<"Enter an integer:";
cin>>arr[i];
}
cout<<"The Values in Array are:<<endl;
for(i=0;i<5;i++)
cout<<arr[i]<<endl;
getch();
}


No comments:

Post a Comment