0% found this document useful (0 votes)
14 views5 pages

Oop - 01

Uploaded by

Awais Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Oop - 01

Uploaded by

Awais Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Pointer Basics: Write a program to declare an integer variable, assign it a value, and then use a
pointer to access and print the value of the variable as well as its memory address.
#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptrNum;
ptrNum = &num;

cout << "The value of num is: " << *ptrNum << endl;
cout << "Memory address of num is: " << ptrNum << endl;

return 0;
}

2. Dereferencing a Pointer: Write a program that declares a pointer to an integer, assigns the address
of a variable to it, modifies the value of the variable using the pointer, and prints the modified value.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int* ptrA = &a;

cout << "Value of a (before modification): " << a << endl;


cout << "Enter a new value for a --> ";
cin >> *ptrA;
cout << "\nValue of a (after modification): " << a << endl;

return 0;
}

3. Pointer Arithmetic: Create a program that declares an array of integers. Use a pointer to traverse
the array and print each element by incrementing the pointer.
#include <iostream>
using namespace std;
int main() {
int arr[] = { 10, 20, 30, 40, 50 };
int size = sizeof(arr) / sizeof(arr[0]);
int* ptrArr = arr;

for (int i = 0; i < size; i++) {


cout << "Element " << i + 1 << " : " << *ptrArr << endl;
ptrArr++;
}

return 0;
}
4. Pointer and Arrays: Write a program that dynamically allocates memory for an array of
integers, takes user input for the size of the array and its elements, and then prints the elements using
pointer arithmetic.
#include <iostream>
using namespace std;
int main() {
int size;

cout << "Enter the size of the array: ";


cin >> size;

int* arr = new int[size];

for (int i = 0; i < size; i++) {


cout << "Enter the Value-" << i + 1 << ": ";
cin >> arr[i];
}
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
delete[]arr;
return 0;
}

5. Pointer to Function: Implement a simple function that swaps two integers using pointers. Write a
swap function that accepts pointers as arguments, and swap the values of two variables in the main
function.
#include <iostream>
using namespace std;
void swapf(int*, int*);
int main() {
int num1 = 10;
int num2 = 20;
cout << "Before swap function.\n";
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;

swapf(&num1, &num2);

cout << "After swap function.\n";


cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
return 0;
}

void swapf(int* a, int* b)


{
int temp = *a;
*a = *b;
*b = temp;
}
6. Passing Pointers to Functions: Create a program where you pass an array and its size as arguments
to a function using pointers, and the function should return the sum of the array elements.
#include <iostream>
using namespace std;
void arrP(int* arr, int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
}
int main() {
int arr1[] = { 10, 20, 30, 40 };
int size = sizeof(arr1) / sizeof(arr1[0]);

arrP(arr1, size);
return 0;
}

7. Dynamic Memory Allocation: Write a program that dynamically allocates memory for storing the
grades of students, takes input from the user for the number of students and their grades, and then
calculates and prints the average grade.
#include <iostream>
using namespace std;
int main() {
int noOfStu;
int sum;
double avg;

cout << "Enter the number of Students: ";


cin >> noOfStu;

int* grade = new int[noOfStu];

for (int i = 0; i < noOfStu; i++) {


cout << "\nEnter the grade(percentage) of Student-" << i + 1 << ": ";
cin >> grade[i];
}

sum = 0;
for (int i = 0; i < noOfStu; i++) {
sum += grade[i];
}

avg = static_cast<double>(sum) / noOfStu;


cout << "The average Grade = " << avg << endl;

return 0;
}
8. Pointer to Pointer: Write a program that demonstrates the use of a pointer to a pointer. Declare an
integer variable and a pointer to it. Then, declare another pointer that stores the address of the first
pointer, and use this to print the value of the integer.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int* ptra = &a;
int** ptrTOptra = &ptra;

cout << "Value of a = " << **ptrTOptra << endl;


return 0;
}

9. Void Pointer: Create a program that demonstrates the use of a void pointer. Declare variables of
different types (int, float, char), and assign their addresses to the void pointer. Use typecasting to
print the values stored at the addresses pointed to by the void pointer.
#include <iostream>
using namespace std;
int main() {
int a = 10;
float b = 2.65;
char name = 'b';

void* ptrA = &a;


void* ptrB = &b;
void* ptrName = &name;

cout << "a = " << *(int*)ptrA << "\nb = " << *(int*)ptrB << "\nname = " <<
*(int*)ptrName << endl;
return 0;
}

10. Dynamic 2D Array Using Pointers: Write a program that dynamically allocates a 2D array using
pointers. Ask the user for the number of rows and columns, take input for each element, and then
print the entire 2D array.
#include <iostream>
using namespace std;

int main() {
int** array;
int r, c;

cout << "Enter the ROWS and COLUMNS, respectively: ";


cin >> r >> c;

// Allocate memory for rows


array = new int* [r];
for (int i = 0; i < r; i++) {
array[i] = new int[c]; // Allocate memory for columns
}

// Input elements into the array


for (int i = 0; i < r; i++) {
cout << "Enter " << c << " number(s) for row number " << i + 1 << ":
for (int j = 0; j < c; j++) {
cin >> array[i][j];
}
cout << endl;
}

// Print the 2D array


cout << "The 2D array is:\n";
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << array[i][j] << " ";
}
cout << endl;
}

// Deallocate memory
for (int i = 0; i < r; i++) {
delete[] array[i];
}
delete[] array;

return 0;
}

You might also like