Task 1: Array me 5 integers input using pointer offset notation
#include <iostream>
using namespace std;
int main() {
int arr[5];
int *p = arr;
cout << "Enter 5 integers:\n";
for (int i = 0; i < 5; i++) {
cin >> *(p + i); // pointer offset notation
}
cout << "You entered:\n";
for (int i = 0; i < 5; i++) {
cout << *(p + i) << " ";
}
return 0;
}
Task 2: Sort array of 10 integers in ascending order (using function)
#include <iostream>
using namespace std;
void sortArray(int *arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (*(arr + i) > *(arr + j)) {
int temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}
int main() {
int arr[10];
cout << "Enter 10 integers:\n";
for (int i = 0; i < 10; i++)
cin >> arr[i];
sortArray(arr, 10);
cout << "Sorted array:\n";
for (int i = 0; i < 10; i++)
cout << arr[i] << " ";
return 0;
}
Task 3: Find maximum of two numbers using pointer
#include <iostream>
using namespace std;
int main() {
int a, b;
int *p1, *p2;
cout << "Input first number: ";
cin >> a;
cout << "Input second number: ";
cin >> b;
p1 = &a;
p2 = &b;
if (*p1 > *p2)
cout << *p1 << " is the maximum number";
else
cout << *p2 << " is the maximum number";
return 0;
}
Task 4: Print all permutations of a string using pointers
#include <iostream>
#include <cstring>
using namespace std;
void swap(char *x, char *y) {
char temp = *x;
*x = *y;
*y = temp;
}
void permute(char *str, int l, int r) {
if (l == r)
cout << str << " ";
else {
for (int i = l; i <= r; i++) {
swap((str + l), (str + i));
permute(str, l + 1, r);
swap((str + l), (str + i));
}
}
}
int main() {
char str[] = "abcd";
int n = strlen(str);
cout << "The permutations of the string are:\n";
permute(str, 0, n - 1);
return 0;
}
Task 5: Swap elements using call by reference
#include <iostream>
using namespace std;
void swapValues(int &a, int &b, int &c) {
int temp = a;
a = c;
c = b;
b = temp;
}
int main() {
int a, b, c;
cout << "Input value of 1st element: ";
cin >> a;
cout << "Input value of 2nd element: ";
cin >> b;
cout << "Input value of 3rd element: ";
cin >> c;
cout << "\nThe value before swapping are:\n";
cout << "element 1 = " << a << endl;
cout << "element 2 = " << b << endl;
cout << "element 3 = " << c << endl;
swapValues(a, b, c);
cout << "\nThe value after swapping are:\n";
cout << "element 1 = " << a << endl;
cout << "element 2 = " << b << endl;
cout << "element 3 = " << c << endl;
return 0;
}
Task 6: Convert kilogram to grams using pointers
#include <iostream>
using namespace std;
void kgToGram(float *kg, float *gram) {
*gram = (*kg) * 1000;
}
int main() {
float kg, gram;
cout << "Enter kilograms: ";
cin >> kg;
kgToGram(&kg, &gram);
cout << "Grams = " << gram;
return 0;
}
Task 7: Print string character by character using pointer
#include <iostream>
using namespace std;
int main() {
char str[50];
cout << "Enter a string: ";
cin.getline(str, 50);
char *p = str;
while (*p != '\0') {
cout << *p << endl;
p++;
}
return 0;
}
Task 8: Find length of string using pointers
#include <iostream>
using namespace std;
int main() {
char str[50];
cout << "Enter a string: ";
cin.getline(str, 50);
char *p = str;
int length = 0;
while (*p != '\0') {
length++;
p++;
}
cout << "Length of string = " << length;
return 0;
}

0 Comments