1

I write a generalized C language program that given a sequence of numbers, sorts, even numbers in ascending order, odd numbers in descending order and places all even numbers in the initial part of an array then odd numbers.

Example: 2, 5, 1, 0, 4, 7, 9, 3, -2, 10, 20, 15
Expect: -2, 0, 2, 4, 10, 20, 15, 9, 7, 5, 3, 1

Six functions are required. The solution must be provided using only the mentioned functions.No global variables shall be declared. Use appropriate data types, return types and function arguments.

  1. Input() – takes total number of elements and values as input from the user. Stores the values in “input” array.
  2. SortEven() – sorts the even numbers in ascending order and stores them in an array named “even”
  3. SortOdd() – sorts the odd numbers in descending order and stores them in an array named “odd”
  4. Merge() – places all the even numbers in the initial part of array named “result” then odd numbers.
  5. Display() – displays the contents of “result” array.
  6. main() – calls the Input() module to begin the execution.

Program:

#include <stdio.h>

int main() {
    input();
}
int input() {
    int n;
    printf("Enter The Number Of Elements You Want To Enter : ");
    scanf("%d", &n);
    int a[n], i, ev = 0, od = 0;
    for (i = 0; i < n; i++) {
        printf("Enter Number : ");
        scanf("%d", &a[i]);
        if (a[i] % 2 == 0) {
            ev++;
        } else {
            od++;
        }
    }
    sorteven(a, ev, od, n);
}
int sorteven(int a[], int ev, int od, int n) {
    int i, j = 0, swap, even[ev];
    for (i = 0; i < n; i++) {
        if (a[i] % 2 == 0) {
            even[j] = a[i];
            j++;
        }
    }
    for (i = 0; i < ev - 1; i++) {
        for (j = 0; j < ev - i - 1; j++) {
            if (even[j] > even[j + 1]) {
                swap = even[j];
                even[j] = even[j + 1];
                even[j + 1] = swap;
            }
        }
    }
    sortodd(a, ev, od, n, even);
}
int sortodd(int a[], int ev, int od, int n, int even[]) {
    int i, k = 0, swap, odd[od], j;
    for (i = 0; i < n; i++) {
        if (a[i] % 2 != 0) {
            odd[k] = a[i];
            k++;
        }
    }
    for (i = 0; i < od - 1; i++) {
        for (j = 0; j < od - i - 1; j++) {
            if (odd[j] < odd[j + 1]) {
                swap = odd[j];
                odd[j] = odd[j + 1];
                odd[j + 1] = swap;
            }
        }
    }
    merge(a, ev, od, n, even, odd);
}
int merge(int a[], int ev, int od, int n, int even[], int odd[]) {
    int merge[n], i;
    for (i = 0; i < ev; i++) {
        merge[i] = even[i];
    }
    for (i = ev; i < n; i++) {
        merge[i] = odd[i];
    }
    display(merge, n);
}
int display(int merge[], int n) {
    int i;
    printf("OUTPUT : ");
    for (i = 0; i < n; i++) {
        printf(" %d ", merge[i]);
    }
}
5
  • Welcome to SO, you have written what Expected results, but what did you obtain ? Did you check intermediate results with a debugger ? Commented Dec 4, 2016 at 13:55
  • when i entered 1,2,3,4,5, the output i was getting was 2,4,1,0,49 Commented Dec 4, 2016 at 14:01
  • One thing that might help: declare prototypes for the functions before the main() function. For example, int input (void); and int sorteven(int a[], int evenCount, int oddCount, int totalCount);. Otherwise, your array will be passed as an int, and if sizeof(int) != sizeof(int *), the code will fail to work correctly. Commented Dec 4, 2016 at 14:13
  • None of your functions returns a value; they should all be declared to return void (except main(), which in my book should return 0). You should be getting warnings from your compiler about functions not returning values. If you aren't, you do not have enough warnings turned on. Similarly, using functions before you declare them should be generating compiler warnings. Commented Dec 4, 2016 at 14:43
  • You should add assertions and diagnostic printing. Your display function should add a newline at the end so you can tell when it has finished printing. Then, for example, you can call it in the sort even function to check that the values in the even array are all even. You should assert that the number of even values found matches the number you were told were in the array. After sorting, you should print the even array to check that it is valid (contains the correct even values in sorted order). Commented Dec 4, 2016 at 14:55

1 Answer 1

2

When looking in the source code, the problem is located in the merge() function. Both sorteven() and sortodd()are well implemented, but when merging both sub-arrays, the for(i=ev;i<n;i++) is not correct.

To add the odd[] array at the end of the even[] array, write:

int j;
// point to the first item of odd[]
for(i=ev,j=0;i<n;i++,j++)
{
    merge[i]=odd[j];
}

Instead of:

only odd[0] to odd[od-1] are allocated and defined.

for(i=ev;i<n;i++)
{
    merge[i]=odd[i];
}

Outputs of:

{ 2, 5, 1, 0, 4, 7, 9, 3, -2, 10, 20, 15 };

Are:

OUTPUT :  -2  0  2  4  10  20  15  9  7  5  3  1
Sign up to request clarification or add additional context in comments.

1 Comment

@SyedHussain, happy to help you... Now, if the answer complies, don't forget to accept or vote for it !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.