Help with possible array?

Hi, I would like some help with this problem.

I have three intergers (say 14 16 7).
Question: How to write a program that will change 1, 2, or 3 of the values higher or lower, but the programe should be able to go back to the original values. The 3 intergers should be grouped together.

Then how to, using the above example, do the same for 10, 100, 1000, or more (groups of 3) different values.

P.S. I have a Borland Compiler v. 4.52

Comments

  • Hi Emf!

    I think you need to use structures! More accurately a structure vector. First of all create a structure data type:

    [code]
    struct my_three_ints
    {
    int first,second,third;
    };
    [/code]

    After that you can define [italic]struct my_three_ints[/italic] type variables:

    [code]
    int main()
    {
    int x=59;
    struct my_three_ints a,vect[4];

    a.first=14;
    a.second=16;
    a.third=7;

    vect[2].first=1;
    vect[2].second=a.first;
    vect[2].third=x;
    }
    [/code]

    In the above example we define two [italic]struct my_three_ints[/italic] variables: [italic]a[/italic] and [italic]vect[/italic]. [italic]a[/italic] is a simple structure [italic]vect[/italic] is a structure vector. As you can see you can access the a structure's [b]members[/b] with the '.' (member) operator.

    I think that the example is self explanatory enough to understand how to use the structures.

    There is also an alternative solution to your problem: use multi dimensional vectors (matrices).
    Assume that you need 100 values (groups of 3):

    [code]
    int main()
    {
    int v[100][3];

    // Fill the first group
    v[0][0]=14;
    v[0][1]=16;
    v[0][2]=7;

    // Fill the second group
    v[1][0]=1;
    v[1][1]=6;
    v[1][2]=9;

    // Fill the third group etc...
    v[2][0]=100;
    v[2][1]=200;
    v[2][2]=300;

    }
    [/code]
    Of course you should use a loop for filling the vector with values...

    I hope it will help you...

    Gabe
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion