Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

Reading in 2 text files, store in 2 2-d array, and display them as one.

Hello, i have two text files, the first one is a list of teams (teams.txt)
Cougers
Dingoes
Eagles
Kangaroos
Taipans
Wolves

I have read the first file under void inputNames (char name[maxNames][nameLength]), and displayed it under, outputNames (char name[maxNames][nameLength]). SEE BELOW.
----------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define nameLength     10
#define maxNames     6
#define     tableColumns 8
#define     tableRows     7


void inputNames (char name[maxNames][nameLength]);
void outputNames (char name[maxNames][nameLength]);


void inputResults (char name[maxNames][nameLength],float table[tableRows][tableColumns]);

int main ()
{

    char name[maxNames][nameLength];
    float table[tableRows][tableColumns]={0.0};

   
      inputNames (name);

    outputNames (name);
      
    inputResults (name,table);
   
    return 0;
}
/*************************************************/
void inputNames (char name[maxNames][nameLength])
{
    FILE *fptrOne;
    int i;

    if ((fptrOne = fopen ("clubs.txt","r"))==NULL)
    {
      printf ("Cannot open file.\n");
      exit(0);
    }
    for(i=0;i<maxNames;i++)
    {
      fscanf(fptrOne,"%s\n", name[i]);
    }

    fclose(fptrOne);
}
/*************************************************/
void outputNames (char name[maxNames][nameLength])
{
    int i;
    for(i=0;i<maxNames;i++)
    {
      printf("%s\n", name[i]);
    }
}
/*************************************************/
void inputResults (char name[maxNames][nameLength],float table[tableRows][tableColumns])
{
   
}


But I have a problem reading and displaying the second file (results.txt). Which looks like this.

week 1 results
Cougers 91 Dingoes 73
Eagles 101 Kangaroos 99
Taipans 85 Wolves 84

week 2 results
Kangaroos 79 Wolves 71
Cougers 108 Eagles 102
Dingoes 93 Taipans 84

I need store them in a second two dimensional floating point array (6 columns and 6 rows), and display the infromation stored in the array and display both of the array, and updates each week.

E.g.
week 1 results
Names           Played     Won   lost     Drawn    For     Against         Points
-------------------------------------------------------------------------------------
Cougers              1          1        0          0        91         73                2
Dingoes              1           0         1         0        73         91                0
Eagles                 1          1        0          0        101       99                2
Kangaroos           1           0        1          0        99         101              0
Taipans               1           1         0         0        85         84               2
Wolves                1           0         1          0        84        85               0


week 2 results
Names           Played     Won   lost     Drawn    For     Against          Points
----------------------------------------------------------------------------------------
Cougers              2          2       0          0        199         144              4
Dingoes              2           1         1         0        166         175             2
Eagles                 2          1        1          0        203       207                2
Kangaroos           2           1        1          0        178         172             2
Taipans               2           1         1         0        169         177             2
Wolves                2           0         2          0        155        164             0

I really need help with the second two dimensional floating point array.
0
Celiowin
Asked:
Celiowin
  • 10
  • 9
1 Solution
 
ankuratvbCommented:
There should be 7 columns in your float double dim. array as you have now included the against column.

Since you want to display the cumulative points against for each player,you will need 7 columns.
0
 
ankuratvbCommented:
When you add points in the for column for one player say with array index pos,you know the index of the other player pos2 also.

So,In the against column,add the average of the other player of this match.

Submit your code so we could help you furthur.

0
 
ankuratvbCommented:
Make these changes,marked with //*****

    while(!feof(fptrTwo))
    {
       fscanf(fptrTwo,"%s %d\n",temp, &t);
       pos=-1;
       //pos2=-1;
       if(strcmpi(temp,"round")==0)
       {
        if(flag==0) flag++;
        else
        {
         printf("\nRound %d results\n",flag);
         flag++;
          outputResults (name,table);
        }
       }
       for(j=0;j<maxNames;j++)
       {
        if(strcmp(name[j],temp)==0) {pos=j;break;}
       }
       if(pos!=-1)
       {
       table[pos][4]+=t;
       table[pos][0]+=1;
       if(pos2==-1) pos2=pos;
       else
       {
        if(table[pos][4]>table[pos2][4])

        {
                    table[pos][1]+=1;table[pos2][2]+=1;
                    table[pos][6]+=2;//*****Points column shifts to index 6
        }

        else if(table[pos][4]==table[pos2][4])
        {
                    table[pos][3]+=1;table[pos2][3]+=1;
                    table[pos][6]+=1;table[pos2][6]+=1;
          //*****Points column shifts to index 6
                 }
        else
        {
                    table[pos2][1]+=1;table[pos][2]+=1;
                    table[pos2][6]+=2;//*****Points column shifts to index 6
        }

//table[pos][5]=points against.For player pos,add average of pos2(which is table[pos2][4])
//and for player pos2,add average of pos(which is table[pos][4])

        table[pos][5]+=table[pos2][4];//******
        table[pos2][5]+=table[pos][4];//******
        pos2=-1;
       }
       }
    }

HTH.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CeliowinAuthor Commented:
There is something wrong with the code. In the Against column it is not displaying the right figures.

week 1 results
Names           Played     Won   lost     Drawn    For     Against         Points
-------------------------------------------------------------------------------------
Cougers              1          1        0          0        91         73                2


This what what week 2 should be.
week 2 results
Names           Played     Won   lost     Drawn    For     Against          Points
----------------------------------------------------------------------------------------
Cougers              2          2       0          0        199         175              4

Cougers (Against) = (week 1)Dingoes 73 + (week 2)Eagles 102 = 175

But it is doing this after week 2
Cougers (Against) = (week 1 FOR)Dingoes 73 + (week 2 FOR)Eagles 203 = 276. It is adding the total points scored by there opponents, instead of adding the points scored by there opponents on that week. That is what i am having troble with.
0
 
ankuratvbCommented:
Sorry for that:

Minor change here:

declare:
    float against1,against2; //at the start of the function

    while(!feof(fptrTwo))//for(i=0;i<maxNames;i++)
    {
       fscanf(fptrTwo,"%s %d\n",temp, &t);
       pos=-1;
       //pos2=-1;
       if(strcmpi(temp,"round")==0)
       {
        if(flag==0) flag++;
        else
        {
         printf("\nRound %d results\n",flag);
         flag++;
          outputResults (name,table);
        }
       }
       for(j=0;j<maxNames;j++)
       {
        if(strcmp(name[j],temp)==0) {pos=j;break;}
       }
       if(pos!=-1)
       {
       table[pos][4]+=t;
       table[pos][0]+=1;
       if(pos2==-1) {pos2=pos;against1=t;}//store the current avg for first player
       else
       {
        against2=t;//store current avg for second player
        if(table[pos][4]>table[pos2][4])

        {table[pos][1]+=1;table[pos2][2]+=1;table[pos][6]+=2;
        }

        else if(table[pos][4]==table[pos2][4])
        {table[pos][3]+=1;table[pos2][3]+=1;table[pos][6]+=1;table[pos2][6]+=1;
        }
        else
        {table[pos2][1]+=1;table[pos][2]+=1;table[pos2][6]+=2;
        }

        table[pos][5]+=against1;//add
        table[pos2][5]+=against2;//add

        pos2=-1;
       }
       }
    }

What was happening was the for score accumulated was being added to the against.
0
 
CeliowinAuthor Commented:
Under the played, won, lost, draw column, it display 0.0, 0.0, 0.0 can it made to display 0, 0, 0?

I'm not allow to use the break statement, so i took it out, it still works, will it have any effects that i don't about?
0
 
ankuratvbCommented:
>I'm not allow to use the break statement, so i took it out, it still works, will it have any >effects that i don't about?

Not too much.The purpose of using the break statement was to stop searching the moment it found a matching name in the names array.Now,it would scan the entire length of the array.Just take a little bit more time.

>Under the played, won, lost, draw column, it display 0.0, 0.0, 0.0 can it made to display 0, >0, 0?

In your display function,You'd have something like:

printf("%f ",table[][]);
change that to :
printf("%.0f ",table[][]);

This will display all values wihtout the decimal part.
0
 
CeliowinAuthor Commented:
Line 1, results01 = against1 - against2;
Line 2, printf("\nTeamA defeated TeamB\t %d\n",points);

I tried inserting the two lines above into the code u sent on, 04/25/2004 12:09AM PDT, but it keeps displaying it multiple times.
0
 
ankuratvbCommented:
When you say Team A beat Team B,you'd be talking about one match.
Since we store values one match at a time,it'd print the message multiple times since you put the printf inside the loop.

When do you want to print this message?
0
 
CeliowinAuthor Commented:
I want print like this -
week 1 results
Cougers defeated Dingoes by 18 points.
Eagles defeated Kangaroos by 2 points.
Taipans defeated Wolves by 1 points.

Names           Played     Won   lost     Drawn    For     Against         Points
-------------------------------------------------------------------------------------
Cougers              1          1        0          0        91         73                2
Dingoes              1           0         1         0        73         91                0
Eagles                 1          1        0          0        101       99                2
Kangaroos           1           0        1          0        99         101              0
Taipans               1           1         0         0        85         84               2
Wolves                1           0         1          0        84        85               0

0
 
