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 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

Calculate Sum and Average of N Numbers using Arrays: C Program

Lets write a C program to calculate Sum and Average of N numbers using Arrays and using macros and for loop.

Related Read:
Calculate Sum and Average of N Numbers without using Arrays: C Program

Formula To Calculate Sum and Average

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

sum = 2 + 4 + 6 + 5 + 9;
average = sum / 5.0;

Result
sum = 26;
average = 5.2;

Important Note:
Look at the formula for calculating average. If you divide any number by integer number, it’ll only return integer value and discard the digits after decimal point. So make sure to divide the number by floating point value. To convert integer to float, make use of typecasting syntax.

Typecasting

int N = 5;

sum = 2 + 4 + 6 + 5 + 9;
average = sum / (float)N;

Since N is integer type variable, dividing any number by N would give us integer data. For some input it’ll result in wrong result. To fix it, we make use of typecasting and cast the type of N to float using above syntax.

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Sum of 5 numbers: 20

Average of 5 numbers: 4.000000

array with size 5

Video Tutorial: Calculate Sum and Average of N Numbers using Arrays: C Program


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

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

Source Code: Calculate Sum and Average of N Numbers using Arrays: C Program

Method 1

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, sum = 0;
    float avg;

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

    for(i = 0; i < N; i++)
    {
        sum = sum + a[i];
    }

    avg = sum / (float)N;

    printf("\nSum of %d numbers: %d\n", N, sum);
    printf("\nAverage of %d numbers: %f\n", N, avg);

    return 0;
}

Output 1:
Enter 5 integer numbers
2
5
6
8
10

Sum of 5 numbers: 31

Average of 5 numbers: 6.200000

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

Sum of 5 numbers: 15

Average of 5 numbers: 3.000000

Logic To Calculate Sum and Average of Array Elements

We ask the user to input N integer numbers. N is a macro and we’ve assigned 5 to it. Integer numbers input by the user is stored inside array variable a[N]. N being the size of the array.

SUM
We use for loop and iterate N times to fetch all the array elements. Variable i is assign a initial value of 0 and for loop iterates until i < N. Inside for loop we add the value of individual array element(a[i]) to previous value of variable sum. Once i < N condition is false, control exits for loop.

AVERAGE
Outside for loop we calculate average by using the formula:
average = sum / (float)N;

Once sum and average are calculated we output the result on to the console window.

Source Code: Calculate Sum and Average of N Numbers using Arrays: C Program

Method 2: Improved Source Code

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, sum = 0;
    float avg;

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

    avg = sum / (float)N;

    printf("\nSum of %d numbers: %d\n", N, sum);
    printf("\nAverage of %d numbers: %f\n", N, avg);

    return 0;
}

You could even calculate sum inside the for loop which is used to accept input from the user. Whenever user inputs a number it is immediately added to the previous value of variable sum.

Advantages of using above source code
We don’t iterate through the array to fetch individual elements of the array and then add it to calculate sum. We calculate sum inside first for loop itself which is used to accept input by user. This method is faster and cheaper on resource usage.

Important Notes:

1. Make use of macros to assign size of array.
2. Make sure to declare variable avg(to calculate average) as float or double.
3. Make sure to divide variable sum by floating point or double type value and not by integer. In order to convert a integer variable or macro, make use of typecasting.

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