Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Solved]txt file output the line number or row number where i got the max value
QtWS25 Last Chance

[Solved]txt file output the line number or row number where i got the max value

Scheduled Pinned Locked Moved C++ Gurus
7 Posts 3 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pushpa416
    wrote on last edited by
    #1

    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,1

    output should be like
    index, maximum number
    2 , 7
    2 , 8
    1 , 5
    0 ,7

    please 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]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pushpa416
        wrote on last edited by
        #3

        thanks but how to use it in my code

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          For each row store the max and max column values and add them to the list before starting a new row

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • IamSumitI Offline
            IamSumitI Offline
            IamSumit
            wrote on last edited by
            #5

            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.

            Be Cute

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pushpa416
              wrote on last edited by
              #6

              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]

              1 Reply Last reply
              0
              • IamSumitI Offline
                IamSumitI Offline
                IamSumit
                wrote on last edited by
                #7

                If your problem is solved then prepend [SOLVED] in thread title.
                Thanks

                Be Cute

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved