question on arrays 
Author Message
 question on arrays

I have a question concerning arrays

Let's pretend I have a structure that looks like this:

typedef struct structure
{
    char letter1
    char letter2
    int n1
    int n2

Quote:
}structure;

structure array[6];

The following elements of the structure (structure) in the array (array[6])
are repectively letter1, letter2, n1, n2

array[0]... A, B, 5, 0
array[1]... B, C, 4, 0
array[2]... C, D, 7, 0
array[3]... B, A, 3, 0
array[4]... C, B, 2, 0
array[5]... D, C, 7, 0

What I'd like to do is as follow:

Go through the array... for example:
- If there are A, B, 5, 0 in array[0] and B, A, 3,0  in array[3]... then I'd
like to put the n1 value of array[3] in n2 value of array[0] so that
array[0] contains A, B, 5, 3...
- If there are B, C, 4, 0 in array[1] and ,C B, 2,0  in array[4]... then I'd
like to put the n1 value of array[4] in n2 value of array[1] so that
array[1] contains B, C, 4, 2... and so on...

Basically I'd like to check if 2 values of letter1 and letter2 are opposites
in 2 places of the array, then I add the n1 value of the second value in the
n2 of the first one... you got it :)

I'm using C/C++

Many thanks

Nico
--



Wed, 20 Nov 2002 03:00:00 GMT  
 question on arrays


Quote:
>I have a question concerning arrays

>Let's pretend I have a structure that looks like this:

>typedef struct structure
>{
>    char letter1
>    char letter2
>    int n1
>    int n2
>}structure;

>structure array[6];

>The following elements of the structure (structure) in the array (array[6])
>are repectively letter1, letter2, n1, n2

>array[0]... A, B, 5, 0
>array[1]... B, C, 4, 0
>array[2]... C, D, 7, 0
>array[3]... B, A, 3, 0
>array[4]... C, B, 2, 0
>array[5]... D, C, 7, 0

>What I'd like to do is as follow:

>Go through the array... for example:
>- If there are A, B, 5, 0 in array[0] and B, A, 3,0  in array[3]... then I'd
>like to put the n1 value of array[3] in n2 value of array[0] so that
>array[0] contains A, B, 5, 3...
>- If there are B, C, 4, 0 in array[1] and ,C B, 2,0  in array[4]... then I'd
>like to put the n1 value of array[4] in n2 value of array[1] so that
>array[1] contains B, C, 4, 2... and so on...

>Basically I'd like to check if 2 values of letter1 and letter2 are opposites
>in 2 places of the array, then I add the n1 value of the second value in the
>n2 of the first one... you got it :)

>I'm using C/C++

Is that C or C++? The following is in C.

Assuming that once you have looked at, and possibly modified, the struct
at index zero then you do not look at it again. Also your two
descriptions differ: the first state compare array[n] with array[n+3]
while the second states compare array[n] with array[n+1]...array[max-1].
Since the latter is a more general version.

#define MAX 6 /* Or whatever */

int i, j;

for( i = 1; i < MAX-1; ++i)
{
    for(j = i + 1; j < MAX; ++j)
    {
        if(array[i].letter1 == array[j].letter2 &&
           array[i].letter2 == array[j].letter1)
        {
            array[i].n2 = array[j].n1;
            break;
        }
    }

Quote:
}

The version with an offset of three would be:

#define MAX    6 /* Or whatever */
#define OFFSET 3
int i;

for(i = 0; i < MAX - OFFSET; ++i)
{
    if(array[i].letter1 == array[i + OFFSET].letter2 &&
       array[i].letter2 == array[i + OFFSET].letter1)
    {
        array[i].n2 = array[i + OFFSET].n1;
    }  

Quote:
}

HTH

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bob Wightman
--



Thu, 21 Nov 2002 03:00:00 GMT  
 question on arrays


Quote:
>I have a question concerning arrays

>Let's pretend I have a structure that looks like this:

Now why would you want to do this?  Now, of course, it might be
homework.  We will help debug homework code but we do not write it
(unless we feel very mean and want to sabotage your development as a
programmer)

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Thu, 21 Nov 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Question: extern *array and array[]

2. Question about array initializers, const

3. Question about arrays

4. a question on array!

5. Stupid question about array boundries

6. Question about array

7. question about array of pointers to chars

8. question about array?

9. Newbie question about arrays

10. Newbie question: Making arrays bigger

11. A question about arrays,pointers and structures

12. Question about arrays and pointers

 

 
Powered by phpBB® Forum Software