Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to find sum of perfect square elements in an array using pointers.
In C programming, finding the sum of perfect square elements in an array using pointers involves traversing the array with pointer arithmetic and checking if each element is a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself (e.g., 1, 4, 9, 16, 25).
Syntax
int isPerfectSquare(int num); int sumPerfectSquares(int *arr, int size);
Algorithm
Step 1 − Read the number of elements in the array.
Step 2 − Input the array elements.
Step 3 − Initialize sum = 0 and declare a pointer variable.
Step 4 − Use pointer arithmetic to traverse the array.
Step 5 − For each element, check if it's a perfect square using sqrt() function.
Step 6 − If it's a perfect square, add it to the sum.
Step 7 − Return the final sum.
Example
The following program demonstrates finding the sum of perfect square elements using pointers −
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int sumPerfectSquares(int n, int *a) {
int i, sum = 0, m;
for (i = 0; i < n; i++) {
if (*(a + i) >= 0) { // Only positive numbers can be perfect squares
m = sqrt(*(a + i));
if (m * m == *(a + i)) {
sum += *(a + i);
}
}
}
return sum;
}
int main() {
int i, *a, n;
printf("Enter the size of array: ");
scanf("%d", &n);
a = (int*)malloc(n * sizeof(int));
if (a == NULL) {
printf("Memory allocation failed!<br>");
return 1;
}
printf("Enter the elements of array:<br>");
for (i = 0; i < n; i++) {
scanf("%d", a + i);
}
printf("Array elements: ");
for (i = 0; i < n; i++) {
printf("%d ", *(a + i));
}
printf("<br>");
printf("Sum of perfect square elements is %d<br>", sumPerfectSquares(n, a));
free(a);
return 0;
}
Enter the size of array: 9 Enter the elements of array: 1 2 3 4 5 9 10 11 16 Array elements: 1 2 3 4 5 9 10 11 16 Sum of perfect square elements is 30
How It Works
- The function
sumPerfectSquares()receives a pointer to the array and its size. - Pointer arithmetic
*(a + i)is used to access array elements instead ofa[i]. - For each element, we calculate its square root using
sqrt()function. - We verify if the number is a perfect square by checking if
m * m == *(a + i). - Only non−negative perfect squares (1, 4, 9, 16, 25, etc.) are added to the sum.
Key Points
- Memory is dynamically allocated using
malloc()and freed usingfree(). - Pointer arithmetic
*(a + i)provides direct memory access to array elements. - The
sqrt()function returns a floating−point value, so we cast it to integer for comparison. - Always check for NULL after
malloc()to handle memory allocation failures.
Conclusion
Using pointers to find the sum of perfect square elements demonstrates efficient memory access and pointer arithmetic in C. The approach combines mathematical computation with dynamic memory management for flexible array processing.
