Dear All
It is the last part of this code that is not working. The function removNb(Length, inner); makes many returns to a structure variable called "pair" The first variable in "pair" is tested to see if its value is above zero. If it is the variable "pair" is written to an array of pair (the data type is called Pair that has 2 variables both of which are long long), via a pointer.
I know that only 2 returns qualify to be written to the array. Before being written to the array they are printed out (they are correct), then they are written to the array and a count of the array element position is incremented.
To confirm this I then printed out the content of the array of pair via 2 print statements. The expected output is:
15 = 21
21 = 15
But the actual output is:
15 = 21
21 = 734423328283964721
So I am stuck again. Any help appreciated
Code:
/*IsMyFriendCheatingV5.C */
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
long long first;
long long snd;
} Pair;
//
Pair removNb(long long length, int n) //
{ //printf("%d ", n );
//printf("%lld ", length ); //650
Pair fpair; fpair.first = 0; fpair.snd = 0;
long long value = ( length * (length+1) ) / 2 ;
//printf("%lld ", value );// 351
static int MultiplyBy = 2;
//printf("%d - %d\n",MultiplyBy, n); // 2-26 , 1-26
if(MultiplyBy*n == value-(MultiplyBy+n) && length == n )
{
fpair.first = MultiplyBy; fpair.snd = n; //printf("(%lld-%lld) ", fpair ); // nothing
MultiplyBy++;
return fpair;
}
else if( MultiplyBy*n == value-(MultiplyBy+n) && length != n )
{ //printf("%d-%d", MultiplyBy, n );
//printf("%ld ", n );
fpair.first = MultiplyBy; fpair.snd = n; //printf("%lld-%lld ", fpair ); // 15-21 21-15
return fpair;
}
else if( MultiplyBy*n != value-(MultiplyBy+n) && length == n )
{
//fpair.first = 0; fpair.snd = 0; //printf("(%lld-%lld) ", fpair ); // nothing
MultiplyBy++;
return fpair;
}
}
//
int main()
{
Pair ArrayOfpair[] = {}; Pair pair;
long long Length = 26; int position = 0;
Pair *PtrOfPair = (Pair *)malloc(sizeof(Pair));
//
for(int outer = 2 ; outer < Length+1 ; outer++)// 2-26, 25 iterations
{ //printf("%d \n",outer); // 2-26
for(int inner = 1 ; inner < Length+1 ; inner++)// 1-26, 25 iterations
{
//printf("[%d ] ",inner);
//pair.first = 0; pair.snd = 0;
pair = removNb(Length, inner);
//printf("pair: [%lld - %lld]\n", pair);
if(pair.first > 0)
{
printf("[%lld - %lld]\n", pair);
PtrOfPair[position] = pair; printf("%d\n",position);
//printf("%lld=%lld", ArrayOfpair[position]);
position++;
}
}
}
printf("%lld = %lld\n", PtrOfPair[0]);
printf("%lld = %lld\n", PtrOfPair[1]);
return 0;
}