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