#include<stdio.h>
#define N 5
void biggest(int *num, int n, int big)
{
if(n < 0)
printf("Biggest element is %d\n", big);
else
{
if(*num > big)
big = *num;
biggest(++num, --n, big);
}
}
int main()
{
int a[N], i;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
biggest(a, N - 1, a[0]);
return 0;
}
Output: Enter 5 integer number 1 2 3 4 5 Biggest Element in the array: 5
Logic To Find Biggest Element of An Array using Recursion
We ask the user to enter N integer numbers and store it inside array variable a[N]. We pass base address(address of first element in the array) of the array, which is present in &a[0] or a, and last index of the array(indicating size of the array, from index 0), and first element of the array(assuming first element itself as big).
Inside Recursive function Base Condition: This is the condition to terminate the recursive call. Here we check if the size of the array is less than zero. If it’s less than zero, then we display the value present inside variable big, which holds the biggest element in the array.
void biggest(int *num, int n, int big)
{
if(n < 0)
printf("Biggest element is %d\n", big);
else
{
if(*num > big)
big = *num;
biggest(++num, --n, big);
}
}
We need to take a pointer variable to accept the base address. Next we’ll have base condition. If base condition isn’t met – we check if the value present in variable big is less than value present at *num. If it’s true, then we transfer the value of *num to big. Next we increment the address of num by 1 and decrement the value of n by 1 and pass them to the same function along with the value of big. This is called recursive function call.
Once value of n is less than 0, we display the value of variable big, which holds the biggest element of the array.
Important Note:
1. Array elements are always stored in contiguous memory location. 2. A pointer when incremented always points to an immediately next location of its own type.
Source Code: Find Biggest Element of An Array using Recursion: With Return Value
printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));
return 0;
}
#include<stdio.h>
#define N 5
int biggest(int *num, int n, int big)
{
if(n < 0)
return big;
else
{
if(big < *num)
big = *num;
return biggest(++num, --n, big);
}
}
int main()
{
int a[N], i;
printf("Enter %d integer number\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));
return 0;
}
Output: Enter 5 integer number 1 2 5 3 4 Biggest Element in the array: 5
After repeatedly incrementing value of num and decrementing the value n, we’ll reach a point where value of n will be less than 0. That’s when all the comparisons end, and variable big will have biggest element of the array. This result will be returned to the calling function, which in turn returns the result to the calling function and so on ..until the result is returned to the first function call, which was from with in main method – where we print the value of variable big.
Source Code: Using Array Variable In Recursive Function
printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));
return 0;
}
#include<stdio.h>
#define N 5
int biggest(int num[], int n, int big)
{
if(n < 0)
return big;
else
{
if(big < num[n])
big = num[n];
return biggest(num, --n, big);
}
}
int main()
{
int a[N], i;
printf("Enter %d integer number\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));
return 0;
}
Output: Enter 5 integer number 10 56 83 978 4 Biggest Element in the array: 978
Here we are taking array variable to receive the base address. We keep checking if a[n] is biggest than value present at variable big. If true, then we transfer a[n] value to variable big, and then recursively call the same function by decrementing the value of n by 1, and also pass the new value of big.
Once the value of n is less than 0, we return the value present in variable big, which holds the biggest element of the array.
Explanation With Example
N = 5; a[N] = {5, 2, 6, 4, 3}; n = N – 1 = 5 – 1 = 4; num = a[0] = 5; big = a[0] = 5;
Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd.
Important Note Most of the time 0 is considered as even number, as it has odd numbers on either side of it i.e., 1 and -1. And 0 is perfectly divisible by 2.
Also note that 0 is neither positive and nor negative.
In above source code, since 0 is perfectly divisible by 2, it is considered as even number.
Logic To Count Positive, Negative, Even And Odd Numbers In An Array
We ask the user to enter N integer numbers and store it inside array variable a[N]. Next we loop through the array using for loop. For each iteration we check: if the selected number is greater than 0. If true, then its positive number, so we increment the value of pos by 1. If the number is less than 0, then its a negative number, so we increment the value of neg by 1.
Next we check if the selected number is perfectly divisible by 2. If true, then its even number, so we increment the value of variable even by 1. If the selected number is not perfectly divisible by 2, then its odd number.
In today’s video tutorial lets learn more about arrays and pointers, and how we can use them with functions.
Note: This video tutorial is very important, so please make sure to watch the video till the end and make some notes. These are the basic concepts of arrays, pointers and functions. If you miss this, you’ll find other programs complicated. If you learn these basic concepts thoroughly all the programs related to using points and arrays with function will feel more straightforward. So please watch the video till the end and make note of important things I’m teaching in it. Happy Learning!
#include<stdio.h>
int main()
{
int num[5] = {1, 2, 3, 4, 5}, *ptr1, *ptr2;
ptr1 = &num[0];
ptr2 = num;
printf("Base Address using &num[0]: %d\n", ptr1);
printf("Base Address using num: %d\n", ptr2);
printf("\n");
printf("Value at num[0]: %d\n", *ptr1);
printf("Value at *num: %d\n", *ptr2);
printf("Value at *(num + 0): %d\n", *(ptr2 + 0));
printf("Value at index 1: %d\n", *(ptr2 + 1));
printf("Value at index 2: %d\n", *(ptr2 + 2));
printf("\n");
return 0;
}
Output: Base Address using &num[0]: 6356708 Base Address using num: 6356708
Value at num[0]: 1 Value at *num: 1 Value at *(num + 0): 1 Value at index 1: 2 Value at index 2: 3
Above program proves that both &num[0] and num(array variable name) hold base address of the array. And if you add 1 to base address or any address, it’ll point to the immediately next address of its own type. Since num is of type integer all the addresses will have 4 bytes gap between them – as each address is allocated with 4 bytes of data space.
Important Note
1. Array elements are always stored in contiguous memory location. 2. A pointer when incremented always points to an immediately next location of its own type. 3. Whenever compiler finds num[i] it’ll convert it into *(num + i). Because it’s faster to work with addresses than with variables.
#include<stdio.h>
int main()
{
int num[5] = {1, 2, 3, 4, 5}, i;
for(i = 0; i < 5; i++)
printf("%d\n", *(num + i));
printf("\n");
return 0;
}
Output: 1 2 3 4 5
If we append * infront of a pointer variable it’ll print the value present at that address or memory location.
Note: If we know the data type of the array and the base address of the array, we can fetch all the remaining addresses and values associated with those addresses.
Source Code: Using num[i] and [i]num Interchangeably
#include<stdio.h>
int main()
{
int num[5] = {1, 2, 3, 4, 5}, i;
for(i = 0; i < 5; i++)
printf("%d\n", i[num]);
printf("\n");
return 0;
}
Output: 1 2 3 4 5
We can access array elements by adding index value to the base address and appending * to it. i.e., *(num + i). We can write the same thing like this *(i + num) as well. Similarly, we can write num[i] as i[num] too and it outputs the same results.
Source Code: Print Array Elements Using Pointer Variable
#include<stdio.h>
int main()
{
int num[5] = {1, 2, 3, 4, 5}, i, *ptr;
ptr = num; // OR ptr = &num[0];
for(i = 0; i < 5; i++)
printf("%d\n", *ptr++);
printf("\n");
return 0;
}
Output: 1 2 3 4 5
Here we’ve assigned base address to pointer variable ptr. Inside for loop, we print the value present at the current address of ptr and then increment the address by 1 – doing which it points to the next address in the array.
Source Code: Print Array Elements In Reverse Order Using Pointer Variable
#include<stdio.h>
#define N 5
int main()
{
int num[N] = {1, 2, 3, 4, 5}, i, *ptr;
ptr = &num[N - 1]; // OR ptr = num;
for(i = 0; i < N; i++)
printf("%d\n", *ptr--);
printf("\n");
return 0;
}
Output: 5 4 3 2 1
Here we are assigning last index elements address to the pointer variable. Inside for loop we print the value present at address ptr and then decrement the value of ptr by 1 for each iteration of for loop – doing which it points to the previous address in the array.
Source Code: Arrays, Pointers & Functions: Call By Value
#include<stdio.h>
#define N 5
void display(int x)
{
printf("%d\n", x);
}
int main()
{
int num[N] = {1, 2, 3, 4, 5}, i;
for(i = 0; i < N; i++)
display(num[i]);
return 0;
}
Output: 1 2 3 4 5
Inside for loop we are passing value/element of array to display() method one by one for each iteration. And inside display() method we’re printing the values. This is called Call by value method.
Source Code: Arrays, Pointers & Functions: Call By Reference
#include<stdio.h>
#define N 5
void display(int *x)
{
printf("%d\n", *x);
}
int main()
{
int num[N] = {1, 2, 3, 4, 5}, i;
for(i = 0; i < N; i++)
display(&num[i]);
return 0;
}
Output: 1 2 3 4 5
Inside for loop we are passing address of each element of array to display() method one by one for each iteration. Since we are passing address to display method, we need to have a pointer variable to receive it. Inside display() method we display the value present at the address being passed.
Source Code: Arrays, Pointers & Functions: Printing address of array elements
#include<stdio.h>
#define N 5
void display(int *x)
{
printf("%d\n", x);
}
int main()
{
int num[N] = {1, 2, 3, 4, 5}, i;
for(i = 0; i < N; i++)
display(&num[i]);
return 0;
}
Output: 6356712 6356716 6356720 6356724 6356728
Inside display() method we’re accepting address using pointer variable, and then printing the addresses to the console window. If we want to print the elements present at the address, we need to append * in front of the pointer variable i.e., *x
Source Code: Add 1 to all the elements of array, using pointers and function
printf("\nArray elements after Oneplus operation!\n");
for(i = 0; i < N; i++)
printf("%d\n", num[i]);
return 0;
}
#include<stdio.h>
#define N 5
void oneplus(int *x)
{
*x = *x + 1;
}
int main()
{
int num[N] = {1, 2, 3, 4, 5}, i;
for(i = 0; i < N; i++)
oneplus(&num[i]);
printf("\nArray elements after Oneplus operation!\n");
for(i = 0; i < N; i++)
printf("%d\n", num[i]);
return 0;
}
Output: Array elements after Oneplus operation! 2 3 4 5 6
Here we are passing address of each element to oneplus() method and inside oneplus() method we’re adding 1 to the value present at the address. We’re printing modified array element values inside main() method.
Source Code: Square all the elements of array – use pointers and function
printf("\nArray elements after Oneplus operation!\n");
for(i = 0; i < N; i++)
printf("%d\n", num[i]);
return 0;
}
#include<stdio.h>
#define N 5
void oneplus(int *x)
{
*x = (*x) * (*x);
}
int main()
{
int num[N] = {1, 2, 3, 4, 5}, i;
for(i = 0; i < N; i++)
oneplus(&num[i]);
printf("\nArray elements after Oneplus operation!\n");
for(i = 0; i < N; i++)
printf("%d\n", num[i]);
return 0;
}
Output: Array elements after Oneplus operation! 1 4 9 16 25
Here we are passing address of each element to oneplus() method and inside oneplus() method we square the value present at the address. We’re printing modified array element values inside main() method.
printf("\nArray elements after oneplus5 operation!\n");
for(i = 0; i < N; i++)
printf("%d\n", num[i]);
return 0;
}
#include<stdio.h>
#define N 5
void oneplus5(int x[], int n)
{
int i;
for(i = 0 ; i < n; i++)
x[i] = x[i] + 5;
}
int main()
{
int num[N] = {1, 2, 3, 4, 5}, i;
oneplus5(num, N);
printf("\nArray elements after oneplus5 operation!\n");
for(i = 0; i < N; i++)
printf("%d\n", num[i]);
return 0;
}
Output: Array elements after oneplus5 operation! 6 7 8 9 10
Here we are passing base address and the size of array to oneplus5() method. Inside oneplus5() method, we are adding 5 to each element of the array. Once the execution of oneplus5() method completes, control comes back to main() method and here we print all the elements of the array. And the modifications made inside oneplus5() method reflects in main() method when we print the elements of array. Internally, x[i] will be converted into *(x + i) and hence the operation takes place at address level. So the manipulation reflects everywhere in the program.
void oneplus5(int x[], int n)
{
int i;
for(i = 0 ; i < n; i++)
*(x + i) = *(x + i) + 5;
}
Word of Caution!
Whenever you pass base address to a function and operate on the value present at that address, the array element/value gets altered. If you want to avoid it, make sure to pass a duplicate copy of the array to the function and not the base address of the original array.
printf("\nElements of array in reverse order ...\n");
for(i = 0; i < N; i++)
printf("%d\n", *ptr--);
return 0;
}
#include<stdio.h>
#define N 5
int main()
{
int a[N], i, *ptr;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
ptr = &a[N - 1];
printf("\nElements of array in reverse order ...\n");
for(i = 0; i < N; i++)
printf("%d\n", *ptr--);
return 0;
}
Output 1: Enter 5 integer numbers 1 2 3 4 5
Elements of array in reverse order … 5 4 3 2 1 Output 2: Enter 5 integer numbers 10 9 8 7 6
Elements of array in reverse order … 6 7 8 9 10
Logic To Display Elements of Array In Reverse Order using Pointers
We ask the user to input N integer numbers and store it inside array variable a[N]. We assign address of (N – 1)th element(last element of any array) to pointer variable ptr.
Inside for loop We iterate through the for loop and for each iteration we print the value present at the address ptr. And for each iteration we decrement the value of ptr by 1.
This prints the array elements in reverse order.
Important Things To Note
1. Array elements are always stored in contiguous memory location. 2. A pointer when incremented always points to an immediately next location of its own type.
Write a C program to find largest / maximum difference between two elements of an array, such that larger element or number appears after the smaller number in the array.
Note: I’m not considering time complexity in this video tutorial intentionally. We’ll have a completely dedicated video teaching about time complexity from basic. My intention in this video is to make the logic as simple and understandable as possible.
printf("The largest difference is %d, ", (big - small));
printf("and its between %d and %d.\n", big, small);
return 0;
}
#include<stdio.h>
#define N 6
int main()
{
int num[N], i, big, small;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &num[i]);
big = small = num[0];
for(i = 1; i < N; i++)
{
if(num[i] > big)
big = num[i];
if(num[i] < small)
small = num[i];
}
printf("The largest difference is %d, ", (big - small));
printf("and its between %d and %d.\n", big, small);
return 0;
}
Output: Enter 6 integer numbers 7 9 5 6 13 2 The largest difference is 11, and its between 13 and 2.
Here we find biggest element in the array and smallest element in the array. Next we subtract smallest element from biggest element to get the largest different between two array elements.
Find Largest Difference, where Largest Element Appears After Smallest Number in Array
printf("The largest difference is %d, ", (big - small));
printf("and its between %d and %d.\n", big, small);
return 0;
}
#include<stdio.h>
#define N 6
int main()
{
int num[N], i, big, small, pos = 0;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &num[i]);
big = small = num[0];
for(i = 1; i < N; i++)
{
if(num[i] > big)
{
big = num[i];
pos = i;
}
}
for(i = 1; i < pos; i++)
{
if(num[i] < small)
small = num[i];
}
printf("The largest difference is %d, ", (big - small));
printf("and its between %d and %d.\n", big, small);
return 0;
}
Output: Enter 6 integer numbers 7 9 5 6 13 2 The largest difference is 8, and its between 13 and 5.
Logic To Find Largest Difference b/w Two Elements of Array, where biggest number appears after the smallest number
As per the problem statement, the smallest number in the array must be chosen from index 0 to the position where the biggest number of the array is present.
For Example: Assume that we’ve an array {1, 2, 5, 3, 0}. Here biggest element in the array is 5 and its position in the array is 2. Now we need to select smallest number in the array between the index range 0 to 2. So the smallest number will be 1.
Step 1: First we iterate through the array using a for loop and find the biggest element in the array and we also determine the index position at which this biggest number is present.
Step 2: We write another for loop and iterate from index 1 to the position where the biggest number is present. And inside for loop we select the smallest element between the range.
Step 3: Now we subtract the smallest element from step 2 with the biggest element from step 1, and we get the largest difference between two elements of an array, where biggest number appears after the smaller number in the array.
Note: 1. We initialize variables big and small to the first element of the array, because we need to have some value to compare it with other elements of the array. 2. In both the for loops we initialize i to 1, as both variables big and small is assigned with value present at index 0. So there is no point in comparing with itself, so we start the comparison from index 1. That’s the reason we initialize i to 1 in both the for loops.