Function for arrays

A

axcytz

Hi all,

I have an array containing 10 elements in it. I want to swap all elements (2 swap) with each other after each swap I will calculate a function value based on the updated array. So, I will have 45 different function values (10choose 2). I will store these values in another array and will get the index of the smallest valued array.

This is what i want to do.

include<algortihm>

My function:


for(int i=0 ; i< 45; i++)
{
for(int j=0; j<10; j++)
{
for(int k=0; k<10; k++)
{
UpdatedArray = swap(MyArray[j], MyArray[k]);
FunctionValue = Data[k]*Value[UpdatedArray]; //here the part after equal sign is not important for now.

}
}
}

Any help is appreciated, thanks!
 
A

Alf P. Steinbach

I have an array containing 10 elements in it. I want to swap all elements
(2 swap) with each other after each swap I will calculate a function value
based on the updated array. So, I will have 45 different function values
(10 choose 2). I will store these values in another array and will get the
index of the smallest valued array.

OK so far.

This implies that you store information about which items were swapped.

This is what i want to do.

include<algortihm>

My function:


for(int i=0 ; i< 45; i++)
{
for(int j=0; j<10; j++)
{
for(int k=0; k<10; k++)
{
UpdatedArray = swap(MyArray[j], MyArray[k]);
FunctionValue = Data[k]*Value[UpdatedArray]; //here the part after equal sign is not important for now.

}
}
}

Any help is appreciated, thanks!


Well the code above does NOT do what you described. It performs 4500
swaps, not 45. It swaps each pair twice. And it doesn't store the
function results.

To gain some clarity,

1. Reduce the array size to, say, 4. Which you can handle manually.

2. Think about how to do those swaps, systematically, BY HAND.
It's the system that's important.
But it's also important to actually try it out, by hand.

3. When you have managed to do it by hand (e.g. using small paper
squares to represent the array items), then try to express it in C++.

At that point it's time to perhaps post a new question here, or if
everything's clear, just proceed directly to computing and storing
function results.


Cheers & hth.,

- Alf (hoping that nobody will spoil your learning experience by posting
code showing how clever they are (not!), & wishing good luck)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top