You are on page 1of 3

C++ programming Date : 15/9/23

Write a program in c++ to find a element in an array at a particular position using pointer
#include<iostream>
using namespace std;
main (){
int n, k, i;
cout<<"Enter number of elements :";
cin>>n;
int a[n];
for (i=0;i<n;i++){
cout<<"Enter :";
cin>> (a[i]);
}
cout<<"\nEnter the position :";
cin>>k;
cout<<"\nElement is :"<<* (a+k−1);
}
OUTPUT:

Write a program in c++ to print all elements of an array using pointers


#include<iostream>
using namespace std;
main (){
int n, k, i;
cout<<"Enter number of elements :";
cin>>n;
int a[n];
for (i=0;i<n;i++){
cout<<"Enter :";
cin>>(a[i]);
}
int *p=&a[0];

for (i=0;i<n;i++){
cout<<*p<<",";
p++;
}
OUTPUT:
Write a program in c++ to print address of all elements of an array using pointer

#include<iostream>
using namespace std;
main(){
int n,k,i;
cout<<"Enter number of elements :";
cin>>n;
int a[n];
for(i=0;i<n;i++{
cout<<"Enter :";
cin>>(a[i]);
}
int *p=&a[0];
for(i=0;i<n;i++{
cout<<p<<",";
p++;
}
}
OUTPUT:
Write a program that has a class name employee that stores the employee's age and name.
Make two functions
getData() and putData() to take input and display output respectively

#include<iostream>
#include<string>
using namespace std;
class employee{
public:
string name;
int age;
void getData(string n,int a);
void putData();
};
void employee::getData(string n,int a){
name=n;
age=a;
}
void employee::putData(){
cout<<endl;
cout<<name<<"\t"<<age;
}
main(){
string name;
int n,age,i;
cout<<"Number of employees :";
cin>>n;
employee a[n];
for(i=0;i<n;i++){
cout<<"Enter name:";
cin>>name;
cout<<"Enter age :";
cin>>age;
a[i].getData(name,age);
}
cout<<"Name\tAge";
for(i=0;i<n;i++){
a[i].putData();
}
}

OUTPUT:

You might also like