C program to print two arrays using a separate function

Published by admin on

Introduction :

In this tutorial, we will learn how to take inputs for arrays from a user and print the arrays using a separate function.
The program will take the inputs of two arrays from the user first. The user will define the size of the arrays. Next, we will pass the arrays to a separate function by reference. This new function will print both arrays one by one.

This tutorial will teach you how to use a function in C and how to read inputs of an array using a loop. You will also learn how we can print an array using a for loop.

C program :

Before going in details how it works, let me show you the C program first :

#include <stdio.h>
//1
#define MAX_SIZE 100

//8
void printArrays(int *array1, int size1, int *array2, int size2)
{
    //9
    int i;
    printf("Array 1 : [ ");
    for (i = 0; i < size1; i++)
    {
        printf("%d ", array1[i]);
    }
    //10
    printf("]\n");

    //11
    printf("Array 2 : [ ");
    for (i = 0; i < size2; i++)
    {
        printf("%d ", array2[i]);
    }
    printf("]\n");
}


int main()
{
    //2
    int firstArray[MAX_SIZE];
    int secondArray[MAX_SIZE];

    //3
    int i;
    int size1, size2;

    //4
    printf("Enter the size of the first array : ");
    scanf("%d", &size1);

    //5
    printf("Enter the values for the first array : \n");
    for (i = 0; i < size1; i++)
    {
        printf("Enter value for index %d : ", i);
        scanf("%d", &firstArray[i]);
    }

    //6
    printf("Enter the size of the second array : ");
    scanf("%d", &size2);

    printf("Enter the values for the second array : \n");
    for (i = 0; i < size2; i++)
    {
        printf("Enter value for index %d : ", i);
        scanf("%d", &secondArray[i]);
    }

    //7
    printArrays(firstArray, size1, secondArray, size2);
}

Explanation :

The commented numbers in the above program denote the step numbers below :

1. MAX_SIZE is the maximum size of the array. We have defined this macro as 100,i.e. we are assuming that the maximum size of the array we will use in the program is 100.
2. First, create two integer arrays firstArray and secondArray. You can see that size of both arrays is MAX_SIZE or 100.
3. We have also defined a few other integer variables to use in the program. i is used in the for loop and size1,size2 is the size of the first and the second array.
4. Ask the user to enter the size of the first array. This value should be less than or equal to MAX_SIZE because we can’t take inputs exceeding this size. Read the user input value and store it in the size1 variable.
5. Now, using one for loop, ask the user to enter the values for the first array one by one. The for loop will run for size1 times and each time it will read the array element from the user input.
6. Similarly, ask the user to enter the size of the second array and store it in size2 variable. Also, take the inputs for the second array secondArray and store it as we have done for the first one.
7. Now, call one function to print the content of both arrays. This function is defined as printArrays. It takes both arrays and size of the arrays as an argument.
8. printArray is defined as void printArrays(int *array1, int size1, int *array2, int size2), that means this function is not returning anything (void) and it takes the address of the first and the second array and its sizes.
9. Inside the function, we have defined one integer variable i and using one for loop, print out all elements of the first array array1.
10 Print one ] character to indicate the ending of printing.
11. Similar to the first array, use one for loop to print the second array.

Sample Output :

Enter the size of the first array : 3
Enter the values for the first array :
Enter value for index 0 : 1
Enter value for index 1 : 2
Enter value for index 2 : 3
Enter the size of the second array : 5
Enter the values for the second array :
Enter value for index 0 : 1
Enter value for index 1 : 2
Enter value for index 2 : 3
Enter value for index 3 : 4
Enter value for index 4 : 5
Array 1 : [ 1 2 3 ]
Array 2 : [ 1 2 3 4 5 ]


Enter the size of the first array : 4
Enter the values for the first array :
Enter value for index 0 : 22
Enter value for index 1 : 203
Enter value for index 2 : 123
Enter value for index 3 : 455
Enter the size of the second array : 4
Enter the values for the second array :
Enter value for index 0 : 299
Enter value for index 1 : 989
Enter value for index 2 : 90
Enter value for index 3 : 49
Array 1 : [ 22 203 123 455 ]
Array 2 : [ 299 989 90 49 ]

Enter the size of the first array : 1
Enter the values for the first array :
Enter value for index 0 : 1
Enter the size of the second array : 1
Enter the values for the second array :
Enter value for index 0 : 2
Array 1 : [ 1 ]
Array 2 : [ 2 ]

Conclusion :

We have learned how to take user inputs for an array and how to print arrays using a different function. Using a separate function is best practice for any programming language. For example, if you are writing one program and you need to print out the array elements multiple times, then you can simply write one different function to handle the printing process. Try to divide a program into multiple functions and debugging will become a lot easier in future.

You might also like :

Categories: c

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *