Showing posts with label Data types. Show all posts
Showing posts with label Data types. Show all posts

Friday, 21 September 2018

Input and output of one-dimensional array

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


Manipulation of a one-dimensional array
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.



Also, see Introduction to matrix and types of array 






Primary data types used in C++ Programming with an example

Data types in C++ 


Data types are used during the declaration of a variable which determined the type of data a variable can store. Primarily they are of three types:
  • Integer 
          If we declare int a; then a can store only integers values like 12, 3, 457.
  • Character
          If we declare char a; then a can store character and symbols like &, @, a, h, i.
  • Float
          If we declare float a; then a can store decimal values like 7.3333, 8.25.


Write a program in C++ giving an idea of primary data types. 


We need to consider different variables with different data types. We need to store the input data in their respective variables and print them. This will let us know about different data types.

Program


#include<iostream.h>
#include<conio.h>
void main()
{
long int admno;
char name[20];
int age;
float prcnt;
clrscr();
cout<<"Enter your admission number=";       //Input part
cin>>admno;
cout<<"Enter your name=";
cin>>name;
cout<<"Enter your age=";
cin>>age;
cout<<"Enter your percentage=";
cin>>prcnt;
cout<<"\nInformation";                                  //Output part    
cout<<"\nAdmission number="<<admno;
cout<<"\nName="<<name;
cout<<"\nAge="<<age;
cout<<"\nPercentage="<<prcnt<<"%";
getch();
}



Output

Example of data types
DATA TYPES IN C++











Explanation


In the program, each variable has own data type and stores a particular value only. The float data type is used to store percentage as it is a decimal number. The int data type can't store decimal so a float data type is used. The char data type is used to store characters and symbols.
// is used to add a comment. It starts from // and ends at end of a line.