Practical / Solution
Passing Arrays to Functions — Exercise 1
Problem
Write a C program to pass a one-dimensional array to a function
and display all its elements.
Program
#include <stdio.h>
void displayArray(int arr[], int n)
{
int i;
// Display array elements
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
// Read array elements
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Pass array and size to the function
printf("Array: ");
displayArray(arr, n);
return 0;
}
Explanation
An array can be passed to a function by providing its name.
The function receives access to the array elements along with its size.
Expected Output
Enter number of elements: 5
Enter 5 elements: 10 20 30 40 50
Array: 10 20 30 40 50
Passing Arrays to Functions — Exercise 2
Problem
Write a C program to pass an array to a function and calculate the
sum of all its elements.
Program
#include <stdio.h>
int arraySum(int arr[], int n)
{
int i, sum = 0;
// Add every array element
for (i = 0; i < n; i++)
{
sum = sum + arr[i];
}
return sum;
}
int main()
{
int arr[100], n, i, result;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Pass array to the function
result = arraySum(arr, n);
printf("Sum = %d\n", result);
return 0;
}
Explanation
The function receives the array and its size and processes each
element to calculate the total.
Expected Output
Enter number of elements: 5
Enter 5 elements: 10 20 5 15 10
Sum = 60
Passing Arrays to Functions — Exercise 3
Problem
Write a C program to pass an array to a function and find its largest
and smallest elements.
Program
#include <stdio.h>
void findValues(int arr[], int n, int *largest, int *smallest)
{
int i;
// Initialize using the first element
*largest = arr[0];
*smallest = arr[0];
// Compare remaining elements
for (i = 1; i < n; i++)
{
if (arr[i] > *largest)
*largest = arr[i];
if (arr[i] < *smallest)
*smallest = arr[i];
}
}
int main()
{
int arr[100], n, i;
int largest, smallest;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Pass array and addresses of result variables
findValues(arr, n, &largest, &smallest);
printf("Largest = %d\n", largest);
printf("Smallest = %d\n", smallest);
return 0;
}
Explanation
The array is passed to the function for processing, while pointers
are used to return the largest and smallest values.
Expected Output
Enter number of elements: 5
Enter 5 elements: 12 45 7 30 18
Largest = 45
Smallest = 7
Passing Arrays to Functions — Exercise 4
Problem
Write a C program to pass an array to a function and calculate its average.
Program
#include <stdio.h>
float arrayAverage(int arr[], int n)
{
int i, sum = 0;
// Calculate total of array elements
for (i = 0; i < n; i++)
{
sum = sum + arr[i];
}
return (float)sum / n;
}
int main()
{
int arr[100], n, i;
float average;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Pass array to the function
average = arrayAverage(arr, n);
printf("Average = %.2f\n", average);
return 0;
}
Explanation
The function calculates the sum of all elements and divides it by
the number of elements to obtain the average.
Expected Output
Enter number of elements: 5
Enter 5 elements: 10 20 30 40 50
Average = 30.00
Passing Arrays to Functions — Exercise 5
Problem
Write a C program to pass an array to a function and count the number
of even and odd elements.
Program
#include <stdio.h>
void countEvenOdd(int arr[], int n, int *even, int *odd)
{
int i;
*even = 0;
*odd = 0;
// Classify each element
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
(*even)++;
else
(*odd)++;
}
}
int main()
{
int arr[100], n, i;
int even, odd;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Pass array and result addresses
countEvenOdd(arr, n, &even, &odd);
printf("Even elements = %d\n", even);
printf("Odd elements = %d\n", odd);
return 0;
}
Explanation
The function examines every array element and uses pointer parameters
to return the two counts.
Expected Output
Enter number of elements: 6
Enter 6 elements: 10 15 22 7 18 9
Even elements = 3
Odd elements = 3
Passing Arrays to Functions — Exercise 6
Problem
Write a C program to pass an array to a function and search for
a given element using linear search.
Program
#include <stdio.h>
int searchElement(int arr[], int n, int search)
{
int i;
// Search elements one by one
for (i = 0; i < n; i++)
{
if (arr[i] == search)
{
return i;
}
}
return -1;
}
int main()
{
int arr[100], n, i, search, position;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &search);
// Pass array to search function
position = searchElement(arr, n, search);
if (position != -1)
printf("Element found at index %d.\n", position);
else
printf("Element not found.\n");
return 0;
}
Explanation
The function checks each array element and returns its index when
the required element is found. It returns -1 if no match exists.
Expected Output
Enter number of elements: 5
Enter 5 elements: 10 25 30 45 50
Enter element to search: 30
Element found at index 2.
Passing Arrays to Functions — Exercise 7
Problem
Write a C program to pass an array to a function and display its
elements in reverse order.
Program
#include <stdio.h>
void displayReverse(int arr[], int n)
{
int i;
// Start from the last array element
for (i = n - 1; i >= 0; i--)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Reverse order: ");
// Pass array to the function
displayReverse(arr, n);
return 0;
}
Explanation
The function receives the array and starts accessing it from the
last valid index, producing the reverse order.
Expected Output
Enter number of elements: 5
Enter 5 elements: 10 20 30 40 50
Reverse order: 50 40 30 20 10
Passing Arrays to Functions — Exercise 8
Problem
Write a C program to pass an array to a function and replace every
negative element with zero.
Program
#include <stdio.h>
void replaceNegative(int arr[], int n)
{
int i;
// Replace negative values
for (i = 0; i < n; i++)
{
if (arr[i] < 0)
{
arr[i] = 0;
}
}
}
int main()
{
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Pass array for modification
replaceNegative(arr, n);
printf("Modified array: ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Explanation
The array is passed to the function, which modifies its elements.
Therefore, the changes are visible in main() as well.
Expected Output
Enter number of elements: 5
Enter 5 elements: 10 -5 20 -8 15
Modified array: 10 0 20 0 15
Passing Arrays to Functions — Exercise 9
Problem
Write a C program to pass an array to a function and sort its elements
in ascending order using Bubble Sort.
Program
#include <stdio.h>
void sortArray(int arr[], int n)
{
int i, j, temp;
// Perform Bubble Sort
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// Swap adjacent elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Pass array to sorting function
sortArray(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Explanation
The sorting function directly rearranges the elements of the passed
array. Bubble Sort compares adjacent elements and swaps them when
they are in the wrong order.
Expected Output
Enter number of elements: 5
Enter 5 elements: 40 10 30 5 25
Sorted array: 5 10 25 30 40
Passing Arrays to Functions — Exercise 10
Problem
Write a C program to pass an array of student marks to a function
and calculate the average, highest and lowest marks.
Program
#include <stdio.h>
void analyzeMarks(
int marks[],
int n,
float *average,
int *highest,
int *lowest)
{
int i, sum = 0;
// Initialize highest and lowest
*highest = marks[0];
*lowest = marks[0];
// Process all marks
for (i = 0; i < n; i++)
{
sum = sum + marks[i];
if (marks[i] > *highest)
*highest = marks[i];
if (marks[i] < *lowest)
*lowest = marks[i];
}
*average = (float)sum / n;
}
int main()
{
int marks[100], n, i;
int highest, lowest;
float average;
printf("Enter number of students: ");
scanf("%d", &n);
printf("Enter marks: ");
for (i = 0; i < n; i++)
{
scanf("%d", &marks[i]);
}
// Pass array and addresses of result variables
analyzeMarks(
marks,
n,
&average,
&highest,
&lowest
);
printf("Average = %.2f\n", average);
printf("Highest = %d\n", highest);
printf("Lowest = %d\n", lowest);
return 0;
}
Explanation
This example combines array passing with pointer parameters. The
function processes the complete array and returns three results:
average, highest and lowest. This is a practical pattern for
processing student marks.
Expected Output
Enter number of students: 5
Enter marks: 12 45 3 67 22
Average = 29.80
Highest = 67
Lowest = 3