A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C program to swap elements of two integer arrays using user define function
Q:

C program to swap elements of two integer arrays using user define function

0

C program to swap elements of two integer arrays using user define function

Given two integer arrays and we have to swap their elements by creating our own function (User Define Function) using C program.

Note: Total number of elements of both arrays should be same.

Here is the function that we have used in the program,

void swapElements(int *arr1 , int *arr2 , int n)

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to swap elements of two integer arrays using user define function in C

/*  
* C program to swap elements of two integer arrays
*/

#include <stdio.h>

// funtion to swap the elements of the two arrays
void swapElements(int *arr1 , int *arr2 , int n)
{
    int i=0,temp=0;
    
    for(i=0 ; i<n ; i++)
    {
        temp    = arr1[i];
        arr1[i] = arr2[i];
        arr2[i] = temp;
    }
}

// main function
int main()
{
    int i=0;
    
    // define two 1d-arrays
    int array_1[6] = {0,1,2,3,4};
    int array_2[6] = {5,6,7,8,9};
    
    // Passing two arrays and the number of 
    // elements to Function
    swapElements(array_1,array_2,5);
	
    printf("\nThe arrays after swap are..\n");
	
    for(i=0 ; i<5 ; i++)
    {
        printf("\narra_1 [%d] : %d",i,array_1[i]);
    }
    
    printf("\n");
    
    for(i=0 ; i<5 ; i++)
    {
	printf("\narray_2 [%d] : %d",i,array_2[i]);
    }
	
    return 0;
}

Output

The arrays after swap are..

arra_1 [0] : 5
arra_1 [1] : 6
arra_1 [2] : 7
arra_1 [3] : 8
arra_1 [4] : 9

array_2 [0] : 0
array_2 [1] : 1
array_2 [2] : 2
array_2 [3] : 3
array_2 [4] : 4

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now