Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to swap two arrays without using temporary variable in C

1 Answer

0 votes
#include <stdio.h>

int main(void) {
    int first[5] = {1, 2, 3, 4, 5} , second[] = {99, 88, 77, 66, 55};
    int size = sizeof(first)/sizeof(first[0]);
    
    for (int i = 0; i < size; i++) {
        first[i] = first[i] + second[i];
        second[i] = first[i] - second[i];
        first[i] = first[i] - second[i];
    }
   
    printf("First\n");
    for (int i = 0; i < size; i ++) {
        printf("%d ",first[i]);
    }
   
    printf("\nSecond\n");
    for (int i = 0; i < size; i ++) {
        printf("%d ",second[i]);
    }
   
    return 0;
}





/*
run:

First
99 88 77 66 55 
Second
1 2 3 4 5 

*/

 



answered Oct 8, 2021 by avibootz

Related questions

1 answer 171 views
1 answer 154 views
1 answer 171 views
1 answer 167 views
1 answer 156 views
6 answers 570 views
1 answer 136 views
...