specbk
specbk

Reputation: 89

Finding the size of an array

The idea of the program is to input elements in an array. Then give the integer 'x' a value. If 'x' is 3 and the array a[] holds the elements {1,2,3,4,5,6}, we must "split" a[] into two other arrays. Lets say b[] and c[]. In b[] we must put all values lower or equal to 3 and in c[] all values greater than 3.

My question is- How can i express the 3 elements in b[i]?

#include <iostream>
using namespace std;

int main()
{
    int a[6];
    int b[6];
    int c[6];
    int d;


    for (int i = 0; i < 6; i++) {
        cin >> a[i];

    }
    cin >> d;

    for (int i = 0; i < 6; i++) {

        if (d >= a[i]) {
            b[i] = a[i]; // if d is 3, then i have 3 elements. How can i express them?
        }
    }

    for (int i = 0; i < 6; i++) {
        if (d< a[i]) {
            c[i] = a[i];
        }
    }

    for (int i = 0; i < 3; i++) {
        cout << b[i];
    }

    cout << endl;

    for (int i = 3; i < 6; i++) {
        cout << c[i];
    }


    return 0;
}

Upvotes: 0

Views: 94

Answers (4)

WhozCraig
WhozCraig

Reputation: 66194

I think all you're trying to do is have a way to determine how many int values you're copying from a[] to either b[] or c[]. To do that, introduce two more counters that start at zero and increment with each item copied to the associated array:

Something like this:

#include <iostream>

using namespace std;

int main()
{
    int a[6];
    int b[6], b_count=0; // see here
    int c[6], c_count=0; // see here
    int d;


    for (int i = 0; i < 6; i++) {
        cin >> a[i];

    }
    cin >> d;

    for (int i = 0; i < 6; i++) {

        if (d >= a[i]) {
            b[b_count++] = a[i]; // see here
        }
    }

    for (int i = 0; i < 6; i++) {
        if (d< a[i]) {
            c[c_count++] = a[i]; // see here
        }
    }

    for (int i = 0; i < b_count; i++) { // see here
        cout << b[i];
    }

    cout << endl;

    for (int i = 3; i < c_count; i++) { // and finally here
        cout << c[i];
    }


    return 0;
}

Now, if you want b[] or c[] to be dynamic in their space allocation, then dynamic-managed containers like st::vector<> would be useful, but I don't think that is required for this specific task. Your b[] and c[] are already large enough to hold all elements from a[] if needed.

Upvotes: 2

Captain Giraffe
Captain Giraffe

Reputation: 14705

WhozCraigs answer does a good job showing what you need to solve this using traditional arrays according to your tasks requirements.

I'd just like to show you how this can be done if you were allowed the full arsenal of the standard library. It is why people are calling for you to use std::vector. Things gets simpler that way.

#include <algorithm>
#include <iostream>

int main()
{
    int a[6] = {1, 2, 3, 4, 5, 6 }; // Not using input for brevity.
    int x = 3; // No input, for brevity

    // Lets use the std:: instead of primitives
    auto first_part = std::begin(a);
    auto last = std::end(a);

    auto comparison = [x](int e){ return e <= x; };
    auto second_part = std::partition(first_part, last, comparison);

    // Print the second part. 
    std::for_each(second_part, last, [](int e){ std::cout << e; });

    // The first part is first_part -> second_part
}

The partition function does exactly what your problem is asking you to solve, but it does it inside of the array a. The returned value is the first element in the second part.

Upvotes: 1

Richard Hodges
Richard Hodges

Reputation: 69882

Here's an example of how you'll approach it once you've a little more experience.

Anything you don't understand in here is worth studying here:

#include <iostream>
#include <vector>
#include <utility>

std::vector<int> get_inputs(std::istream& is)
{
    std::vector<int> result;
    int i;
    while(result.size() < 6 && is >> i) {
        result.push_back(i);
    }
    return result;
}

std::pair<std::vector<int>, std::vector<int>>
split_vector(const std::vector<int>& src, int target)
{
    auto it = std::find(src.begin(), src.end(), target);
    if (it != src.end()) {
        std::advance(it, 1);
    }
    return std::make_pair(std::vector<int>(src.begin(), it),
                          std::vector<int>(it, src.end()));
}

void print_vector(const std::vector<int>& vec)
{
    auto sep = " ";
    std::cout << "[";
    for (auto i : vec) {
        std::cout << sep << i;
        sep = ", ";
    }
    std::cout << " ]" << std::endl;
}

int main()
{
    auto initial_vector = get_inputs(std::cin);
    int pivot;
    if(std::cin >> pivot)
    {
        auto results = split_vector(initial_vector, pivot);
        print_vector(results.first);
        print_vector(results.second);
    }
    else
    {
        std::cerr << "not enough data";
        return 1;
    }


    return 0;
}

example input: 1 2 3 4 5 6 3

expected output:

[ 1, 2, 3 ]
[ 4, 5, 6 ]

Upvotes: 0

Exceptyon
Exceptyon

Reputation: 1582

use std::vectors. do not use int[]s.

with int[]s (that are pre-c++11) you could, with a few heavy assumptions, find array length with sizeof(X)/sizeof(X[0]); This has, however, never been a good practice.

in the example you provided, probably you wanted to:

    #define MAX_LEN 100
    ...
    int main() {
       int a[MAX_LEN];
       int b[MAX_LEN];
       int c[MAX_LEN];
       int n;
       std::cout << "how many elements do you want to read?" << std::endl;
       std::cin >> n;

and use n from there on (these are common practice in programming schools)

Consider a function that reads a vector of ints:

std::vector<int> readVector() {
    int n;
    std::cout << "how many elements do you want to read?" << std::endl;
    std::cin >> n;

    std::vector<int> ret;

    for (int i=0; i<n; i++) {
        std::cout << "please enter element " << (i+1) << std::endl;
        int el;
        std::cin >> el;
        ret.push_back(el);
    }

    return ret;
}

you could use, in main, auto a = readVector(); auto b = readVector(); a.size() would be the length, and would allow to keep any number of ints

Upvotes: 0

Related Questions