We help IT Professionals succeed at work.
Get Started

How do I make an array name variable?

dx100 asked
Last Modified: 2010-04-01

I have this bubblesort loop

      // bubblesort algorithm to find min & max values of array
      for (int pass=1; pass<velg1_new_size; pass++)
                  // This next loop becomes shorter and shorter
                  for (int x=0; x < velg1_new_size-pass; x++)
                        if (velg1_new[x] > velg1_new[x+1])
                              // exchange elements
                              double temp_store = velg1_new[x]; velg1_new[x] = velg1_new[x+1]; velg1_new[x+1] = temp_store;
This loop goes through my array velg1_new. Unfortunately I have 6 arrays, velg1_new, velg2_new, accl1_new, accl2_new, dist1_new, dist2_new. Copying this algorithm 6 times do go through the 6 different arrays is obviously not an elegant or efficient way of doing this, my question is - how do I make the array names variable?

I was hoping I could do something like [arrayName][x] and then create and array of array names and just incriment arrayName. I was also thinking I could do something like a function call, and then just call the function so - bubbleSort(char x); and then replace [arrayName] with char? I'm losing myself now, I hope I'm making sense.


I am new to C++ so some help here would be fantastic.

Watch Question
Top Expert 2012
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE