How to take the number separated by the whole function just below and sort it correctly as described in the main doubt in bold in the question in C++

Asked

Viewed 128 times

0

The function to separate an integer number from a noninteger is simple

void Inteiro(float n)
 {
      int x = n;
      float y = n;
      if (x==y)
      {
        cout << "E inteiro" << endl;
      }
      else 
      {
        cout << "Nao e inteiro" << endl;
       }
}

I just wanted you to do something like this: Data input example OBS: data is in a file txt

0,9
36646
0,4
38833
33882
38849
0,7
36646

main doubt, it is algorithm that I need in this question {I need the logic that must be implemented in the integer function that reads mode 0.9 is note corresponding to the number of the bottom that in case 36646 an integer, soon after comes 0.4 that say new broken number in which corresponds the number 38833,33882,38849 and 0,7 again corresponds to the number 36646 only that 0,7 must be added with 0,9 because both are of the same number}

and when printed will show the number with your corresponding note

as

36646 1,6 //com a nota somada
....
..
.
.
  • 2

    It’s not clear what your question is. Your title asks a question that you answer yourself. Then you mention another definition of problem but no code nor doubt. You’d better edit your question to make it clear what problem you’re having and the solution you’re trying to solve.

  • @Tiagogomes at a glance at the issue see if understood, focus on the bold part in the italics section of the question.

  • 2

    Rodolfo, in the "bold part" you ask for the algorithm, and I find it difficult for someone to do it for you. The purpose of the site is to answer concrete and objective questions, and not to do your homework for you. Why not edit the question and focus on a specific question (for example, how do you actually identify whether a number is integer or not? or, how do you store and access a value mapped by a key? - in your case, the registration ID - and so on). Make it easy for you to get an answer, and then you can open up new questions for any other questions you have.

1 answer

2


It is not clear in the question, but it seems that its difficulty is how to relate pairs of values (the student’s enrollment and his grade, for example, or a player’s score to his ID, etc).

For that, use a map (class map) of the standard library. The code below exemplifies its use in the context of your problem (without the remainder of reading the file and identifying the numbers):

#include <string>
#include <map>

using namespace std;

int main(void)
{
    /* Define o mapa de notas (matrícula x valor da nota). */
    map<int, float> notas;

    notas[36646] += 0.9f;

    notas[38833] += 0.4f;
    notas[33882] += 0.4f;
    notas[38849] += 0.4f;

    notas[36646] += 0.7f; /* <- Observe a repetição da matricula 36646! */


    /* Teste de verificação */

    map<int, float>::iterator it;
    for(it = notas.begin(); it != notas.end(); ++it)
    {
        printf("Matricula #%d -> Nota: %02.02f\n", it->first, it->second);
    }

    return 0;
}

Remarks:

  1. The map is declared as map<int, float> because it relates an integer (the registration) to a floating point value (the note). So, whenever you do nota[XXXX] you have access to the value of the note for registration XXXX. If this value has not yet been updated on the map, it is zero (0).
  2. Updates are always done using the operator += because it does not replace the value that was already mapped for that registration, but adds the new value in the one that already existed.
  3. Floating point values fixed in my sample code are set with a letter f at the end to indicate this to the compiler and avoid boring warnings. In your case you will read from the file then this letter will not appear.
  4. The piece of code at the end with the loop for serves only to demonstrate that the result is what you expect.

Output from previous example program:

Matricula #33882 -> Nota: 0.40
Matricula #36646 -> Nota: 1.60
Matricula #38833 -> Nota: 0.40
Matricula #38849 -> Nota: 0.40

Browser other questions tagged

You are not signed in. Login or sign up in order to post.