?
Solved

Easy one

Posted on 2003-03-05
7
Medium Priority
?
185 Views
Last Modified: 2010-04-15

Hi

i have a txt file with this format:
1 23
41 6
1 2
4 5
23 1
...

i need to fill a matrix with the following structure:
every pair of numbers represent the number of row and column, for example, if i find the couple 1 23 in the text file, then to the position row 1, column 23, i must add 1 to the value of that single position (i begin with a 50 by 50 matrix full of zeros). But the same should happen when i find a couple 23 1, this is, I only want to fill the upper part of the matrix. Afterwards i need to find the maximum value on the matrix.

Other important thing is that i dont know how many lines i have in the txt file, it is created randomly and every time is has a different number of lines.
0
Comment
Question by:elprado
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:snewo
ID: 8074870

1. What do you mean by "You only want to fill the upper part of the matrix"?

2. What do you mean the "Same" should happen when using 23 1, that should hit a different box in the matrix.

3. I'm new here and I get the impression that we aren't supposed to answer questions that smell of homework.  Is that the case experts?


Snewo
0
 

Author Comment

by:elprado
ID: 8075003
1.What do you mean by "You only want to fill the upper part of the matrix"?

the upper part of the matrix is that one above the main diagonal

2. What do you mean the "Same" should happen when using 23 1, that should hit a different box in the matrix.

the thing is that the listing in the text file can be like:
1 23
4 14
11 3
23 1


so in this case when the pair 1 23 and pair 23 1 appear, i would like to add +1 to position row 1 column 23 for each case.
Meaning that i would like the biggest of the two numbers in each line should be the column and the lowest the row for every case.

3. I'm new here and I get the impression that we aren't supposed to answer questions that smell of homework.  Is that the case experts?

im sorry you got that impression, im trying to answer a RWA problem, which is routing and wavelength assignment problem in a optical transmission system, i just not that good in c programming, and i got stuck in this point. the text file is the solution for the optimal routing and i still have to finish the wavelength assignment part.

thank you


0
 
LVL 6

Expert Comment

by:gj62
ID: 8075046
If you have a 50x50 matrix, why do you care about reversing the x,y values?  It takes up the same amount of space, it is just that 1/2 is unused... maybe I am missing something there.

In any event, you can set up a loop and test for EOF - you don't need to know how many lines the file has.

For each line, you can use strtok() to parse them into two strings, convert the strings to int's by using atoi(), and then use them in the array indexes, e.g...

int matrix[50][50];
char *sX, *sY;
char seps[] = " ";
int x, y;

memset(matrix,0,50*50*sizeof(int)); /* set to 0 */
while( !feof(myfile) )
{
  fgets(string, MAX_STRING_SIZE, myfile);
  sX = strtok(string,seps);
  sY = strtok(NULL,seps);
  x = atoi(sX);
  y = atoi(sY);
  matrix[x][y]+=1;
}

That's pretty much it.

If you want to, you can add the following after you atoi to get the ints...

if (y>x)
{
  iTemp = x;
  x = y;
  y = iTemp;
}
0
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!

 
LVL 6

Expert Comment

by:gj62
ID: 8075053
So, it would look like this:

while( !feof(myfile) )
{
 fgets(string, MAX_STRING_SIZE, myfile);
 sX = strtok(string,seps);
 sY = strtok(NULL,seps);
 x = atoi(sX);
 y = atoi(sY);
 if (y>x)
 {
   iTemp = x;
   x = y;
   y = iTemp;
 }  
 matrix[x][y]+=1;
}

0
 
LVL 6

Expert Comment

by:gj62
ID: 8075090
Of course, the above code makes a number of assumptions:

1) that the text file will not contain a token larger than 50 - you can test for that and print an error. If you don't, and there is a value >50, you could overrun your array and you will certainly not get the right values...

2) it assumes 2 tokens per line (it may have more, but they will be ignored.  It also assumes that string is large enough to hold an entire line - if not, the file reads will be off and you'll read garbage.

3) the array is of int's - you can change that to longs if you expect a large number of possible values...

Hope this helps...
0
 
LVL 6

Accepted Solution

by:
gj62 earned 200 total points
ID: 8075164
To discover the maximum value, just walk the array:

maxVal=0;
for (i=0;i<50;++i)
{
  for (j=0;j<50;++j)
  {
    if (matrix[i][j]>maxVal)
    {
      xIndex = i;
      yIndex = j;
      maxVal = matrix[i][j];
    }
  }
}

This will give you the max value in the matrix, and the coordinate where it occurs (if needed).  It will scan the entire array (exercise for the user if you only want to scan the 1/2 that's populated <grin>) and it will keep the first max value encountered in case of ties - you didn't say if you needed anything other than that..
0
 

Author Comment

by:elprado
ID: 8075516
Thank you very much, you have been lots of help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

801 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