answersLogoWhite

0

[]temp = array[1]

array[2]=array[1]

array[1]=[]temp

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

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


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


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


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.


What is the c program to sort the n elements in the array in ascending order?

The following example repeatedly asks for a number. As each number is entered, the array (arr) is dynamically resized to accommodate the new number. To finish entering numbers, enter any non-number (such as the letter X). If any numbers were entered, the array is then sorted using a simple bubble sort and the result is then displayed. Note that resizing an array like this is highly inefficient because the array must be copied each time it is resized. If you can determine how many numbers will be entered in advance (at runtime), then the array can be allocated just once, before reading the numbers into each array element. #include <iostream> int main() { float t, num = 0, *arr = NULL; int max = 0, i, j; while( 1 ) { printf( "\nEnter a number:\n(Any letter to quit)\n>" ); int iResult = scanf( "%f", &num ); if( !iResult ) break; arr = ( float * ) realloc( arr, ++max * sizeof( float )); arr[ max-1 ] = num; } printf( "%d numbers entered.\n", max ); if( max ) { for( i=1; i<max; i++ ) { for( j=0; j<max-i; j++ ) { if( arr[j] > arr[j+1] ) { t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t; } } } printf( "Sorted:\n"); for( i=0; i<max; i++ ) printf( "%f\n", arr[i] ); printf( "\n" ); free( arr ); arr = NULL; } return( 0 ); }

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


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.


Java program to swap values of 2 variables?

a=a+b; b=a-b; a=a-b;


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.


Write a java program to reverse the words in a sentence in place?

This is actually an interesting request. A "sentence" in Java would normally be stored in a String object. However, you also requested an "in place" sort, which is not possible to do with a String.Strings are an immutable type, which means that once its value is set a String can never change. If you try to change it, Java will actually be making an entirely new String in the background.Given all of the above, the next best thing we can do is look at a reversal of a character array.// This method will accept a String, convert it to a char[], reverse the char[], then convert// it back into a String to return.public static String reverseSentence(final String sentence) {// Get the character array of sentencefinal char[] sentenceChars = sentence.toCharArray();// Iterate through the first half of the array and swap each character with its// "mirrored" counterpart - first and last swap, second and second-to-last swap...final int halfLength = sentenceChars.length / 2;for (int i = 0; i < halfLength; ++i) {final char temp = sentenceChars[i];sentenceChars[i] = sentenceChars[sentenceChars.length - i - 1];sentenceChars[sentenceChars.length - i - 1] = temp;}// Use String.valueOf(char[]) to recreate a Stringreturn String.valueOf(sentenceChars);}


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 is Tapestry used in the Java programming language?

Tapestry is used in the Java programming language to monitor changes to Java pages or HTML templates and hot-swap them into the application being run without needing a reboot. It is an open source program.


What does transpose mean in math?

Transpose means swap places. In maths, the term is usually used for matrices. It means truning the matrix around so that its rows become columns and columns become rows.


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 Write a program to swap one demensional array?

void swap(int array1[],int array2[],int numberOfElements) { for(int i=0;i&lt;numberOfElements;i++){ int temp=array1[i];array1[i]=array2[i];array2[i]=temp;} }


Programme to swap two arrays in C programming?

#include &lt;iostream&gt; int main() { // Declare and initialise an array with two elements. int A[2]={1,2}; // Output the array values: std::cout&lt;&lt;"Before swap:\t"&lt;&lt;"A[0]="&lt;&lt;A[0]&lt;&lt;", A[1]="&lt;&lt;A[1]&lt;&lt;std::endl; // Swap the array values. A[0]^=A[1]^=A[0]^=A[1]; // Output the array values: std::cout&lt;&lt;"After swap:\t"&lt;&lt;"A[0]="&lt;&lt;A[0]&lt;&lt;", A[1]="&lt;&lt;A[1]&lt;&lt;std::endl; return(0); } Output: Before swap: A[0]=1, A[1]=2 After swap: A[0]=2, A[1]=1