Question

Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays...

Use C programming

Swap two arrays using pointers

Given two integer arrays, swap the two arrays using pointers.

Input:

    4

    1 2 3 4

    5

    4 5 6 7 8

    where:

  • First line represents the number of elements in the first array.
  • Second line represents the elements in the first array.
  • Third line represents the number of elements in the second array.
  • Fourth line represents the elements in the second array.

Output:

    4 5 6 7 8

    1 2 3 4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>

int main(void) {
        int s1, s2;
        int i;

        scanf("%d", &s1);
        int *arr1 = malloc(sizeof(int) * s1);
        for(i=0; i<s1; i++) {
                scanf("%d", &arr1[i]);                
        }

        scanf("%d", &s2);
        int *arr2 = malloc(sizeof(int) * s2);
        for(i=0; i<s2; i++) {
                scanf("%d", &arr2[i]);                
        }

        // now both arrays are read in arr1 and arr2.
        // now swap pointers.
        int *temp = arr1;
        arr1 = arr2;
        arr2 = temp;

        // print
        for(i=0; i<s2; i++) {
                printf("%d ", arr1[i]);                
        }
        printf("\n");
        for(i=0; i<s1; i++) {
                printf("%d ", arr2[i]);                
        }
        printf("\n");


        return 0;
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

  • please use c++ language 1. Request three different integers from the console. a) Write a swap...

    please use c++ language 1. Request three different integers from the console. a) Write a swap function that swaps two integer values. b) Write a sort function which accepts three integers as input then sorts them from largest to smallest using the swap function. c) Output the integers before and after sorting. Example 1 Output (input in bold italics) Enter three different integers: 3 2 4 3 2 4 4 3 2 Example 2 Output (input in bold italics) Enter...

  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • input: Task3: Given two int arrays, output true if the two arrays are identical and false...

    input: Task3: Given two int arrays, output true if the two arrays are identical and false if they are not. Input: task3 input Output: Array 1: 2 3 1 6 7 8 1 3 9 5 Array 2: 5 4 7 9 3 2 4 1 6 2 False 10 ܕ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ : : 7 s a

  • Write a C++ program that will test function described below that use pointers and dynamic memory...

    Write a C++ program that will test function described below that use pointers and dynamic memory allocation. Functions: You will write the function described below. Then you will call them from the main function, to demonstrate their correctness. concat_array: takes two int arrays and the arrays' sizes as arguments (that's 4 arguments). It should create a new array big enough to store both arrays. Then it should copy the contents of the first array to the new array, and then...

  • Can someone solve number 5 using Matlab? In Class Exercises 8-Arrays For the following exercises, assume...

    Can someone solve number 5 using Matlab? In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last index. In other words, you code for 1-4&6 should be able to handle the following 2 arrays: amp2 10 array1 [52, 63, 99, 71, 3.1] array2 - [99:-3: 45); For 5: wordArray1 wordArray2 ('number, 'arrays', 'indices', 'hello', finish' [number, 'different, 'finish? 1....

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last in...

    using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last index. In other words, you code for 1-4&6 should be able to handle the following 2 arrays: amp2 10 array1 [52, 63, 99, 71, 3.1] array2 - [99:-3: 45); For 5: wordArray1 wordArray2 ('number, 'arrays', 'indices', 'hello', finish' [number, 'different, 'finish? 1. Determine the sum of all...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding...

    Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding array elements, that array1 [0] + array2[0], etc. Save the sum into a third array called sumArray[]. Display the sum array. Submit your and the input and output of the complete execution.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT