Home > Backend Development > C++ > body text

How to exchange the values ​​of two arrays in C language without using temporary variables?

WBOY
Release: 2023-09-02 22:53:03
forward
1432 people have browsed it

How to exchange the values ​​of two arrays in C language without using temporary variables?

Exchange two arrays without using temporary variables. Here we will use arithmetic and bitwise operators instead of the third variable.

The logic of reading the first array is as follows:-

printf("enter first array ele:</p><p>");
for(i = 0; i < size; i++){
   scanf("%d", &first[i]);
}
Copy after login
Copy after login

The logic of reading the second array is as follows-

printf("enter first array ele:</p><p>");
for(i = 0; i < size; i++){
   scanf("%d", &first[i]);
}
Copy after login
Copy after login

Do not use the third variable exchange The logic of the two arrays is as follows −

for(i = 0; i < size; i++){
   first[i] = first[i] + sec[i];
   sec[i] = first[i] - sec[i];
   first[i] = first[i] - sec[i];
}
Copy after login

Program

The following is a C program to exchange two arrays without using temporary variables:

Online demonstration

#include<stdio.h>
int main(){
   int size, i, first[20], sec[20];
   printf("enter the size of array:");
   scanf("%d", &size);
   printf("enter first array ele:</p><p>");
   for(i = 0; i < size; i++){
      scanf("%d", &first[i]);
   }
   printf("enter second array ele:</p><p>");
   for(i = 0; i < size; i ++){
      scanf("%d", &sec[i]);
   }
   //Swapping two Arrays
   for(i = 0; i < size; i++){
      first[i] = first[i] + sec[i];
      sec[i] = first[i] - sec[i];
      first[i] = first[i] - sec[i];
   }
   printf("</p><p> first array after swapping %d elements</p><p>", size);
   for(i = 0; i < size; i ++){
      printf(" %d \t ",first[i]);
   }
   printf("sec array after Swapping %d elements</p><p>", size);
   for(i = 0; i < size; i ++){
      printf(" %d \t ",sec[i]);
   }
   return 0;
}
Copy after login

Output

When the above program is executed, it produces the following results −

enter the size of array:5
enter first array ele:
11 12 13 14 15
enter second array ele:
90 80 70 60 50
first array after swapping 5 elements
90 80 70 60 50
sec array after Swapping 5 elements
11 12 13 14 15
Copy after login

The above is the detailed content of How to exchange the values ​​of two arrays in C language without using temporary variables?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!