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;

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

array... A, B, 5, 0
array... B, C, 4, 0
array... C, D, 7, 0
array... B, A, 3, 0
array... C, B, 2, 0
array... 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 and B, A, 3,0  in array... then I'd
like to put the n1 value of array in n2 value of array so that
array contains A, B, 5, 3...
- If there are B, C, 4, 0 in array and ,C B, 2,0  in array... then I'd
like to put the n1 value of array in n2 value of array so that
array 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;

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

>array... A, B, 5, 0
>array... B, C, 4, 0
>array... C, D, 7, 0
>array... B, A, 3, 0
>array... C, B, 2, 0
>array... 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 and B, A, 3,0  in array... then I'd
>like to put the n1 value of array in n2 value of array so that
>array contains A, B, 5, 3...
>- If there are B, C, 4, 0 in array and ,C B, 2,0  in array... then I'd
>like to put the n1 value of array in n2 value of array so that
>array 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

 Page 1 of 1 [ 3 post ]

Relevant Pages