answersLogoWhite

0


Best Answer

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];

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you swap two adjecent no in array in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How to write a C program to generate Fibonacci series up to 10 elements and store in array?

#include <iostream.h> #include <conio.h> #include <stdlib.h> int swap(int* , int*); int main() { int c=0; int b=1; int max; cout<<"Enter the maximum limit of fibbonacci series = "; cin>>max; if(max>0) { cout<<c<<endl; } else { exit (EXIT_FAILURE); } for(int i=0;i<max;i++) { c=b+c; swap(&b,&c); if(c<max) { cout<<c<<endl; } else { break; } } getch(); return 0; } int swap(int *x,int *y) { int z; z=*x; *x=*y; *y=z; return z; }


How do you initialise a two dimentional array?

C provides rectangular multidimensional arrays. In C, a two-dimensional array is really a one-dimensional array, each of whose elements is an array. An array is initialized by a list of initializations in braces; each row of a two-dimensional array is initialized by a corresponding sub-list. Example of two dimensional array initialization: char array_example[2][4] = { {11, 12, 13, 14}, {21, 22, 23, 24} };


Columns and rows in c programming?

Do you perhaps mean -- a two-dimensional array? A two dimensional array is nothing more than a one-dimensional array where every element is a one-dimensional array. int matrix[4][5]; C is a row-major language thus the first dimension refers to the number of rows. Here we have declared an array of 4 rows, where each row is an array of 5 elements of type int.


What is the lowest subscript of an array in c plus plus?

The lowest subscript of an array in C, or C++ is 0.


Programme to swap two arrays in C programming?

#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

Related questions

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 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 < 4; ++i) { if (array[i]<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]);


What is the array of string in c?

A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension array.


How to write a C program to generate Fibonacci series up to 10 elements and store in array?

#include <iostream.h> #include <conio.h> #include <stdlib.h> int swap(int* , int*); int main() { int c=0; int b=1; int max; cout<<"Enter the maximum limit of fibbonacci series = "; cin>>max; if(max>0) { cout<<c<<endl; } else { exit (EXIT_FAILURE); } for(int i=0;i<max;i++) { c=b+c; swap(&b,&c); if(c<max) { cout<<c<<endl; } else { break; } } getch(); return 0; } int swap(int *x,int *y) { int z; z=*x; *x=*y; *y=z; return z; }


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 < size) fscanf(stdin, "%f", array + (i++)); } void spill(float* array, int size, char* delimiter) { int i = 0; while (i < 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 < size; i++) for (j = i; j < size; j++) if (array[i] > array[j]) swap(array + i, array + j); } void reverse(float* array, int size) { int i; for (i = size / 2; i >= 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.


How do you initialise a two dimentional array?

C provides rectangular multidimensional arrays. In C, a two-dimensional array is really a one-dimensional array, each of whose elements is an array. An array is initialized by a list of initializations in braces; each row of a two-dimensional array is initialized by a corresponding sub-list. Example of two dimensional array initialization: char array_example[2][4] = { {11, 12, 13, 14}, {21, 22, 23, 24} };


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 make a C plus plus program that arrange the the numbers in ascending order?

Heres something i whipped up in a hurry... This uses the Bubble Sort method found (related links) #include &lt;iostream&gt; using namespace std; int main(int argc, const char* argv) { int arraysize = 5; //Unsorted array size int array [] = { 5, 3, 4, 2, 1 }; //The array of numbers itself //Display the unsorted array cout &lt;&lt; "Before: {"; for (int c=0; c &lt;= arraysize; c++) { cout &lt;&lt; array[c]; if (c != arraysize) { cout &lt;&lt; ","; } } cout &lt;&lt; "}" &lt;&lt; endl; //Acctually sort the array int tmp=0; //Used for swaping values for (int loop=0; loop &lt;= (arraysize - 1); loop++) { for (int c=0; c &lt;= (arraysize - 1); c++) //The sort loop { if (array[c] &gt; array[c + 1]) { //Swaps the two values in the array tmp = array[c]; array[c] = array[c + 1]; array[c + 1] = tmp; //Cleanup tmp = 0; } } } //Display the sorted array cout &lt;&lt; "After: {"; for (int c=0; c &lt;= arraysize; c++) { cout &lt;&lt; array[c]; if (c != arraysize) { cout &lt;&lt; ","; } } cout &lt;&lt; "}" &lt;&lt; endl; return 0; }


How to swap two numbers by call by reference in c plus plus?

void swap(int&amp; a, int&amp; b ) { a^=b^=a^=b; }


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 you create table in c language?

The simplest way to create a table in C is to use a two-dimensional array.