Code:
/* This is an update to scorecard.c
# Our Data for the Scores Array:
2 3 4 3 2 1 1 5 4
0 3 2 3 0 4 2 2 4
2 3 5 0 1 3 3 0 2
1 1 1 5 5 2 3 1 5
4 2 0 2 3 0 5 2 1
3 0 3 1 4 4 0 3 2
0 4 2 3 1 1 6 4 3
5 5 4 0 3 2 4 0 0
6 2 3 1 3 1 5 6 3
3 3 1 2 5 0 5 4 1
*/
#include <stdio.h>
#include <string.h>
#define Rows 10
#define Cols 15
#define NumberOfGames 11
void getPoints(int scores[Rows][NumberOfGames], int team, int home[Rows][NumberOfGames]);
void output(char teams[Rows][Cols], int scores[Rows][NumberOfGames],int home[Rows][NumberOfGames]);
int main(void) {
int c, r, i, goals1, goals2, away;
int scores[Rows][NumberOfGames] = {
{-1,2,3,4,3,2,1,1,5,4,0}, //team[0]'s game scores
{0,-1,3,2,3,0,4,2,2,4,0}, //team[1]'s game scores
{2,3,-1,5,0,1,3,3,0,2,0}, //last column is team's total score
{1,1,1,-1,5,5,2,3,1,5,0}, //away win= 3 pts.
{4,2,0,2,-1,3,0,5,2,1,0}, //home win= 2 pts.
{3,0,3,1,4,-1,4,0,3,2,0}, //tie= 1 pt.
{0,4,2,3,1,1,-1,6,4,3,0}, //loss= -1 pt.
{5,5,4,0,3,2,4,-1,0,0,0},
{6,2,3,1,3,1,5,6,-1,3,0},
{3,3,1,2,5,0,5,4,1,-1,0}
};
char teams[Rows][Cols] = {
{"Kickers"},
{"Kings"},
{"Flyers"},
{"Jaguars"},
{"Giants"},
{"Gladiators"},
{"Rogues"},
{"Miners"},
{"Titans"},
{"Eagles"}
};
int home[Rows][NumberOfGames]={0};
printf("\n\n");
//calculate home games
away=0;
for(r=0;r<Rows;r++) {
for(c=0;c<NumberOfGames-1;c++) {
if(r==c)
home[r][c]= -1;
else if(r<c)
home[r][c]=away;
else
if(!home[c][r])
home[r][c]=1;
away = !away;
printf(" %2d", home[r][c]);
}
putchar('\n');
away = !away;
}
//getch(); //check the home and away assignments
for(r=0;r<Rows;r++) {
for(c=0;c<NumberOfGames-1;c++) {
if((home[r][c] == home[c][r]) && (r != c)) {
printf("\nconflict at R: %d C: %d. press enter to continue", r, c);
(void)getchar();
}
}
}
//(void) getchar(); return 0; //for debug only
putchar('\n');
for(r = 0; r < Rows; r++) {
printf("%s", teams[r]);
for(c = 0; c < NumberOfGames; c++)
printf(" %d", scores[r][c]);
putchar('\n');
}
//calculate points
getPoints(scores, i, home);
output(teams, scores, home);
printf("\n\n\t\t Program Complete - Press Enter When Ready ");
i = getchar(); i++;
return 0;
}
void output(char teams[Rows][Cols], int scores[Rows][NumberOfGames],int home[Rows][NumberOfGames]) {
int i, j, namelen, maxlen, spaces, points;
printf("\n\n\n");
for(i = 0, maxlen = 0; i < Rows; i++) { //get the length of the longest team name
namelen = strlen(teams[i]);
if(namelen > maxlen)
maxlen = namelen;
}
//print the header
for(i = 0; i < maxlen; i++) //the slashes: backslashes are escape char's
printf("%c", '\\'); //so we need two of them, to print one.
printf(" ");
for(i = 0; i < Rows; i++) { //format the team column names
printf(" %c%c%c%c%c", teams[i][0],teams[i][1],teams[i][2],teams[i][3],teams[i][4],teams[i][5]);
}
printf(" Points");
printf("\n-------------------------------------------------------------------------------");
//print the rows
for(i = 0; i < Rows; i++) {
printf("\n %s", teams[i]);
namelen = strlen(teams[i]);
if(namelen < maxlen) {
spaces = maxlen - namelen;
while(spaces--)
putchar(' ');
}
printf(" |");
for(j = 0; j < NumberOfGames-1; j++) {
if(scores[i][j] < 0) {
printf(" --- |");
continue;
}
//this is the scorecard flip trick:
if(home[i][j])
printf(" %d%c%d |", scores[i][j], 254,scores[j][i]);
else
printf(" %d-%d |", scores[i][j], scores[j][i]);
//every game in on the scorecard TWICE, once from the
//perspective of each of the two teams.
}
printf(" %2d", scores[i][10]);
}
}
void getPoints(int scores[Rows][NumberOfGames], int team, int home[Rows][NumberOfGames]) {
int i, j;
for(i=0;i<Rows;i++) {
scores[i][10]=0;
for(j=0;j<NumberOfGames-1;j++) {
if(home[i][j] && scores[i][j] > scores[j][i]) //win at home +2
scores[i][10]+=2;
else if(!home[i][j] && scores[i][j] > scores[j][i]) //win at away +3
scores[i][10]+=3;
else if(scores[i][j] < scores[j][i]) //loss -1
scores[i][10]--;
else if(scores[i][j]==scores[j][i] && i != j) //draw +1
scores[i][10]++;
}
}
}
Output:
-1 1 0 1 0 1 0 1 0 1
0 -1 1 0 1 0 1 0 1 0
1 0 -1 1 0 1 0 1 0 1
0 1 0 -1 1 0 1 0 1 0
1 0 1 0 -1 1 0 1 0 1
0 1 0 1 0 -1 1 0 1 0
1 0 1 0 1 0 -1 1 0 1
0 1 0 1 0 1 0 -1 1 0
1 0 1 0 1 0 1 0 -1 1
0 1 0 1 0 1 0 1 0 -1
Kickers -1 2 3 4 3 2 1 1 5 4 0
Kings 0 -1 3 2 3 0 4 2 2 4 0
Flyers 2 3 -1 5 0 1 3 3 0 2 0
Jaguars 1 1 1 -1 5 5 2 3 1 5 0
Giants 4 2 0 2 -1 3 0 5 2 1 0
Gladiators 3 0 3 1 4 -1 4 0 3 2 0
Rogues 0 4 2 3 1 1 -1 6 4 3 0
Miners 5 5 4 0 3 2 4 -1 0 0 0
Titans 6 2 3 1 3 1 5 6 -1 3 0
Eagles 3 3 1 2 5 0 5 4 1 -1 0
\\\\\\\\\\ Kicke Kings Flyer Jagua Giant Gladi Rogue Miner Titan Eagle Points
-------------------------------------------------------------------------------
Kickers | --- | 2-0 | 3-2 | 4-1 | 3-4 | 2-3 | 1-0 | 1-5 | 5-6 | 4-3 | 8
Kings | 0-2 | --- | 3-3 | 2-1 | 3-2 | 0-0 | 4-4 | 2-5 | 2-2 | 4-3 | 10
Flyers | 2-3 | 3-3 | --- | 5-1 | 0-0 | 1-3 | 3-2 | 3-4 | 0-3 | 2-1 | 5
Jaguars | 1-4 | 1-2 | 1-5 | --- | 5-2 | 5-1 | 2-3 | 3-0 | 1-1 | 5-2 | 8
Giants | 4-3 | 2-3 | 0-0 | 2-5 | --- | 3-4 | 0-1 | 5-3 | 2-3 | 1-5 | -1
Gladiators | 3-2 | 0-0 | 3-1 | 1-5 | 4-3 | --- | 4-1 | 0-2 | 3-1 | 2-0 | 15
Rogues | 0-1 | 4-4 | 2-3 | 3-2 | 1-0 | 1-4 | --- | 6-4 | 4-5 | 3-5 | 3
Miners | 5-1 | 5-2 | 4-3 | 0-3 | 3-5 | 2-0 | 4-6 | --- | 0-6 | 0-4 | 5
Titans | 6-5 | 2-2 | 3-0 | 1-1 | 3-2 | 1-3 | 5-4 | 6-0 | --- | 3-1 | 14
Eagles | 3-4 | 3-4 | 1-2 | 2-5 | 5-1 | 0-2 | 5-3 | 4-0 | 1-3 | --- | 2
Program Complete - Press Enter When Ready