Initialization of one-dimensional array by the user
In this process, we assign different values to the element of an array as per the choice of the user. Each element is assigned a value entered by the user. It is stored collectively in individual memory spaces. It is then displayed on the output screen by printing the elements individually.
Write a program in C++ to initialize an array from the user and print it.
We have to consider an array. Values must be entered individually using for loop statement and printed using another for loop.
Program
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
cout<<"\nInput of array\n\n";
for(i=0;i<5;i++)
{
cout<<"Enter the value in a["<<i<<"]= ";
cin>>a[i];
}
cout<<"\nDisplaying of array\n";
for(i=0;i<5;i++)
{
cout<<"\nThe value in a["<<i<<"]= "<<a[i];
}
getch();
}
Output
INITIALIZATION OF ONE-DIMENSIONAL ARRAY |
Explanation
We have assigned a value to each and every element one by one as per the input of the user. We used a for loop for obtaining the elements of the array individually and also for displaying them. In the above program, we used a for loop for a[i] during input and display of array.
No comments:
Post a Comment