C Program To Find Size of An Array

Lets write c program to find number of elements present in an array, where the array size is not mentioned explicitly.

Related Read:
Sizeof Operator in C Programming Language

Example: Expected Output

int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23, 56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
We have 25 no of elements in array.

Video Tutorial: C Program To Find Number of Elements In An Array


[youtube https://www.youtube.com/watch?v=RMJzZXvr2mY]

YouTube Link: https://www.youtube.com/watch?v=RMJzZXvr2mY [Watch the Video In Full Screen.]

Source Code: C Program To Find Size of An Array

Method 1

#include<stdio.h>

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};

    int count = sizeof(a) / sizeof(int);

    printf("We have %d no of elements in array\n", count);

    return 0;
}

Output:
We have 25 no of elements in array.

We know that array variable name contains base address or the address of first array element. But sizeof(array_name) gives back the number of bytes occupied by the entire array. Now divide this value by the memory occupied by 1 element – this can be determined either by using sizeof() on the data type(as shown in above source code) or any of the element in the array(as shown in below source code).

Method 1

#include<stdio.h>

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};

    int count = sizeof(a) / sizeof(a[0]);

    printf("We have %d no of elements in array\n", count);

    return 0;
}

Output:
We have 25 no of elements in array.

We assume that the array will at least have 1 element, so we find the number of bytes occupied by it. This is the best approach because, this way we’ll not be compelled to change the data type passed to sizeof() operator whenever we need to check the number of elements present in a array of different data type.

Method 3: Using Macros

#include<stdio.h>

#define SIZE(a) ( sizeof(a) / sizeof(a[0]) )

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};

    printf("We have %d no of elements in array.\n", SIZE(a));

    return 0;
}

Output:
We have 25 no of elements in array.

Here we pass the array variable name to macro SIZE(). Again the macro expansion will be replacing the macro template before passing the source code to the compiler.

Related Read:
Macros With Arguments: C Program

Using this method has advantage too. We can calculate size of multiple array variables, of different data type: as shown in below example.

Method 4: Using Macros

#include<stdio.h>

#define SIZE(a) ( sizeof(a) / sizeof(a[0]) )

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
    char ch[] = {'i', 'P', 'h', 'o', 'n', 'e'};
    double dbl[] = {2.3, 4, 5.2, 6.3, 7.1, 3.4, 1.3};

    printf("Integer variable a[%d].\n", SIZE(a));
    printf("Character variable ch[%d].\n", SIZE(ch));
    printf("Double variable dbl[%d].\n", SIZE(dbl));

    return 0;
}

Output:
Integer variable a[25].
Character variable ch[6].
Double variable dbl[7].

Here we find the number of elements present in array variable a and ch using the same macro SIZE.

Where Is It useful?

Whenever we want to iterate through individual elements of the array and we’ve no idea whats the size of the array or whats the largest designator of that array. In such cases we can’t write condition inside loop.

Using above formula we could easily find the number of elements present in the array and make use of the result in our for loop condition:

#include<stdio.h>

#define SIZE(a) ( sizeof(a) / sizeof(a[0]) )

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
    int count = SIZE(a);

    for(i = 0; i < count; i++)
        printf("%d\n", a[i]);

    return 0;
}

This prints all the elements of the array.

Note: Do not write i < SIZE(a) condition directly inside for loop. This would create unnecessory overhead, as for each iteration SIZE(a) [which will be replaced by ( sizeof(a) / sizeof(a[0]) ) while preprocessing] has to calculate the size of the array. Instead calculate the size of array once and store it inside a variable and then use that variable in your for loop as shown in above source code.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Copy Elements of One Array To Another In Reverse Order

Lets write a c program to copy elements of one array to another array in reverse order. Hint: Make use of macros to assign size of the array. And both the arrays must have same size.

Related Read:
Basics of Arrays: C Program

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Copying elements from array a to b
In reverse Order

Original(a[5]) –> Copy(b[5])
5 –> 3
2 –> 4
6 –> 6
4 –> 2
3 –> 5
Copy array elements in reverse order

Video Tutorial: C Program To Copy Elements of One Array To Another In Reverse Order


[youtube https://www.youtube.com/watch?v=UsIzctxViDs]

YouTube Link: https://www.youtube.com/watch?v=UsIzctxViDs [Watch the Video In Full Screen.]

Source Code: C Program To Copy Elements of One Array To Another In Reverse Order

#include<stdio.h>

#define N 5

int main()
{
    int a[N], b[N], i, j;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("\n\nCopying elements from array a to b, in reverse order\n");
    for(i = N - 1, j = 0; i >= 0; i--, j++)
        b[j] = a[i];

    printf("\nOriginal(a[%d])  -->  Copy(b[%d])\n", N, N);
    for(i = 0; i < N; i++)
        printf("%4d\t\t-->%6d\n", a[i], b[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5

Copying elements from array a to b, in reverse order

Original(a[5]) –> Copy(b[5])
1 –> 5
2 –> 4
3 –> 3
4 –> 2
5 –> 1

Logic To Copy Elements of One Array To Another In Reverse Order

We ask the user to enter N integer numbers. N is macro which is used to define size of the array. We store the user entered numbers inside array variable a. We initialize the variable i to last index of array a, and we initialize the variable j to first index of array variable b. Now for each iteration of the for loop, we assign the value of a[i] to b[j]. For each iteration of for loop we decrement the value of i by 1 and increment the value of j by 1. For loop iterates until i value is greater than or equal to 0.

At the end we print / display the content of both original array(a[5]) and the array to which the elements are copied to(b[5]) in reverse order.

Explanation With Example

If int a[5] = {5, 2, 6, 4, 3};

    for(i = N - 1, j = 0; i >= 0; i--, j++)
        b[j] = a[i];
ija[i]b[j]
40a[4] = 3b[0] = 3
31a[3] = 4b[1] = 4
22a[2] = 6b[2] = 6
13a[1] = 2b[3] = 2
04a[4] = 5b[0] = 5

a[5] = {5, 2, 6, 4, 3};
b[5] = {3, 4, 6, 2, 5};
Copy array elements in reverse order
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Copy Elements of One Array To Another

Lets write a c program to copy all the elements of one array to another array of same size.

Related Read:
Basics of Arrays: C Program

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Copying elements of array a to b

Original(a[5]) –> Copy (b[5])
5 –> 5
2 –> 2
6 –> 6
4 –> 4
3 –> 3
Copy array elements from a to b

Video Tutorial: C Program To Copy Elements of One Array To Another


[youtube https://www.youtube.com/watch?v=ib_xqm4J1d0]

YouTube Link: https://www.youtube.com/watch?v=ib_xqm4J1d0 [Watch the Video In Full Screen.]

Source Code: C Program To Copy Elements of One Array To Another

#include<stdio.h>

#define N 5

int main()
{
    int a[N], b[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < 5; i++)
        scanf("%d", &a[i]);

    printf("\n\nCopying elements of array a to b\n");
    for(i = 0; i < N ; i++)
        b[i] = a[i];

    printf("\nOriginal(a[%d])  -->  Copy (b[%d])\n", N, N);
    for(i = 0; i < N; i++)
        printf("%4d\t\t-->%6d\n", a[i], b[i]);

    return 0;
}

Output:
Enter 5 integer numbers
5
4
3
2
1

Copying elements of array a to b

Original(a[5]) –> Copy (b[5])
5 –> 5
4 –> 4
3 –> 3
2 –> 2
1 –> 1

Logic To Copy Elements of One Array To Another

We ask the user to enter N integer numbers. N is macro which is used to define size of the array. We store the user entered numbers inside array variable a. Since both array variables a and b has same size we copy individual elements of array variable a to array variable b at same index position.

At the end we print / display the content of both original array(a[5]) and the array to which the elements are copied to(b[5]).

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Smallest Element In An Array

Lets write a C program to find smallest element in an array, without sorting the elements. And also print the position at which the smallest number is present in the array.

Related Read:
C Program To Find Biggest Element of An Array

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Biggest of 5 numbers is 2, at position 2.
array with size 5

Video Tutorial: C Program To Find Smallest Element In An Array


[youtube https://www.youtube.com/watch?v=0lnvlxKPD3A]

YouTube Link: https://www.youtube.com/watch?v=0lnvlxKPD3A [Watch the Video In Full Screen.]

Source Code: C Program To Find Smallest Element of An Array

Method 1

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, small, pos;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
6
1
3
2
Smallest Number: 1, at position 3.

Logic To Find Smallest Element In An Array

We ask the user to input N integer numbers. N being Macro, used to assign array size. In above source code N is assigned a value of 5. So user enters 5 integer numbers. While the user inputs numbers we check if it’s the first number input by the user. If its the first number/element, then we assign that first number/element entered by the user to variable small and assign 1 to variable pos, indicating that its the first element in the array.

Next, for each consecutive iteration of the for loop we check if the new value entered by the user is smaller than the value present in variable small. If it’s true, then we assign the new value entered by the user to variable small and also update the value of pos accordingly.

Once i < N condition is false, control exits for loop and we print the value present inside variable small and pos which will have smallest of N numbers or the smallest element of the array and position of that element in the array.

Explanation With Example

If int a[5] = {5, 2, 6, 4, 3};

    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }
indexa[index]smallpos = index + 1
0a[0] = 551
1a[1] = 222
2a[2] = 6
3a[3] = 4
4a[4] = 3
5

a[5] = {5, 2, 6, 4, 3}
small = 2;
pos = 2;

Source Code: C Program To Find Smallest Element of An Array

Method 2

 #include<stdio.h>

#define N 5

int main()
{
    int a[N], i, small, pos;

    printf("Enter %d integer numbers\n", N);

    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    small = a[0];
    pos   = 1;

    for(i = 1; i < N ; i++)
    {
        if(small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
2
6
4
3
Smallest Number: 2, at position 2.

Method 2: LOGIC

Here we accept N integer numbers from the user. Next we assign the first element of the array to variable small and 1 to pos. Now we use another for loop to loop through the array.

Inside for loop
Inside for loop we check if the value present inside variable small is greater or bigger than the value present at a[i]. If it’s true, then we assign the value of a[i] to small and value of (i+1) to variable pos.

At the end of for loop, variable small and pos will have smallest element of the array and the position at which this number is present in the array.

Note: Since we assign the value of first element of the array(which is present at location a[0]) to variable small, while comparing with other elements of the array, we start comparing from a[1] till a[N-1]. That’s the reason we’ve assigned initial value of i to 1 in the second for loop.

Since a[0] is assigned to variable small, there is no point in comparing big with a[0]. So we start comparison from a[1].

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find First and Second Biggest Element In An Array

Lets write a C program to find first and second biggest element/number in an array, without sorting it.

Related Read:
Find First and Second Biggest In An Array, Without Sorting It: C

Example: Expected Output

Enter 5 unique integer numbers
5
2
6
4
3
First Big: 6
Second Big: 5
array with size 5

Important Note:
This code only works for inputs which has unique integer numbers. If there are duplicate integer numbers which is biggest in the array, then it’ll show the same number for both first and second biggest element. We can fix it, but it’s out of scope of this problem statement. Just know the limitation of this code.

Ex: a[5] = {2, 4, 5, 3, 5};
In this case, both first big and second big will have value 5.

Video Tutorial: C Program To Find First and Second Biggest Element In An Array


[youtube https://www.youtube.com/watch?v=iBqz2ZmQHSE]

YouTube Link: https://www.youtube.com/watch?v=iBqz2ZmQHSE [Watch the Video In Full Screen.]

Source Code: C Program To Find First and Second Biggest Element In An Array

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, fbig, sbig;

    if(N < 3)
    {
        printf("Please have an array with at least 2 elements\n");
        return(0);
    }

    printf("Enter %d unique integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

    for(i = 2; i < N; i++)
    {
        if(fbig < a[i])
        {
            sbig = fbig;
            fbig = a[i];
        }
        else if(sbig < a[i])
        {
            sbig = a[i];
        }
    }

    printf("First Big: %d\nSecond Big: %d\n", fbig, sbig);

    return 0;
}

Output 1:
Enter 5 unique integer numbers
5
2
6
4
3
First Big: 6
Second Big: 5

Output 2:
Enter 5 unique integer numbers
2
4
5
7
9
First Big: 9
Second Big: 7

In above source code we’re making use of macros to assign array size.

Logic To Find First and Second Biggest In An Array

First we assign biggest of a[0] and a[1] to variable fbig and the second biggest to sbig.
1. Using if else

    if(a[0] > a[1])
    {
        fbig = a[0];
        sbig = a[1];
    }
    else
    {
        fbig = a[1];
        sbig = a[0];
    }

2. Using Ternary/Conditional Operators

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

In both the cases the logic is same. If a[0] is greater than a[1], then value present at a[0] will be assigned to fbig, and a[1] will be assigned to sbig. If a[0] is not greater than a[1], then a[1] will be assigned to fbig and a[0] will be assigned to sbig.

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

for loop
Since both a[0] and a[1] are already sorted out, the comparison must start from a[2]. So we initialize the loop counter variable i to 2 and loop through until i < N and for each iteration we increment the value of i by 1.

Inside for loop
If value of fbig is less than the fetched element of the array, then we transfer the value of fbig to sbig and assign the new array element value to fbig.

Else if, the fetched element of the array is less than fbig but greater than sbig, then we assign the value of the array element to sbig.

    for(i = 2; i < N; i++)
    {
        if(fbig < a[i])
        {
            sbig = fbig;
            fbig = a[i];
        }
        else if(sbig < a[i])
        {
            sbig = a[i];
        }
    }

Once control exits for loop, we print the values present in fbig and sbig.

Explanation With Example

If int a[6] = {2, 4, 5, 7, 9, 8};
Here a[0] = 2 and a[1] = 4;
After executing below code:

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

fbig will have a[1], which is 4.
sbig will have a[0], which is 2.

indexa[index]sbigfbig
2a[2] = 545
3a[3] = 757
4a[4] = 979
5a[5] = 88

At the end of for loop, fbig will have 9 and sbig will have 8.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert