简体   繁体   中英

Do we have to create an even and an odd array?

How can we modify the following code (which initially asks the user for 10 numbers to be entered, get stored in an array, and printed on the screen) so that the even numbers are printed on the first line, and the odd on the second:

#include <stdlib.h>
#include <stdio.h>
int i,j;
int array_1[10];
int main() {
    for(i=0;i<10;i++) {
    printf("Enter a number: ");
        scanf("%d", &array_1[i]);
    }
    printf("The elements of the array are: ");
    for (j=0;j<10;j++) {
        printf("%d ", array_1[j]);
    }
    printf("\n");
    return 0;
}

No need to create a new array. You can just go through it first checking for even numbers, and then again for odd numbers. Also, there's no need to declare i and j before using them. You can just declare them and initialize them in the for loop:

#include <stdlib.h>
#include <stdio.h>
int array_1[10];
int main() {
    for(int i=0;i<10;i++) {
    printf("Enter a number: ");
        scanf("%d", &array_1[i]);
    }
    printf("The elements of the array are: ");
    
    // Print even numbers
    for (int j=0;j<10;j++) {
        if(array_1[j] % 2 == 0)
            printf("%d ", array_1[j]);
    }
    printf("\n");

    // Print odd numbers
    for (int j=0;j<10;j++) {
        if(array_1[j] % 2 != 0)
            printf("%d ", array_1[j]);
    }
    printf("\n");
    return 0;
}

Edit: As tadman suggested in the comment below, there's a better and cleaner way to do this kind of task. As you can see in the above example, I'm repeating 4 lines of code where only one character changes. This task could be abstracted into a function to reduce code repetition:

void printIfMod(int* arr, size_t array_size, int mod){
    for (int j=0;j<array_size;j++) {
        if(arr[j] % 2 != mod)
            continue;
        printf("%d ", arr[j]);
    }
    printf("\n");

Remember to add a function prototype before main if you place the function after main:

void printIfMod(int* arr, size_t array_size, int mod);
int main(){...}

Now, to print the numbers, call the method with modulo 0 to get even numbers, and 1 to get odd numbers:

// Print even numbers
void printIfMod(&array_1, 10, 0);

// Print odd numbers
void printIfMod(&array_1, 10, 1);

One last note, hard-coding array_size is not wise, and that goes for all arrays. I recommend using sizeof() to dynamically calculate the size of your array:

size_t size = sizeof(array_1) / sizeof(int);
// Print even numbers
void printIfMod(&array_1, size, 0);

// Print odd numbers
void printIfMod(&array_1, size, 1);

O(n) Solution:

you have to add odd numbers at the back of the array and add the even numbers at the front of the array and keep track of the indexes.


int array_1[10];

int main() {

    int even = 0, odd = 10;

    for (int i = 0; i < 10; i++) {
        printf("Enter a number: ");
        int inp;
        scanf("%d", &inp);
        if (inp % 2 == 0) {
            array_1[even++] = inp;
        } else {
            array_1[--odd] = inp;
        }
    }

    // even numbers
    for (int i = 0; i < even; i++) {
        printf("%i ", array_1[i]);
    }
    printf("\n");

    // odd numbers
    for (int i = 9; i >= odd; i--) {
        printf("%i ", array_1[i]);
    }
    printf("\n");

    return 0;
}

Since you asked, here is how I would do it. I suspect this may leave you with more questions than answers through.

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#define NUMBERS_SIZE 10

typedef bool (*number_validator)(int num);

bool isEven(int num)
{
    return (num & 1) == 0;
}

bool isOdd(int num)
{
    return (num & 1) != 0;
}

void print(const char *title, int *array, int array_size, number_validator isValid)
{
    printf("%s", title);
    bool first = true;
    for (int i = 0; i < array_size; ++i)
    {
        if (isValid(array[i]))
        {
            if (!first)
            {
                printf(", ");
            }
            printf("%d", array[i]);
            first = false;
        }
    }
    printf("\n");
}

int main()
{
    int numbers[NUMBERS_SIZE] = { 0 };

    for (int i = 0; i < NUMBERS_SIZE; i++)
    {
        printf("Enter a number: ");
        scanf("%d", &numbers[i]);
    }

    printf("\n");
    print("Even: ", numbers, NUMBERS_SIZE, isEven);
    print(" Odd: ", numbers, NUMBERS_SIZE, isOdd);

    return 0;
}

Demo on ideone

you can try this way.I have used binary AND(&) instead of MOD(%) as it is faster:

#include <stdlib.h>
#include <stdio.h>
int i,j;
int array_1[10];

int main()
{
for(i=0; i<10; i++)
{
  printf("Enter a number: ");
  scanf("%d", &array_1[i]);
}

printf("The Even elements of the array are: ");

for (j=0; j<10; j++)
{
    if((array_1[j]&1) == 0)
        printf("%d ", array_1[j]);
}

printf("\nThe Odd elements of the array are: ");

for (j=0; j<10; j++)
{
    if((array_1[j]&1) != 0)
        printf("%d ", array_1[j]);
}

printf("\n");
return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM