[Solved]txt file output the line number or row number where i got the max value
-
Hi friends,
i have to read a txt file and compare the rows get the maximum value along with the index of the row i have got the maximum number.
what i have done so far is read the txt file line by line and compare the values and output the maximum number, but not index of the row where I got the maximum number.
could someone please help me how i could get the index of a maximum number(or the line number where i get the maximum value).
for example if have
1,2,3,4,7
2,3,4,5,6
7,8,9,2,1output should be like
index, maximum number
2 , 7
2 , 8
1 , 5
0 ,7please i have tried different ways but i couldn't be able to figure out how to do this, hope some one will assist me.
@
for (row = 0; row < 5; row++){
QByteArray line = file.readLine(); // read line by line
QList<QByteArray> numbers = line.split(',');
for (column = 0; column < (numbers.size() - 1); column++) {
data = numbers[column].toInt(); // convert the byte array into an integer
if (data > max[column]) // compare with row number to get the maximum number of colum0 etc.
max[column] = data;
}
}
@[edit: added coding tags @ SGaist]
-
Hi,
There are several ways to do it e.g. a QList<QPair<int, int>> where the QPair's first is the row and QPair's second is the max value
Hope it helps
-
For each row store the max and max column values and add them to the list before starting a new row
-
Hi try this..
int l=0;
QList<int> lst;
QList<QPair<int,int> > list;
QPair<int,int> pair;
int Column=4;
int Row=3;
for(int i = 0; i < Column; i++)
{
for(int j = 0; j < Row; j++)
{
pair.first = j;
pair.second=data[j][l]
list.append(pair);
lst.append(data[j][l]);
}
QList<int>::iterator it= std::max_element(lst.begin(),lst.end());
if(pair.second==*it)
qDebug() <<"MAX : "<< *it << "index :" << pair.first;
lst.clear();
list.clear();
++l;
}
Data is your 2D array inwhich your values of your file is stored.
-
Thanks IamSumit & SGaist that works, have taken one more variable Maxindex and updated row to that variable, fixes the problem
@
for (row = 0; row < 5; row++){
QByteArray line = file.readLine(); // read line by line
QList<QByteArray> numbers = line.split(',');
for (column = 0; column < (numbers.size() - 1); column++) {
data = numbers[column].toInt(); // convert the byte array into an integer
if (data > max[column]) // compare with row number to get the maximum number of colum0 etc.
max[column] = data;
Maxindex[column] = row;
}
}
@[edit add missing @ coding tags SGaist]