0% found this document useful (0 votes)
23 views

LAB 1 Solution

Uploaded by

shahzad nazir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

LAB 1 Solution

Uploaded by

shahzad nazir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

LAB 1

TASK 1Compile all sample programs and get familiar with pointers. Done

TASK 2 Create a C++ program to find the sum of 5 numbers using pointers

#include "stdafx.h" #include<iostream> using namespace std; int _tmain(int argc, _TCHAR*
argv[])
{ int a=2,b=4,c=6,d=6,e=6;
int *p1,*p2,*p3,*p4,*p5;
p1=&a; p2=&b; p3=&c; p4=&d;
p5=&e; int x; x=*p1+*p2+*p3+*p4+*p5; cout<<x;
system ("pause");
return 0; }

TASK 3 Create a C++ program to swap the values of two


variables using pointer notation. #include "stdafx.h" #include<iostream> using
namespace std;
int _tmain(int argc, _TCHAR* argv[])
{ int a=10,b=20,temp;
int *x,*y,*tem;
cout<<"VALUES BEFORE SWAP "<<a<<" and "<<b<<endl;
x=&a; y=&b;
tem=&temp; *tem=*x;
*x=*y;
*y=*tem;
cout<<"VALUES AFTER SWAPPING
"<<a<<" AND "<<b;
system ("pause");
return 0; }
TASK 4 Using the concept of pointer to pointer create a program which add two float
values *(*p1)+*(*p2) #include "stdafx.h" #include<iostream> using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
float a=10.4,b=9.8,*p1,*p2,**pa,**pb;
p1=&a; p2=&b; pa=&p1;
pb=&p2;
cout<<(**pa+**pb); system("pause");
return 0;
}

TASK 5 Create a C++ program to find the largest number from the array of 7 numbers
using pointer
#include "stdafx.h" #include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{ int a[7];
int max; int *p[7];
for(int i=0;i<7;i++)
{ cout<<"Enter the
number"; cin>>a[i];
p[i]=&a[i]; if(*p[i]>max)
max=a[i]; }
cout<<"MAXIMUM IS "<<max; system
("pause");
return 0; }
TASK 6 Create a program which print the table of 2 upto 12 using pointers.

#include "stdafx.h" #include<iostream> using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{ int x=2,y=1,*p1,*i;
p1=&x;
i=&y;
for(*i=1;*i<=12;(*i)++)
{ cout<<*p1<<"x"<<*i<<"="<<((*p1)*(*i))<<endl; }
system("pause"); return
0; }

You might also like