answersLogoWhite

0


Best Answer

#include <iostream>

int main()

{

// Declare and initialise an array with two elements.

int A[2]={1,2};

// Output the array values:

std::cout<<"Before swap:\t"<<"A[0]="<<A[0]<<", A[1]="<<A[1]<<std::endl;

// Swap the array values.

A[0]^=A[1]^=A[0]^=A[1];

// Output the array values:

std::cout<<"After swap:\t"<<"A[0]="<<A[0]<<", A[1]="<<A[1]<<std::endl;

return(0);

}

Output:

Before swap: A[0]=1, A[1]=2

After swap: A[0]=2, A[1]=1

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

#include

using std::cin;
using std::cout;
using std::endl;

int main()
{
const int myArraySize = 5;
double myArray1[myArraySize] = {23, 54, 23, 76, 5};
cout << "Your first array is: " << endl;
for (int i(0); i < myArraySize; i++)
{
cout << myArray1[i] << endl;
}

double myArray2[myArraySize] = {67, 56, 3, 673, 345};
cout << "Your second array is: " << endl;
for (int i(0); i < myArraySize; i++)
{
cout << myArray2[i] << endl;
}

double swapArrays[myArraySize] = {0.0};
for (int i(0); i < myArraySize; i++)
{
swapArrays[i] = myArray1[i];
myArray1[i] = myArray2[i];
myArray2[i] = swapArrays[i];
}

cout << "Your first array after swapping is: " << endl;
for (int i(0); i < myArraySize; i++)
{
cout << myArray1[i] << endl;
}

cout << "Your second array after swapping is: " << endl;
for (int i(0); i < myArraySize; i++)
{
cout << myArray2[i] << endl;
}

system("PAUSE");
return 0;
}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Programme to swap two arrays in C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why are matrices used for representation while programming?

Let me correct you: two-dimensional arrays are used in programming to represent matrices. (Matrices are objects of mathematics, arrays are objects of programming.)


How do you swap two arrays in c plus plus using string?

You can't. While a string is a character array, an array is not necessarily a string. Treating arrays as if they were strings simply to swap them is madness. The correct way to physically swap arrays A and B is to copy A to a new array, C, then copy B to A, then C to B. If the arrays are the same size this is not a problem. If they are different sizes, you can only swap them if they are dynamic (not static). This means you must reallocate them. To speed up the process, copy the smallest array to C, first. A much better approach would be to point at the two arrays and swap the pointers instead.


How do you programme two keys simultaneusly on a Mazda 6?

If you know the steps to follow to programme your remote, all you have to do is at the time of programming 1st remote (by pressing any button) you also press any button on 2nd remote during the programming stage. Answers how to programme your remote can be found on this site.


Given two arrays A and B Array?

distinguish extra element in two arrays


How do you find the difference between two arrays in Java?

for arrays you can list the different arrays and what attributes that you give to them.


Name two smaller arrays you can use to find the product of 8x6?

name two smaller arrays you can use to find the product


How do you get difference between two or more arrays in PHP?

The inherit function `array_dif($arrayOne, $arrayTwo, $arrayThree, ...)` is likely what you're looking for. It compares two or more arrays, and returns an array of values that are unique among the arrays.


How do you get the mean median and mode in c programming?

By writing in C code the mathematical methods for finding the mean, median and mode of your data taking into account how your data is stored (eg an array; two separate arrays one with data and the other with frequencies; a two dimensional array containing both data and frequencies; an array of structures containing the data instead of arrays; a linked list of structures; etc).


How union of two arrays in java programming?

How to combine arrays in JavaGiven two arrays:int[] array1 = ...int[] array2 = ...It's possible to combine them into one like so:int[] arrayCombined = new int[array1.length + array2.length];// copy array1 to beginning of arrayCombinedSystem.arraycopy(array1, 0, arrayCombined, 0, array1.length);// copy array2 after array1System.arraycopy(array2, 0, arrayCombined, array1.length, array2.length);


Give the syntax to declare two dimensional array using array of pointers?

It is not possible to declare a two-dimensional array using an array of pointers in any programming language, but many programming languages support declarations of N-dimensional arrays of pointers.The exact syntax varies with the programming language, and requires support for N-dimensional arrays and pointers. In C, the following declares an array of pointer variables, each implemented as pointer to the generic type "void":void* array_1D[10];The type of the expression array_1D is "void * const."The following example expands on the previous one by declaring a two-dimensional array of "void" pointers:void* array_2D[10][20];The type of the expression array_2D is "void ** const."The last example declares a 3-dimensional array of "void" pointers, which can be seen as a 2-dimensional array of arrays of pointers:void* array_3D[10][20][30];


What do you call numbers that can be arranged into two rows arrays?

Even numbers.


What do you call the numbers that cannot be arranged into two row arrays?

crestwood