Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Need help to correct a function !
Forum Updated to NodeBB v4.3 + New Features

Need help to correct a function !

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 6 Posters 1.7k Views 2 Watching
  • 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.
  • L Offline
    L Offline
    Lab_younes
    wrote on last edited by aha_1980
    #1

    void calcMean(QVariant *rawdata){
    double mean = 0.0;
    // convert data to QMap
    if (rawdata->type() == QVariant::Map)
    {
    QMap<QString, QVariant> rawdataMap = rawdata->toMap();
    QList<QVariant> velocity = rawdataMap["velocity"].toList();
    // calc mean value of velocity
    for (int i = 0; i < velocity.size(); i++){
    mean += velocity.at(i).toDouble();
    }
    mean = mean / velocity.size();
    // write mean into file
    QFile outputFile("outputData.txt");
    outputFile.open(QIODevice::WriteOnly);
    QTextStream stream(&outputFile);
    stream << mean;
    }
    }

    How can be this function corrected ?
    thank you in advance

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      What is the problem ? We can help u to correct if we know the issue u r facing

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      L 1 Reply Last reply
      0
      • Maaz MominM Offline
        Maaz MominM Offline
        Maaz Momin
        wrote on last edited by Maaz Momin
        #3

        There is high chance that you'll get a "Divide by 0 error" on line "mean = mean / velocity.size();" if your velocity.size() is 0.

        1 Reply Last reply
        5
        • dheerendraD dheerendra

          What is the problem ? We can help u to correct if we know the issue u r facing

          L Offline
          L Offline
          Lab_younes
          wrote on last edited by
          #4

          @dheerendra
          That's an exercise , that i have to solve it
          and that's the statement :
          The errors must be intercepted. Given is the source code to a function. Identify locations where errors can occur and suggest a way to manage errors. Also identify places that could lead to undefined behavior of the software !

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

            Hi,

            So you are basically asking us to do your homework for you ?

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

            L 1 Reply Last reply
            2
            • SGaistS SGaist

              Hi,

              So you are basically asking us to do your homework for you ?

              L Offline
              L Offline
              Lab_younes
              wrote on last edited by
              #6

              @SGaist
              looool
              it's just an exercise that's not part of my university life!
              because I'm not professional in Q.t and I always try to learn new things and solve some exercises :D
              but thanks anyway

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

                Then it's always better to be very precise about what you are currently doing. That will allow people to help you in better ways.

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

                L 1 Reply Last reply
                2
                • SGaistS SGaist

                  Then it's always better to be very precise about what you are currently doing. That will allow people to help you in better ways.

                  L Offline
                  L Offline
                  Lab_younes
                  wrote on last edited by
                  #8

                  @SGaist
                  I have to Identify locations where errors can occur and suggest a way to manage errors :/
                  but here I see no error
                  and I'am not sure

                  JonBJ 1 Reply Last reply
                  0
                  • L Lab_younes

                    @SGaist
                    I have to Identify locations where errors can occur and suggest a way to manage errors :/
                    but here I see no error
                    and I'am not sure

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Lab_younes
                    Although there is no glaringly obvious fault in the code, I presume you are supposed to analyse it and think were it might have problems. One post above made a suggestion. Parameters are worth looking at. I/O can have problems. That sort of thing?

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      Hi, do you mean what checks it needs to be production quality ?

                      void calcMean(QVariant *rawdata)  
                      {
                          double mean = 0.0;
                      // convert data to QMap
                          if (rawdata->type() == QVariant::Map) {  // ** no check if null before use
                              QMap<QString, QVariant> rawdataMap = rawdata->toMap();
                              QList<QVariant> velocity =
                                  rawdataMap["velocity"].toList(); // ** no check if "velocity" exists.
                      // calc mean value of velocity
                              for (int i = 0; i < velocity.size(); i++) {
                                  mean += velocity.at(i).toDouble(); // ** no check if toDouble fails
                              }
                              mean = mean / velocity.size(); // ** no check if list is empty and would divide by zero
                      // write mean into file
                              QFile outputFile("outputData.txt");
                              outputFile.open(QIODevice::WriteOnly); // ** no check if file could be open
                              QTextStream stream(&outputFile);
                              stream << mean;
                          }
                      }
                      
                      L 1 Reply Last reply
                      3
                      • mrjjM mrjj

                        Hi, do you mean what checks it needs to be production quality ?

                        void calcMean(QVariant *rawdata)  
                        {
                            double mean = 0.0;
                        // convert data to QMap
                            if (rawdata->type() == QVariant::Map) {  // ** no check if null before use
                                QMap<QString, QVariant> rawdataMap = rawdata->toMap();
                                QList<QVariant> velocity =
                                    rawdataMap["velocity"].toList(); // ** no check if "velocity" exists.
                        // calc mean value of velocity
                                for (int i = 0; i < velocity.size(); i++) {
                                    mean += velocity.at(i).toDouble(); // ** no check if toDouble fails
                                }
                                mean = mean / velocity.size(); // ** no check if list is empty and would divide by zero
                        // write mean into file
                                QFile outputFile("outputData.txt");
                                outputFile.open(QIODevice::WriteOnly); // ** no check if file could be open
                                QTextStream stream(&outputFile);
                                stream << mean;
                            }
                        }
                        
                        L Offline
                        L Offline
                        Lab_younes
                        wrote on last edited by
                        #11

                        @mrjj said
                        Hi
                        Here no try-catch blocks may be used. For the software developed to be robust, errors must be intercepted.

                        mrjjM 1 Reply Last reply
                        0
                        • L Lab_younes

                          @mrjj said
                          Hi
                          Here no try-catch blocks may be used. For the software developed to be robust, errors must be intercepted.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Lab_younes
                          You would not use exception for this function.
                          But it does no input validation , nor check the conversion. And also
                          do not check if the out put file could be open.
                          It should also return a value indication success or not.

                          L 1 Reply Last reply
                          2
                          • mrjjM mrjj

                            @Lab_younes
                            You would not use exception for this function.
                            But it does no input validation , nor check the conversion. And also
                            do not check if the out put file could be open.
                            It should also return a value indication success or not.

                            L Offline
                            L Offline
                            Lab_younes
                            wrote on last edited by
                            #13

                            @mrjj
                            Thank you very much !
                            it makes sense :D
                            and can be for example a possibility for error management suggested ?

                            mrjjM 1 Reply Last reply
                            0
                            • L Lab_younes

                              @mrjj
                              Thank you very much !
                              it makes sense :D
                              and can be for example a possibility for error management suggested ?

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #14

                              @Lab_younes
                              Hi
                              It could be done in different ways but minimum it could report back to caller the status.

                              enum calcMeanErros { Success = 1, NullInput, EmptyVelocityList, WrongMapFormat, ConvertError, FileOpenError  };
                              constexpr const char *velocityStr = "velocity";
                              calcMeanErros calcMean(QVariant *rawdata)
                              {
                                  if (! rawdata ) return NullInput; //-> check
                                  double mean = 0.0;
                              // convert data to QMap
                                  if (rawdata->type() == QVariant::Map) {
                                      QMap<QString, QVariant> rawdataMap = rawdata->toMap();
                                      if ( ! rawdataMap.contains(velocityStr) ) return WrongMapFormat;
                                      QList<QVariant> velocity = rawdataMap[velocityStr].toList();
                                      if ( velocity.size() == 0 )  return EmptyVelocityList;  //-> check
                              // calc mean value of velocity
                                      for (int i = 0; i < velocity.size(); i++) {
                                          bool ok;
                                          mean += velocity.at(i).toDouble(&ok);
                                          if (!ok) return ConvertError;  //-> check
                                      }
                                      mean = mean / velocity.size(); 
                              // write mean into file
                                      QFile outputFile("outputData.txt");
                                      if ( ! outputFile.open(QIODevice::WriteOnly) ) return FileOpenError;  //-> check
                                      QTextStream stream(&outputFile);
                                      stream << mean;
                                      return Success;
                                  }
                                  return WrongMapFormat;
                              }
                              
                              
                              1 Reply Last reply
                              3

                              • Login

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