c - Swapping within an Array of user inputs -


i here advice on how continue program. homework assignment , idea have method called int is_sorted(int array[], int length); these pre , post conditions.

  1. precondition: array array of integers of length length.
  2. postcondition: returns true if array in sorted(nondecreasing) order, or false otherwise.

so far have been able put user input array , how long should be.

#include <stdio.h> #include <math.h>  int is_sorted(int array[], int lenght);  int is_sorted(int array[], int lenght) {     int swap;     int smallest;     int index = 0;     scanf("%d", &lenght);      int list[lenght];     int i;     (i = 0; < lenght; i++)     {         scanf("%d", &list[i]);     }      return 0; }  int main() {  } 

how go asking user input swap 2 elements @ time within given array?

the final product should similar this:

sample run: user input in bold

4 <- length array should be.

1 1 1 2 <- user input these 4 numbers.

what next swap? 2 3

evan has unsorted array.

what next swap? 2 0

what next swap? 0 3

evan has sorted array.

what next swap? -1 -1

steve right!

-1 -1 end swapping process , check if array sorted.

while(i != -1 && j != -1){     scanf("%d %d", &i, &j);     swap(&array[i], &array[j]); } 

and swap() looks this:

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

there's nifty trick swaping 2 variables doing bitwise xor-ing don't remember about.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -