Solved

# Easy one

Posted on 2003-03-05
Medium Priority
187 Views

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
• 4
• 2

LVL 4

Expert Comment

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

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

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

LVL 6

Expert Comment

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

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

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

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

## Featured Post

Question has a verified solution.

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

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ā¦
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
###### Suggested Courses
Course of the Month12 days, 17 hours left to enroll

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

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