Velocity Reviews > C++ > Function for arrays

# Function for arrays

axcytz@gmail.com
Guest
Posts: n/a

 10-22-2013
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[i] = swap(MyArray[j], MyArray[k]);
FunctionValue[i] = Data[k]*Value[UpdatedArray]; //here the part after equal sign is not important for now.

}
}
}

Any help is appreciated, thanks!

Alf P. Steinbach
Guest
Posts: n/a

 10-22-2013
On 22.10.2013 04:45, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> 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[i] = swap(MyArray[j], MyArray[k]);
> FunctionValue[i] = 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)