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

C Program To Find Biggest Element of An Array

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

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

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Biggest of 5 numbers is 6, at position 3

array with size 5

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


[youtube https://www.youtube.com/watch?v=Ram2KW-n0Qc]

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

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

Method 1

#include<stdio.h>

#define N 5

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

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

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

    printf("\nBiggest of %d numbers is %d, at position %d.\n", N, big, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
1
3
2
5
8

Biggest of 5 numbers is 8, at position 5.

Output 2:
Enter 5 integer numbers
10
2
5
9
3

Biggest of 5 numbers is 10, at position 1.

Logic To Find Biggest Element In An Array

We ask the user to input N integer numbers. N being a Macro. In above source code N is 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, then we assign that first number entered by the user to variable big 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 greater than the value present in variable big. If it’s true, then we assign the new value entered by the user to variable big 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 big and pos which will have biggest of N numbers or the biggest element of the array and position of that element in the array.

Explanation With Example

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

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

        if(i == 0 || big < a[i])
        {
            big = a[i];
            pos = i + 1;
        }
    }
indexa[index]bigpos = index + 1
0a[0] = 221
1a[1] = 442
2a[2] = 3
3a[3] = 4
4a[4] = 555
5

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

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

Method 2

#include<stdio.h>

#define N 5

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

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

    big = a[0];
    pos = 1;

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

    printf("\nBiggest of %d numbers is %d, at position %d.\n", N, big, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
1
5
6
3
0

Biggest of 5 numbers is 6, at position 3.

Output 2:
Enter 5 integer numbers
2
4
3
4
5

Biggest of 5 numbers is 5, at position 5

Method 2: LOGIC

Here we accept N integer numbers from the user. Next we assign the first element of the array to big and 1 to pos. Now we use another for loop to loop through the array. Inside for loop we check if the value present inside variable big is less than the value present at a[i]. If it’s true, then we assign the value of a[i] to big and value of (i+1) to variable pos.

At the end of for loop, variable big and pos will have biggest 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 big, 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 big, 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