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];
#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; }
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} };
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.
The lowest subscript of an array in C, or C++ is 0.
#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
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.
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]);
A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension 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; }
#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.
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} };
/* 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;
Heres something i whipped up in a hurry... This uses the Bubble Sort method found (related links) #include <iostream> 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 << "Before: {"; for (int c=0; c <= arraysize; c++) { cout << array[c]; if (c != arraysize) { cout << ","; } } cout << "}" << endl; //Acctually sort the array int tmp=0; //Used for swaping values for (int loop=0; loop <= (arraysize - 1); loop++) { for (int c=0; c <= (arraysize - 1); c++) //The sort loop { if (array[c] > 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 << "After: {"; for (int c=0; c <= arraysize; c++) { cout << array[c]; if (c != arraysize) { cout << ","; } } cout << "}" << endl; return 0; }
void swap(int& a, int& b ) { a^=b^=a^=b; }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
The simplest way to create a table in C is to use a two-dimensional array.