Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to count occurences of a number and its most frequent pair in a text file?

How to count occurences of a number and its most frequent pair in a text file?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
4 Posts 4 Posters 0 Views 1 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.
  • K Offline
    K Offline
    kangkongflea
    wrote on last edited by
    #1

    Supposed I have a text file having the results of the 6/49 lottery in it: 49-05-13-42-18-45 43-24-36-16-39-22 32-48-05-18-34-29 13-14-07-41-48-39 47-05-17-13-37-49 06-23-34-21-46-37 06-42-13-40-11-37 39-44-40-21-32-20 17-26-38-43-01-02 How can I achieve the following output written to a text file with filename, "occurences.txt"? 49 - 2 times 49 paired with 13 - 2 times I don't have an exact idea but maybe the following pseudocode can help:

    void main ()
    {
    int number

    cout<<"CHARACTER \t\ How many times it appeared"<

    How can I go about this? :-(

    L S D 3 Replies Last reply
    0
    • K kangkongflea

      Supposed I have a text file having the results of the 6/49 lottery in it: 49-05-13-42-18-45 43-24-36-16-39-22 32-48-05-18-34-29 13-14-07-41-48-39 47-05-17-13-37-49 06-23-34-21-46-37 06-42-13-40-11-37 39-44-40-21-32-20 17-26-38-43-01-02 How can I achieve the following output written to a text file with filename, "occurences.txt"? 49 - 2 times 49 paired with 13 - 2 times I don't have an exact idea but maybe the following pseudocode can help:

      void main ()
      {
      int number

      cout<<"CHARACTER \t\ How many times it appeared"<

      How can I go about this? :-(

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You cannot use character types that contain more than one character, like '49'. The first thing you need to do is read all the input, split the items into separate fields and convert the numbers to integers. You can then count the individual values, and the sets just by counting through all the saved numbers. You do not need to count from 1 to 49, since some of those values will not be present.

      1 Reply Last reply
      0
      • K kangkongflea

        Supposed I have a text file having the results of the 6/49 lottery in it: 49-05-13-42-18-45 43-24-36-16-39-22 32-48-05-18-34-29 13-14-07-41-48-39 47-05-17-13-37-49 06-23-34-21-46-37 06-42-13-40-11-37 39-44-40-21-32-20 17-26-38-43-01-02 How can I achieve the following output written to a text file with filename, "occurences.txt"? 49 - 2 times 49 paired with 13 - 2 times I don't have an exact idea but maybe the following pseudocode can help:

        void main ()
        {
        int number

        cout<<"CHARACTER \t\ How many times it appeared"<

        How can I go about this? :-(

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        In additoon to what Richard said, you seem to be having a very vague understanding of C/C++ programming. Before trying your hand on a moderately difficult problem, you should really start with a tutorial on C++ programming. There are plenty of good sites on the web for this, e.g. at C++ Language - C++ Tutorials[^] You should specifically focus on the first chapter, which covers pretty much everything you need for the program you asked about.

        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

        1 Reply Last reply
        0
        • K kangkongflea

          Supposed I have a text file having the results of the 6/49 lottery in it: 49-05-13-42-18-45 43-24-36-16-39-22 32-48-05-18-34-29 13-14-07-41-48-39 47-05-17-13-37-49 06-23-34-21-46-37 06-42-13-40-11-37 39-44-40-21-32-20 17-26-38-43-01-02 How can I achieve the following output written to a text file with filename, "occurences.txt"? 49 - 2 times 49 paired with 13 - 2 times I don't have an exact idea but maybe the following pseudocode can help:

          void main ()
          {
          int number

          cout<<"CHARACTER \t\ How many times it appeared"<

          How can I go about this? :-(

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          I'm sure there are many ways to skin this cat, but how about a loop that reads each line of the file into a string type, then calls string::find() for things like "49-" and "-49"? If one of those is found, then do something similar with the 13. Would that work?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups