Function for arrays

Discussion in 'C++' started by axcytz@gmail.com, Oct 22, 2013.

  1. Guest

    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!
     
    , Oct 22, 2013
    #1
    1. Advertising

  2. On 22.10.2013 04:45, 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 = 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)
     
    Alf P. Steinbach, Oct 22, 2013
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,893
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,339
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    589
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    403
  5. Philipp
    Replies:
    21
    Views:
    1,183
    Philipp
    Jan 20, 2009
Loading...

Share This Page