1

I have a triangle class consisting of a triangle's sides perimeter and many helper functions.

I have created a function that will populate an array of instances of triangle. Let us say I will create 3 objects so the triangles parameter will be set to 3.

void populate_triangle(triangle in_array[], int triangles) {
    for (int i = 0; i < triangles; i++) {
        in_array[i].input_random_sides();
        in_array[i].get_perimeter();
    }
}

I have also created a print function that will output all the information about said 3 objects, i.e. the three sides and perimeter.

void print_triangle(trianglein_array[], int triangles) {
    for (int i = 0; i < triangles; i++) {
        cout << "Triangle N: " << i << endl;
        in_array[i].print_information();
    }
}

I later call them in my main using

populate_triangle(in_array, 3);
print_triangle(in_array, 3);

I feel like this way is very inefficient and ugly.

Is there a better way to go through the filled-in array, other than the for (int i = 0; i < triangles; i++) loop? I know I can fuse this into one function, but I want to know how to do it with two.

9
  • 1
    If you used std::array or std::vector you could have used a range based for loop. The choice between the two here are if the number of items is fixed or not. std::vector if the number can change. Commented Dec 4, 2020 at 19:07
  • Why aren't you using std::array? Why are you using int instead of size_t? And why are you writing C++ without using the STL and its Iterators functionality? Commented Dec 4, 2020 at 19:07
  • 3
    If you use arrays and pointers (as you are currently) then there's no better. I'm not sure why you think it is very inefficient and ugly. What were you hoping for instead? Can you explain what your concern is? Maybe someone could set your mind at rest. Commented Dec 4, 2020 at 19:14
  • Because I had this pythonic thought that I could go “for each in array”. Commented Dec 4, 2020 at 19:16
  • It's C++ though, not Python. It's usually not going to be pythonic.. Commented Dec 4, 2020 at 19:19

1 Answer 1

2

As mentioned in the comments, you can use a std::vector.

std::vector keeps track of its own length so when you write your methods, there's no need to include the length as an extra argument, it's automatically deduced in the range for loop.

With a std::vector your method might look something like this:

void populate_triangle(std::vector<Triangle> triangles) {
    for (auto triangle : triangles) {
        triangle.input_random_sides();
        triangle.get_perimeter();
    }
}

std::vector's data array is guaranteed to be stored in contiguous memory.

Sign up to request clarification or add additional context in comments.

Comments

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.