ankuratvbCommented:
DO the following:

    while(!feof(fptrTwo))//for(i=0;i<maxNames;i++)
    {
       fscanf(fptrTwo,"%s %d\n",temp, &t);
       pos=-1;
       //pos2=-1;
       if(strcmpi(temp,"round")==0)
       {
        if(flag==0) flag++;
        else
        {
         printf("\nRound %d results\n",flag);
         flag++;
          outputResults (name,table);
        }
       }
       for(j=0;j<maxNames;j++)
       {
        if(strcmp(name[j],temp)==0) {pos=j;break;}
       }
       if(pos!=-1)
       {
       table[pos][4]+=t;
       table[pos][0]+=1;
       if(pos2==-1) {pos2=pos;against1=t;}
       else
       {
        against2=t;
//note the printf statements below:
        if(table[pos][4]>table[pos2][4])
        {table[pos][1]+=1;table[pos2][2]+=1;table[pos][6]+=2;
         printf("%s defeated %s by %.0f points\n",name[pos],name[pos2],against1-against2);
        }

        else if(table[pos][4]==table[pos2][4])
        {table[pos][3]+=1;table[pos2][3]+=1;table[pos][6]+=1;table[pos2][6]+=1;
         printf("%s drew with %s at %.0f points\n",name[pos],name[pos2],against1);
        }
        else
        {table[pos2][1]+=1;table[pos][2]+=1;table[pos2][6]+=2;
         printf("%s defeated %s by %.0f points\n",name[pos2],name[pos],against1-against2);
        }

        table[pos][5]+=against1;
        table[pos2][5]+=against2;
        pos2=-1;
       }
       }
    }
0
 
CeliowinAuthor Commented:
This line printf("\nRound %d results\n",flag); how can i make it print out like this:

Round 1 results
Cougers defeated Dingoes by 18 points.
Eagles defeated Kangaroos by 2 points.
Taipans defeated Wolves by 1 point.

Sorting Column (Points), but do you move the whole row including the team name with it?
like the table below?

Names           Played     Won   lost     Drawn    For     Against         Points
-------------------------------------------------------------------------------------
Cougers              1          1        0          0        91         73                2
Eagles                 1          1        0          0        101       99                2
Taipans               1           1         0         0        85         84               2
Dingoes              1           0         1         0        73         91                0
Kangaroos           1           0        1          0        99         101              0
Wolves                1           0         1          0        84        85               0
0
 
ankuratvbCommented:
You just want the output sorted,right?

In your output function,just check and display the rows in descending order of points.

BTW,is this your homework?
0
 
ankuratvbCommented:
If you do want to sort,sort on the points column and swap the corresponding values of the names array as well.

Assuming you are doing bubble sort(its the simplest but least efficient)

when you swap,you'd have code like:
You;d get i and j by comparing the points values.See bubble sort algorithm

the 2 positions being swapped are i and j
{
 float temp;

 for(i=0;i<6;i++)
 {
  temp=table[i][6];
  table[i][6]=table[j][6];
  table[j][6]=temp;
 }
 char tmp[100];
 strcpy(tmp,name[i]);
 strcpy(name[i],name[j]);
 strcpy(name[j],tmp);
}

Thats it.
0
 
CeliowinAuthor Commented:
No, the sorting question was just a tutorial question, the text books only demonstrated on one dimensional array.

But the actual question i was asking was this

This line printf("\nRound %d results\n",flag); how can i make it print out like this:

Round 1 results

Cougers defeated Dingoes by 18 points.
Eagles defeated Kangaroos by 2 points.
Taipans defeated Wolves by 1 point.

Currently it's printing like this:
Cougers defeated Dingoes by 18 points.
Eagles defeated Kangaroos by 2 points.
Taipans defeated Wolves by 1 point.
Round 1 results

I've got it printing like this at one stage.
Round 1 results
Cougers defeated Dingoes by 18 points.
Round 1 results
Eagles defeated Kangaroos by 2 points.
Round 1 results
Taipans defeated Wolves by 1 point.



0
 
CeliowinAuthor Commented:
Don't worry, I figured it out, and thanks for all your help.
0
 
CeliowinAuthor Commented:
I need help to write a function which ouputs the final league table to a file called final_table.txt.

void finalTable (char name[maxNames][nameLength],float table[tableRows][tableColumns])
{
FILE *newfileptr;
int i,j;

newfileptr = fopen("final_table.txt,"w");

      
for(i=0;i<tableRows;i++)
{
printf("%s ",name[i]);
for(j=0;j<tableColumns;j++)
{
printf("%.0f",table[i][j]);
}
else
{
printf("%.2f",table[i][j]);
}                  
}
fclose(newfileptr);
}
0
 
ankuratvbCommented:
open the file using FILE* and fopen.
Replace all your printf statements by fprintfs
if u use FILE *f,
replace printf("%.0f",table[i][j]); with fprintf(f,"%.0f',table[i][j]);
Similarly for other printfs also.
0
 
CeliowinAuthor Commented:
Can u please give me an example?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now