Solved

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

Posted on 2004-04-24
19
312 Views
Last Modified: 2010-04-15
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
Comment
Question by:Celiowin
  • 10
  • 9
19 Comments
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 

Author Comment

by:Celiowin
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 

Author Comment

by:Celiowin
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
>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
 

Author Comment

by:Celiowin
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Celiowin
Comment Utility
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
 
LVL 9

Accepted Solution

by:
ankuratvb earned 500 total points
Comment Utility
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
 

Author Comment

by:Celiowin
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 

Author Comment

by:Celiowin
Comment Utility
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
 

Author Comment

by:Celiowin
Comment Utility
Don't worry, I figured it out, and thanks for all your help.
0
 

Author Comment

by:Celiowin
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 

Author Comment

by:Celiowin
Comment Utility
Can u please give me an example?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now