Is it possible to input numbers into two different arrays like so,

1 2 3 4 5 6 7 8 9

where the first array would hold {1, 3, 5, 7, 9}
and the second array would hold {2, 4, 6, 8}?

Dani AI

Generated

The example can mean two different things: split by position (take every other element) or split by numeric parity (odd-valued vs even-valued). The sample input (1 2 3 4 5 6 7 8 9) gives the same result either way, but many inputs do not — be explicit about which behavior you want. is right that this must be done in code, and 's .NET/LINQ idea is a clean choice for C#; below are simple C and C++ patterns for the same job.

Alternate-by-position (first number -> first array, second -> second array) in C++:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

int main() {
    std::string line;
    if (!std::getline(std::cin, line)) return 0;
    std::istringstream iss(line);

    std::vector<int> a, b;
    int x;
    bool toA = true; // toggle to send to a, then b, then a, ...

    while (iss >> x) {
        if (toA) a.push_back(x);
        else     b.push_back(x);
        toA = !toA;
    }

    for (int v : a) std::cout << v << ' ';
    std::cout << '\n';
    for (int v : b) std::cout << v << ' ';
    std::cout << '\n';
}

Simple C approach (parses one line, grows arrays with realloc):

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

int main(void) {
    char buf[4096];
    if (!fgets(buf, sizeof buf, stdin)) return 0;

    char *p = buf, *end;
    int *a = NULL, *b = NULL;
    size_t na = 0, nb = 0;
    int toA = 1;

    while (1) {
        long v = strtol(p, &end, 10);
        if (end == p) break;
        if (toA) {
            int *tmp = realloc(a, (na+1)*sizeof *a);
            if (!tmp) { free(a); free(b); return 1; }
            a = tmp; a[na++] = (int)v;
        } else {
            int *tmp = realloc(b, (nb+1)*sizeof *b);
            if (!tmp) { free(a); free(b); return 1; }
            b = tmp; b[nb++] = (int)v;
        }
        p = end;
        toA = !toA;
    }

    for (size_t i = 0; i < na; ++i) printf("%d ", a[i]);
    printf("\n");
    for (size_t i = 0; i < nb; ++i) printf("%d ", b[i]);
    printf("\n");

    free(a); free(b);
}

Notes and tips: if you need numeric parity instead of position, test the value (e.g., check odd/even) inside the loop. In C++ you can also use algorithms like std::stable_partition to split by value while preserving order. For large inputs prefer reserving capacity (C++ reserve) or doing a two-pass count before malloc in C to avoid many reallocs. Handle malformed tokens and negatives (strtol and stream extraction both give ways to validate).

Recommended Answers

All 4 Replies

Sure. But as you split that string up you would need to programattically do the splitting into the arrays.

You can unzip an array like that using the Where extension method (in .NET 3.5).

int[] evenPositioned = numbers.Where((x, i) => i % 2 == 0).ToArray();
int[] oddPositioned = numbers.Where((x, i) => i % 2 == 1).ToArray();

So input them into one array and then do the disentangling.

Interesting, I'm not familiar with the .Where method. Where or what object does that exist in?

In System.Linq.Enumerable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.