answersLogoWhite

0

How do Write a program to swap one demensional array?

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

void swap(int array1[],int array2[],int numberOfElements)

{

for(int i=0;i<numberOfElements;i++){

int temp=array1[i];array1[i]=array2[i];array2[i]=temp;}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do Write a program to swap one demensional array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How check four number in c who is bigger?

In order to determine which of four number is bigger you need 16 if statements arranged in a nested if statement. That exceeds the complexity of just sorting it in an array. Here is a solution using a simple array sort. int array[] = {7, 2, 27, 4}; int i, swap; do { swap = 0; for (i = 0; i &lt; 4; ++i) { if (array[i]&lt;array[i+1]) { swap=array[i]; array[i]=array[i+1]; array[i+1]=swap; swap=1; } } } while (swap == 1); printf ("%d\n", array[0]);


Write a program to swap two numbers using function?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }


How do you swap rows in 2 dimensional array in Java?

[]temp = array[1] array[2]=array[1] array[1]=[]temp


Write a program to input a string in the form of name and swap the initials of the namefor example enter a string is rohit Kumar sharma and should be printed RK Sharma?

Get the string from user , then U Split the string with space and put in a string array .Then Find Length of string array , Take first letter of each element in array, Except last. Assigned those to a string, then Fetch Last element of an array, Add it With that String.That's it.


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


How would you write a program to swap the values of character variable using call by address?

void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }


When you write a program for swap two no what is functin declaration?

void funksoin (int *vlau1, int *vula2);


Bubble sort program in c using for loop?

/* ellipses (...) used to represent tabs for clarity */int array[N]; /* N must be contant literal in this example */bubblesort (int *a, int n) {... int swap = 1;... while (swap) {... ... int i;... ... int temp;... ... swap = 0;... ... for (i=0; i < n-2; i++) {... ... ... if (a[i] > a[i+1]) {... ... ... ... swap = 1;... ... ... ... temp = a[i];... ... ... ... a[i] = a[i+1];... ... ... ... a[i+1] = temp;... ... ... }... ... }... ... n--;... }}/* fill the array with some data */bubblesort (a, N);


How create swap program in c?

The best way to explain how to make a swap function in C is through an example: int x = 1; int y = 2; int tmp = x; New variable (temporary) is used to store the old x value. x = y; So now both x and y are equal to 2. y = tmp; y is now equal to 1. So after this function x is equal to 2 and y is equal to 1. To create a swap program you must apply this function in some way. One example being the bubble sort. for (j = 0; j &lt; n; j++) { for (i = 0; i &lt; n; i++) { if (array[i-1] &gt; array[i]) { int tmp = array[i-1]; array[i-1] = array[i]; array[i] = tmp;


How do you swap two adjecent no in array in c?

Option 1) Use a temporary variable: int x = array[i]; array[i] = array[i+1]; array[i+1] = x; Option 2) Use bit operators: array[i] ^= array[i+1] ^= array[i];


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 create a program that will let the user to enter 10 numbers and arrange it in ascending and descending?

#include "stdio.h" #define ARRAY_SIZE 10 void fill(float* array, int size); void spill(float* array, int size, char* delimiter); void bubble_sort(float* array, int size); void reverse(float* array, int size); void swap(float* a, float* b); int main(int argc, char* argv[]) { float numbers[ARRAY_SIZE]; fill(numbers, ARRAY_SIZE); bubble_sort(numbers, ARRAY_SIZE); spill(numbers, ARRAY_SIZE, " "); reverse(numbers, ARRAY_SIZE); spill(numbers, ARRAY_SIZE, " "); return 0; } void fill(float* array, int size) { int i = 0; while (i &lt; size) fscanf(stdin, "%f", array + (i++)); } void spill(float* array, int size, char* delimiter) { int i = 0; while (i &lt; size) fprintf(stdout, "%f%s", array[i++], delimiter); fputc('\n', stdout); } void bubble_sort(float* array, int size) { int i, j; for (i = 0; i &lt; size; i++) for (j = i; j &lt; size; j++) if (array[i] &gt; array[j]) swap(array + i, array + j); } void reverse(float* array, int size) { int i; for (i = size / 2; i &gt;= 0; i--) swap(array + i, array + (size - (i + 1))); } void swap(float* a, float* b) { float c = *a; *a = *b; *b = c; } fill gets the numbers from input spill sends them to output bubble sort will sort the array in ascending order reverse will reverse the list so that it is in descending order swap is used to swap two floats You can change float to double or int depending on which datatype you want to use.