C Program To Concatenate Two Arrays

Lets write a C program to concatenate or append two arrays into a third array. Make use of macros to assign size of the arrays.

Example: Expected Output

Enter 5 integer numbers, for first array
0
1
2
3
4
Enter 5 integer numbers, for second array
5
6
7
8
9

Merging a[5] and b[5] to form c[10] ..

Elements of c[10] is ..
0
1
2
3
4
5
6
7
8
9
Concatenation of arrays

Video Tutorial: C Program To Concatenate Two Arrays


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

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

Source Code: C Program To Concatenate Two Arrays

Method 1: Arrays with same size

#include<stdio.h>

#define N 5
#define M (N * 2)

int main()
{
    int a[N], b[N], c[M], i, index = 0;

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

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

    printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);
    for(i = 0; i < N; i++)
        c[index++] = a[i];

    for(i = 0; i < N; i++)
        c[index++] = b[i];

    printf("\nElements of c[%d] is ..\n", M);
    for(i = 0; i < M; i++)
        printf("%d\n", c[i]);

    return 0;
}

Output:
Enter 5 integer numbers, for first array
0
9
8
7
6
Enter 5 integer numbers, for second array
5
4
3
2
1

Merging a[5] and b[5] to form c[10] ..

Elements of c[10] is ..
0
9
8
7
6
5
4
3
2
1

Logic To Concatenate Two Array To Form Third Array

Here size of array a and b are same, so the for loops are same to accept elements of array a and b. We initialize variable index to 0. We write a for loop and iterate from 0 to 4, and copy the individual elements of array a to array c. Here index of array a and c varies from 0 to 4.

In the second for loop again we iterate the for loop from 0 to 4. This time value of variable index varies from 5 to 9, where as value of index of variable b varies from 0 to 4. So the individual elements of array b are copied to array c from index 5 to 9.

Method 2: Arrays with different size

#include<stdio.h>

#define N1 5
#define N2 6
#define M (N1 + N2)

int main()
{
    int a[N1], b[N2], c[M], i, index = 0;

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

    printf("Enter %d integer numbers, for second array\n", N2);
    for(i = 0; i < N2; i++)
            scanf("%d", &b[i]);

    printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N1, N2, M);
    for(i = 0; i < N1; i++)
        c[index++] = a[i];

    for(i = 0; i < N2; i++)
        c[index++] = b[i];

    printf("\nElements of c[%d] is ..\n", M);
    for(i = 0; i < M; i++)
        printf("%d\n", c[i]);

    return 0;
}

Output:
Enter 5 integer numbers, for first array
0
1
2
3
4
Enter 6 integer numbers, for second array
5
6
7
8
9
10

Merging a[5] and b[6] to form c[11] ..

Elements of c[11] is ..
0
1
2
3
4
5
6
7
8
9
10

Here the logic to concatenate array elements is same, but only difference is the size of array variable a and b. So we need to be careful while writing conditions in for loop.

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

Designated Initializers In Array: C Program

In this video tutorial lets learn about Designated Initializers in arrays.

C99 introduced the concept of designated initializers. These allow you to specify which elements of an array, structure or union are to be initialized by the values following.

We’ll look at using Designated Initializers for structures and unions in separate video tutorial, for now lets see how we can use it for arrays in C programming language.

Related Read:
Basics of Arrays: C Program

Where should we use Designated Initializers?

When you have a very large array and most of the elements of array are zeros and only couple of elements are non-zeros. In that case, you can use designators to initialize particular elements of an array to non-zero values.

Video Tutorial: Designated Initializers In Array: C Program


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

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

Source Code: Designated Initializers In Array: C Program

1. Display array Elements

#include<stdio.h>

int main()
{
    int a[5] = {0, 0, 1, 0, 8}, i;

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

    return 0;
}

Output:
0
0
1
0
8

We use simple for loop to loop through all the elements of an array and display it on to the console window.

2. Garbage values inside uninitialized array

#include<stdio.h>

int main()
{
    int a[5], i;

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

    return 0;
}

Output:
6356864
4200750
4200656
46
8

If array elements are not initialized it’ll have garbage values. Even if you initialize one element in that array, the rest of the elements will be automatically initialized to zero. And even if you simply have a equal sign and opening and closing curly braces in front of array variable, the compiler will assign zero to all the elements.

3. All zeros as array element

#include<stdio.h>

int main()
{
    int a[5] = {}, i;

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

    return 0;
}

Output:
0
0
0
0
0

Since we’ve opening and closing flower bracket or curly brackets in front of array declaration, compiler will automatically assign zeros as array elements.

4. Overwrite the values of array element

#include<stdio.h>

int main()
{
    int a[5], i;

    a[2] = 1;
    a[4] = 8;

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

    return 0;
}

Output:
6356864
4200766
1
46
8

From source code 2 above, we know that uninitialized arrays will have garbage values inside it. We are over-writing the garbage values at index 2 and index 4 with 1 and 8 respectively. So except for index 2 and 4 all other spots are filled with garbage values.

5. Overwrite the zeros in array

#include<stdio.h>

int main()
{
    // {0, 0, 1, 0, 8}
    int a[5] = {}, i;

    a[2] = 1;
    a[4] = 8;

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

    return 0;
}

Output:
0
0
1
0
8

So this fixes our issue. Since we’ve empty opening and closing curly braces after declaring array variable, all the elements of array will be automatically initialized to 0.

After that we overwrite the values at index 2 and 4 with 1 and 8.

Now lets make use of Designated Initializers

For arrays with large size we need to use Designated Initializers. Because we can’t individually assign values by overwriting it. It’ll take lot of space in your source code. To avoid that, and to write it in single line of code, we make use of Designated Initializers.

6. Designated Initializers in array

#include<stdio.h>

int main()
{
    // {0, 0, 1, 0, 8}
    int a[5] = {[2] = 1, [4] = 8}, i;

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

    return 0;
}

Output:
0
0
1
0
8

Here we initialized values of index 2 and 4 to 1 and 8 respectively in a single line of code. Here [2] and [4] are called Designators.

7. Designated Initializers: order of index appearance doesn’t matter

#include<stdio.h>

int main()
{
    // {0, 0, 1, 0, 8}
    int a[5] = {[4] = 8, [2] = 1}, i;

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

    return 0;
}

Output:
0
0
1
0
8

As you can see the designator [4] appears before the designator [2], but still it doesn’t change anything with the output.

8. Designated Initializers: Alternate Syntax

#include<stdio.h>

int main()
{
    // {0, 0, 1, 0, 8}
    int a[5] = {[2]1, [4]8}, i;

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

    return 0;
}

Output:
0
0
1
0
8

As you can see in above source code, we are not using equal sign(=) to assign value to designators. And it’s valid syntax.

9. Designated Initializers: Mixed Syntax

#include<stdio.h>

int main()
{
    // {0, 0, 1, 0, 8}
    int a[5] = {[2]1, [4] = 8}, i;

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

    return 0;
}

Output:
0
0
1
0
8

You can see that we’ve used both syntax to initialize value to particular index of the array. i.e., [2]1 and [4] = 8. Even if we mix both these syntax, it works and its perfectly valid syntax in C standard.

10. Designated Initializers: And dynamic array size

#include<stdio.h>

int main()
{
    // {0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 5}
    int a[] = {[2]1, [4] = 8, [10] = 5}, i;

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

    return 0;
}

Output:
0
0
1
0
8
0
0
0
0
0
5

If we don’t mention length or size of the array explicitly, then the compiler will assign the length or size of the array from the largest designator in the list.

Note: Since the largest designator is 10 in above source code, which must be N – 1 i.e, 11 – 1 = 10. So array size is 11. OR since index starts from 0, and the largest designator is 10. i.e., 0 to 10, which means 11 elements in an array.

11. Designated Initializers: Designators and normal initializers

#include<stdio.h>

int main()
{
    // {4, 6, 1, 0, 8}
    int a[5] = {4, 6, [2]1, [4] = 8}, i;

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

    return 0;
}

Output:
4
6
1
0
8

Here we’ve intialized index 0 to 1, index 1 to 6, and then we’re using designators to initialize index 2 to value 1 and index 4 to value 8.

12. Designated Initializers: initialize range of elements in an array

#include<stdio.h>


int main()
{
    // {0, 2, 2, 2, 3, 3, 3, 0, 0, 0}
    int a[10] = {[1 ... 3] = 2, [4 ... 6] = 3}, i;

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

    return 0;
}

Output:
0
2
2
2
3
3
3
0
0
0

Here we make use of designators to initialize range of elements. Syntax is [first … last] = value. i.e., the first index to start initializing and the last index to end the initialization. In between these two indexes there are 3 dots.

13. Designated Initializers: initialize range of elements in an array, with mixed syntax

#include<stdio.h>

int main()
{
    // { 0, 2, 2, 2, 3, 3, 3, 5, 0, 0}
    int a[10] = {[1 ... 3] = 2, [4 ... 6]3, 5}, i;

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

    return 0;
}

Output:
0
2
2
2
3
3
3
5
0
0

In above source code you can see mixed syntax to initialize elements of the array. i.e., [1 … 3] = 2 is valid, [4 … 6]3 is valid too, and index 7 is assigned a value of 5 directly. All other indexes will have a value of zero.

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 Print Elements of Array In Reverse Order

Lets write a c program to print or display the elements of an array in reverse order.

Related Read:
Basics of Arrays: C Program

Note: This is a very simple program but still a very important one, because we’ll be using some form of logic to print elements of an array. So better we know ins and outs of printing array elements in whichever order the program demands. So please pay attention to the logic.

