100% found this document useful (1 vote)
25 views

c++

notes

Uploaded by

Subhan Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
25 views

c++

notes

Uploaded by

Subhan Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ Pointers

In C++, pointers are variables that store the memory addresses of other variables. If we have
a variable var in our program, &var will give us its address in the memory.

For example,

Example 1: Printing Variable Addresses in C++

#include <iostream>
using namespace std;
int main()
{

// declare variables

int var1 = 3;
int var2 = 24;
int var3 = 17;
// print address of var1
cout << "Address of var1: "<< &var1 << endl;
// print address of var2
cout << "Address of var2: " << &var2 << endl;
// print address of var3
cout << "Address of var3: " << &var3 << endl;
}

Output:

Address of var1: 0x7fff5fbff8ac


Address of var2: 0x7fff5fbff8a8
Address of var3: 0x7fff5fbff8a4
Here, 0x at the beginning represents the address is in the hexadecimal form.
C++ Pointers
As mentioned above, pointers are used to store addresses rather than values. Here is how
we can declare pointers.

int *pointVar;

We can also declare pointers in the following way.

int* pointVar; // preferred syntax

Let's take another example of declaring pointers.

int* pointVar, p;

GET THE VALUE FROM THE ADDRESS USING POINTERS

To get the value pointed by a pointer, we use the * operator. For example:

int* pointVar, var;


var = 5;

// assign address of var to pointVar


pointVar = &var;

// access value pointed by pointVar


cout << *pointVar << endl; // Output: 5

EXAMPLE: Write a program in C++ to calculate sum of two numbers using pointers?

#include <iostream>
using namespace std;
int main()
{ int num1, num2;
int *ptr1, *ptr2;
int sum;
cout << "\n Enter first number: ";
cin >> num1;
cout << "\n Enter second number: ";
cin >> num2;
ptr1 = &num1; //assigning an address to pointer
ptr2 = &num2;
sum = *ptr1 + *ptr2; //values at address stored by pointer
cout << "\n Sum is: " << sum;
return 0;
}
ARRAYS EXERCISE

1. Take 10 integer inputs from user and store them in an array and print them on
screen.

#include<iostream>
using namespace std;

int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cout << "Enter a number\n";
cin >> a[i];
}
cout << "Numbers are:\n";

for(int i=0;i<10;i++)
{
cout << a[i] << "\n";
}
return 0;
}

2. Take 10 integer inputs from user and store them in an array. Again ask user to
give a number. Now, tell user whether that number is present in array or not.

3. Take 20 integer inputs from user and print the following:

number of positive numbers


number of negative numbers
number of odd numbers
number of even numbers
number of 0.

#include<iostream>
using namespace std;

int main()
{
int z[20];
int pos = 0;
int neg = 0;
int odd = 0;
int even = 0;
int zero = 0;
for(int i=0;i<20;i++)
{
cout << "Enter a number\n";
cin >> z[i];

if(z[i]>0)
pos++;
else if(z[i]<0)
neg++;
else
zero++;
if(z[i]%2==0)
even++;
else
odd++;
}
cout << "Positive " << pos << "\nNegative " << neg <<"\nZero
"<<zero<<"\nOdd "<<odd<<"\nEven "<<even<<"\n";
return 0;
}

4. Take 10 integer inputs from user and store them in an array. Now, copy all the
elements in another array but in reverse order.

#include<iostream>
using namespace std;
int main()
{
int arr[10], i;
cout<<"Enter 10 Array Elements: ";
for(i=0; i<10; i++)
cin>>arr[i];
cout<<"\nThe Original Array is:\n";
for(i=0; i<10; i++)
cout<<arr[i]<<" ";
cout<<"\n\nThe Reverse of Given Array is:\n";
for(i=(10-1); i>=0; i--)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}

5. Write a program to find the sum and product of all elements of an array.

#include<iostream>
using namespace std;
int main()
{
int array[10],n,i,sum=0,product=1;
cout<<"enter the size of array"<<endl;
cin>>n;
cout<<"enter the element of array"<<endl;
for(i=0;i<n;i++)
cin>>array[i];
for(i=0;i<n;i++){
sum=sum+array[i];
product=product*array[i];
}
cout<<"sum :"<<sum<<endl;
cout<<"product :"<<product<<endl;
return 0;
}

6. Initialize and print all elements of a 2D array.

#include<iostream>
using namespace std;
main( )
{
int arr[4][2] = { { 10, 11 }, { 20, 21 }, { 30, 31 }, { 40, 41 }} ;
int i,j;

cout<<"Printing a 2D Array:\n";
for(i=0;i<4;i++)
{
for(j=0;j<2;j++)
{
cout<<"\t"<<arr[i][j];
}
cout<<endl;
} }

7. Find the largest and smallest elements of an array.

#include<iostream>
using namespace std;

int main()
{
int a[] = {23,6,328,34,12,234,9,23,4,54};
int largest = a[0];
int smallest = a[0];

for(int i=0;i<10;i++)
{
if(a[i]>largest)
largest = a[i];
if(a[i]<smallest)
smallest = a[i];
}
cout << "Largest "<<largest<<"\nSmallest "<<smallest<<"\n";
return 0;
}

8. Write a program to check if elements of an array are same or not it read from front
or back. E.g.-

2 3 15 15 3 2

#include<iostream>
using namespace std;
int main()
{
int a[] = {2,3,15,15,3,2};
int read = 1;
int i,j = 5;
for(i =0;i<6/2;i++)
{
if(a[i]!=a[j])
{
read = 0;
break;
}
else
j--;
}
cout << read << "\n";
return 0;
}

You might also like