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;}
}
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]);
swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }
[]temp = array[1] array[2]=array[1] array[1]=[]temp
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.
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }
void funksoin (int *vlau1, int *vula2);
/* 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);
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 < n; j++) { for (i = 0; i < n; i++) { if (array[i-1] > array[i]) { int tmp = array[i-1]; array[i-1] = array[i]; array[i] = tmp;
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];
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.
#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.