Video Tutorial: C Program To Print Elements of Array In Reverse Order


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

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

Source Code: C Program To Print Elements of Array In Reverse Order

#include<stdio.h>

int main()
{
    int a[5], i;

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

    printf("Array elements are:\n");
    for(i = 4; i >= 0; i--)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
5
4
3
2
1

Since the array size is 5, the last index of the array will be (5-1) which is 4. So we initialize i to 4, and keep decrementing the value of i by 1 for each iteration of the for loop. Control exits for loop once i value is equal to 0. In arrays the index starts from 0. Inside for loop, for each iteration, we print the value of i.

#include<stdio.h>

#define N 5

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

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

    printf("Array elements are:\n");
    for(i = N-1; i >= 0; i--)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
5
4
3
2
1

Here we initialize value of i to the last index of the array, which is N-1. We iterate through the for loop until i value is 0(which is the first index of the array), for each iteration of the for loop we decrement the value of i by 1. Inside for loop we print the value of a[i].

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

Basics of Arrays: C Program

Lets look at basics of arrays in C programming language. We’ve already covered a lot of stuffs about arrays in Introduction To Arrays: C Programming Language. In this video tutorial we’ll look at some specific things about arrays which we use often.

Related Read:
For Loop In C Programming Language
Sizeof Operator in C Programming Language

Declaring Array Variable

Syntax:

Data_type variable_name[array_size];

Ex: int a[5];

Here array variable is a, it can hold upto 5 integer values.

Index of array starts from zero, and it ends at N-1. N being the size of the array.

For Ex:
int a[N];
Here the first element of array a is at a[0] and the last element is at a[N-1].

Definition of Array

An array is a collection of data items, of same data type, accessed using a common name.

Video Tutorial: Basics of Arrays: C Program


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

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

Source Code: Basics of Arrays: C Program

Printing all the elements of an array

#include<stdio.h>

int main()
{
    int a[5] = {1, 2, 3, 4, 5}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
1
2
3
4
5

This prints all the elements of an array. In this program we’re declaring and initializing array variable simultaneously.

Empty curly braces for Array Variable

#include<stdio.h>

int main()
{
    int a[5] = {}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
0
0
0
0
0

Compiler will insert zero in all the empty spots.

Un-initialized Array Variable

#include<stdio.h>

int main()
{
    int a[5], i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
6356864
4200750
4200656
46
8

If array variable is left un-initialized it’ll have garbage values inside it.

Array Variable Not fully initialized Manually

#include<stdio.h>

int main()
{
    int a[5] = {3, 2}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
3
2
0
0
0

Whenever we assign less values than the array size, the remaining elements will get assigned to 0.

Expression As Array Size

#include<stdio.h>

int main()
{
    int a[2+3] = {3, 2, 1, 0, 5}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
3
2
1
0
5

Any valid expression which ultimately resolves to a positive integer is valid inside square brackets.

Negative Integer Number As Array Size

#include<stdio.h>

int main()
{
    int a[-5] = {3, 2, 1, 0, 5}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
error: size of array ‘a’ is negative
warning: excess elements in array initializer

You can only have positive integer as size of an array and nothing else.

Trying To Assign More Values Than The Array Size

#include<stdio.h>

int main()
{
    int a[5] = {1, 2, 3, 4, 5, 6, 7}, i;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
warning: excess elements in array initializer

You can’t assign more values than the array size.

Overwrite values present at an index: Array

#include<stdio.h>

int main()
{
    int a[5] = {1, 2, 3, 4, 5}, i;

    a[3]  = 100;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
1
2
3
100
5

Here the previous value present at index 3(which can be access using a[3]), is overwritten by value 100.

Garbage Values In Empty Spots: Array

#include<stdio.h>

int main()
{
    int a[5], i;

    a[3]  = 100;

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Array elements are:
6356864
4200766
4200672
100
8

As you can see, the element at index 3 has 100, and all other elements value is just garbage values.

Array Size In Memory

#include<stdio.h>

int main()
{
    int    a[5];
    float  b[5];
    char   c[5];
    double d[5];

    printf("Int    Array: %d\n", 5 * sizeof(int));
    printf("Float  Array: %d\n", 5 * sizeof(float));
    printf("Char   Array: %d\n", 5 * sizeof(char));
    printf("Double Array: %d\n", 5 * sizeof(double));

    return 0;
}

Output:
Int Array: 20
Float Array: 20
Char Array: 5
Double Array: 40

Each cell in array occupies memory space depending upon its data type. Int and float occupies 4 bytes. Char occupies 1 byte. Double occupies 8 bytes. Also remember, size of data type is machine dependent.

Initializing and Accessing Array Elements

#include<stdio.h>

int main()
{
    int a[5], i;

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

    printf("Array elements are: \n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
1
2
3
4
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