Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maths equation with an array of integers.

Status
Not open for further replies.

buntine

Programmer
Mar 1, 2004
1
AU
I have made a simple black jack card game which can have between 1 and 4 players. (not including the dealer).

Each member becomes an index in an array of structures. I have to determine whether 2 or more of the array members have the same value..

I know this seems logical at the moment. But the size of the array varies because there may be 1, 2, 3 or 4 players at once..

I considered using a loop to go through each player and find out what that precise players total was.

Just to explain why i cant just use an if statement:
Code:
if (pstPlayer[0].intTotal == pstPlayer[1].intTotal)
{
 printf("Total scores are equal.");
}
Occasionally, this will not work because player 3 may not exist.

Im new to this type of programming so my terminology may be a bit off.

Regards,
Andrew Buntine.
 
See that improvisation:
Code:
typedef struct _NumScore
{
  int num, score;
} NumScore;

typedef struct _Scores
{
  int n;
  NumScore ns[4];
} Scores;
/** Bubble sort */
void SortScores(Scores* ps)
{
  NumScore* pns = ps->ns, wns;
  int i, j, more = 1;
  for (i = ps->n-1; i > 0 && more; --i)
  {
    more = 0;
    for (j = 0; j < i; ++j)
      if (pns[j].score > pns[j+1].score)
      {
        wns = pns[j]; pns[j] = pns[j+1]; pns[j+1] = wns;
        more = 1;
      }
  }
}

/* Returns 0 if no equals at all */
int PrintEquals(Scores* ps)
{
  SortScores(ps);
  int i, k = 0, m = 0;
  for (i = 0; i < ps->n-1; ++i)
    if (ps->ns[i].score == ps->ns[i+1].score)
    {
      if (k == 0)
	printf("%d:\t",ps->ns[i].score); /* New group */
	++k;
	m = 1;
	printf(" %d",ps->ns[i].num);
    }
    else if (k)
    {
      printf(" %d\n",ps->ns[i].num);
      k = 0;
    }
  if (k) /* Last array member of equ group */
    printf(" %d\n",ps->ns[i].num);
  return m;
}

/* Test only; write your own Scores filler... */
static Scores s = {
  4,
  {{0, 100}, {1, 50}, {2,50}, {3, 100}}
};

int main(int argc, char* argv[])
{
  PrintEquals(&s);
  return 0;
}
It's not an optimal implementation (but it plays OK;).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top