📘 Section 9 — Pointer Practice
Practice using pointers with arrays and functions.
🎯 What You’ll Practice
- Moving through an array using a pointer
- Passing arrays to functions
- Returning results from functions
- Updating values through pointer parameters
- Using pointers to find values inside arrays
🧪 Exercises
Exercise 1: Print Array in Reverse
Task: Write a program to print elements in an array on integers in reverse order using a pointer.
💡 Hint
- Pointer can move backward using
ptr-- - Start from:
arr + size - 1
🟢 Click to Show Solution
#include <stdio.h>
void printReverse(int arr[], int size)
{
int *ptr = arr + size - 1;
printf("Reverse: ");
for (int i = 0; i < size; i++)
{
printf("%d ", *ptr);
ptr--;
}
printf("\n");
}
int main()
{
int arr[] = {1, -2, 3, 4, -5, 3, 1};
int size = sizeof(arr) / sizeof(arr[0]);
printReverse(arr, size);
return 0;
}
Exercise 2: Sum of Array Elements
Task: Write a function that calculates the sum of an array using pointers.
💡 Hint
- Use pointer arithmetic:
*(arr + i) - Accumulate values in a
sumvariable
🟢 Click to Show Solution
int sumArray(int arr[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += *(arr + i);
}
return sum;
}
Exercise 3: Count Even Numbers
Task: Write a function that counts how many elements are even in an array.
💡 Hint
- Even number condition:
% 2 == 0 - Traverse using pointer instead of indexing
🟢 Click to Show Solution
int countEven(int arr[], int size)
{
int count = 0;
for (int i = 0; i < size; i++)
{
if (*(arr + i) % 2 == 0)
{
count++;
}
}
return count;
}
Exercise 4: Count Positive and Negative Numbers
Task: Write a function that counts positive and negative numbers in an array using pointers.
💡 Hint
- Use pointers to modify values:
(*pos)++ - Check:
> 0and< 0
🟢 Click to Show Solution
void countPosNeg(int arr[], int size, int *pos, int *neg)
{
*pos = 0;
*neg = 0;
for (int i = 0; i < size; i++)
{
if (*(arr + i) > 0)
(*pos)++;
else if (*(arr + i) < 0)
(*neg)++;
}
}
Exercise 5: Find Maximum and Minimum
Task: Write a function that takes an integer array and its size as parameters and updates two integer pointers to point to the maximum and minimum values in the array.
💡 Hint
- Start with first element as initial max and min
- Compare each element and update values using
*
🟢 Click to Show Solution
void findMinMax(int arr[], int size, int *maxPtr, int *minPtr)
{
*maxPtr = arr[0];
*minPtr = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] > *maxPtr)
*maxPtr = arr[i];
if (arr[i] < *minPtr)
*minPtr = arr[i];
}
}
Exercise 6: Palindrome Array
Task: Write a function that checks if an array is a palindrome using pointers.
💡 Hint
- Use two pointers: start & end
- Move inward:
start++,end--
🟢 Click to Show Solution
int isPalindrome(int arr[], int size)
{
int *start = arr;
int *end = arr + size - 1;
while (start < end)
{
if (*start != *end)
return 0;
start++;
end--;
}
return 1;
}
Exercise 7: Count Numbers Greater Than Average
Task: Write a function that counts how many numbers are greater than the average of the array.
💡 Hint
- First compute sum → then average
- Compare each element with average
🟢 Click to Show Solution
int countGreaterThanAvg(int arr[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += *(arr + i);
}
float avg = (float)sum / size;
int count = 0;
for (int i = 0; i < size; i++)
{
if (*(arr + i) > avg)
count++;
}
return count;
}
⬅️ Back to Home