Like Tree2Likes
  • 2 Post By Salem

Count the repeated numbers

This is a discussion on Count the repeated numbers within the C++ Programming forums, part of the General Programming Boards category; I am using this code that to check a set of values from a text file and show it on ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    3

    Count the repeated numbers

    I am using this code that to check a set of values from a text file and show it on the output.

    Code:
    void MatchNumber(int b) {
        vector<Rect> rects; 
       ifstream theFile("CheckNumber.txt");
        double x1,y1,x2,y2;
    
        while(theFile >> x1 >> y1 >> x2 >> y2 ){ 
    
         rects.push_back(Rect(x1,y1, x2,y2)); 
       }
          int num=0;  
    int freq[101] = {0};  
      int adj_count = 0;  
           for (int x = 0; x < rects.size(); ++x) { 
    
     if (rects[b].isAdjacent(rects[x])) {
      if (x==b) {
       continue;
      }
           adj_count++;
        } 
        }  
    freq[num]++;
           cout<<"The common number is = "<<adj_count<<endl;
            cout<< "The "<<adj_count<<" repeated = "<< freq[num]<<" times"<<endl;      cout<<endl; 
    }
    
    
    int main() {
    
     for(int i=0; i<10; i++){
      MatchNUmber(i);
    
      }
      return 0;
    }
    I want to calculate how many times the common number is repeated . So I have used freq[num] in that function. But I am getting the output like this-

    Code:
    The common number is = 5
    The 5 repeated = 1 times 
    The common number is = 6 
    The 6 repeated = 1 times 
    
    The common number is = 4 
    The 4 repeated = 1 times 
    
    The common number is = 5 
    The 5 repeated = 1 times 
    
    The common number is = 5 
    The 5 repeated = 1 times 
    
    The common number is = 8 
    The 8 repeated = 1 times 
    
    The common number is = 9 
    The 9 repeated = 1 times 
    
    The common number is = 6 
    The 6 repeated = 1 times 
    
    The common number is = 6 
    The 6 repeated = 1 times 
    
    The common number is = 8 
    The 8 repeated = 1 times
    So the freq[num] is only returning 1 instead of counting the total number of repeating which is wrong!! I would like to have somthing like this in my output -

    Code:
    
    The common number is = 5
     The common number is = 6
     The common number is = 4
     The common number is = 5
     The common number is = 5
     The common number is = 8
     The common number is = 9
     The common number is = 6
     The common number is = 6
     The common number is = 8 
    The 4 repeated = 1 times 
    The 5 repeated = 3 times 
    The 6 repeated = 3 times 
    The 8 repeated = 2 times 
    The 9 repeated = 1 times
    How can I do that?
    Last edited by aries0152; 07-12-2013 at 11:33 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    Step 1 is learn how to indent code consistently.
    SourceForge.net: Indentation - cpwiki
    It's only 30 lines, but it's basically unreadable.

    > ifstream theFile("CheckNumber.txt");
    Next, you create a number of simple input files, for example
    - an empty file
    - a file with one rectangle
    - a file with two rectangles, which "pass" the test
    - a file with two rectangles, which "fail" the test

    When your code passes the simple tests, then you can try it with more complicated files, knowing that you've already ironed out all the silly mistakes.
    stahta01 and rogster001 like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    3
    Quote Originally Posted by Salem View Post
    Step 1 is learn how to indent code consistently.

    > ifstream theFile("CheckNumber.txt");
    Next, you create a number of simple input files, for example
    - an empty file
    - a file with one rectangle
    - a file with two rectangles, which "pass" the test
    - a file with two rectangles, which "fail" the test

    When your code passes the simple tests, then you can try it with more complicated files, knowing that you've already ironed out all the silly mistakes.
    I do not have problem with getting input from the file . I have already checked it. I am having problem in while calculating the total "repeated common numbers".

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    Your code still looks a mess.

    You haven't provided us with an example file (short, but causes a specific problem) to test with.

    If you want people to help, then minimise the amount of work they have to do (it should be just copy/paste/compile/run).

    You're making people format your code and guess what's in your data file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help: count even and odd numbers
    By taigaicon in forum C Programming
    Replies: 1
    Last Post: 03-11-2011, 01:19 PM
  2. Replies: 9
    Last Post: 03-13-2010, 02:40 AM
  3. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 09:42 AM
  4. Trying to count numbers in file
    By ammochck21 in forum C++ Programming
    Replies: 25
    Last Post: 11-17-2006, 11:32 AM
  5. count numbers input
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-10-2002, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21