C++ Programming Exercises - Practice
Set 3
Concepts covered:
Pointers - Arrays - Functions - Input and Output - Loops - Conditional
Statements
Problems:
1. Swap Two Numbers Using Pointers
Write a C++ program that swaps two numbers using pointers. The program should
define a function swapNumbers which takes two integer pointers as arguments and
swaps their values.
Function Signature:
void swapNumbers(int *a, int *b);
Input:
Two integer values, num1 and num2, entered by the user.
Output:
After swapping, print the updated values of num1 and num2.
2. Reverse a List Using Pointers
Write a C++ program that reverses an array using pointers. Define a function
reverseList that takes a pointer to the list (array) and its size as parameters, and
reverses the array.
Function Signature:
void reverseList(int *arr, int size);
Input:
An integer array of size n entered by the user.
Output:
Print the reversed array.
3. Find the Maximum Element in an Array Using Pointers
Write a C++ program that accepts a dynamic array of doubles, and calls a function
findMax to find the maximum element. The function should use pointers to traverse the
array.
Function Signature:
double findMax(double *arr, int size);
Input:
A dynamic array of doubles, with size n, entered by the user.
Output:
The maximum element in the array.
4. Sum of Array Elements Using Pointers
Write a C++ program that calculates the sum of all elements in an array using pointers.
Define a function sumArray that takes a pointer to the array and its size as arguments.
Function Signature:
int sumArray(int *arr, int size);
Input:
An integer array of size n entered by the user.
Output:
Print the sum of the array elements.
5. Find the Length of a String Using Pointers
Write a C++ program that calculates the length of a string using pointers. Define a
function findLength that accepts a pointer to a character array (string) and returns its
length.
Function Signature:
int findLength(char *str);
Input:
A string entered by the user.
Output:
The length of the string.
6. Check if an Array Contains a Specific Element
Write a C++ program that checks whether a given integer exists in an array using
pointers. Define a function containsElement that takes a pointer to the array, its size,
and the target integer as arguments.
Function Signature:
bool containsElement(int *arr, int size, int target);
Input:
An integer array of size n and the target integer entered by the user.
Output:
Print whether the element is found in the array or not.
7. Print the Elements of an Array Using Pointers
Write a C++ program that prints all the elements of an integer array using pointers.
Define a function printArray that takes a pointer to the array and its size as
parameters.
Function Signature:
void printArray(int *arr, int size);
Input:
An integer array of size n entered by the user.
Output:
Print the elements of the array.
8. Count Occurrences of a Character in a String Using Pointers
Write a C++ program that counts the number of occurrences of a character in a string
using pointers. Define a function countOccurrences that accepts a pointer to the
string and a character to search for.
Function Signature:
int countOccurrences(char *str, char target);
Input:
A string and a character entered by the user.
Output:
Print the number of occurrences of the character in the string.
9. Find the Minimum Element in an Array Using Pointers
Write a C++ program that finds the minimum element in an integer array using pointers.
Define a function findMin that takes a pointer to the array and its size.
Function Signature:
int findMin(int *arr, int size);
Input:
An integer array of size n entered by the user.
Output:
Print the minimum element in the array.
10.Find the Product of All Elements in an Array Using Pointers
Write a C++ program that calculates the product of all elements in an integer array using
pointers. Define a function productArray that takes a pointer to the array and its size.
Function Signature:
int productArray(int *arr, int size);
Input:
An integer array of size n entered by the user.
Output:
Print the product of the array elements.