Counting Numbers in Array, not counting last number!

This is a discussion on Counting Numbers in Array, not counting last number! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <algorithm> #include <fstream> using namespace std; char signed_character; unsigned char byte; unsigned int numberofbytes; int i, ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    Counting Numbers in Array, not counting last number!

    Code:
    #include <iostream>
    #include <algorithm>
    #include <fstream>
    using namespace std;
    
    char signed_character;
    unsigned char byte;
    unsigned int numberofbytes;
    int i, num=0, number=0;
    int numarray[262143];
    
    
     int main()
    
    {
    	
    	ifstream file("Binary File", ios::in||ios::binary);
    
    	while (file.get(signed_character))
    	{
    		byte =(unsigned char) signed_character;
    			numarray[i]=byte;
    			numberofbytes++;
    			i++;
    
    	}
    	sort(numarray, numarray+262143);
    
    		for (i=0;i!=262143;i++)
    		{
    		if(number==numarray[i])
    		{
    			num++;
    		}
    		else
    		{
    			
    			cout<<"There are "<<num<<" "<<number<<" s in this file \n";
    			i--;
    			number++;
    			num=0;
    		}
    		
    
    }
    
    
    return 0;
    
    }
    This code Reads in a binary file, counts up the number of binary 1s,2s,3s,4s.....255s and displays them on screen, the only problem is it doesnt count and display the number of binary 255s, it only goes up to 254!!

    Anyone able to give me a clue of why its doing this??

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I believe you should replace 262143 with 262144 everywhere.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    a #define ARRAY_SIZE 262143 would make robatino's solution much simpler to manipulate; just change 262143 to 262144 in that case.

    Don't forget to close the file stream.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by robatino
    I believe you should replace 262143 with 262144 everywhere.

    Hi thanks for the reply, ive tried doing this, but all this does is add another 0 to the array so that an extra 0 is counted, it still only goes up to 254

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > if(number==numarray[i])
    In order to print out the number of 255's, you have to find an entry which has 256 in it (to cause the else to happen).
    This of course isn't going to happen.

    You need to refine you logic to print out the final stat when you get to the end of your array.

    > ifstream file("Binary File", ios::in||ios::binary);
    Note that there is a world-o-difference between what you've written and what it should be
    ifstream file("Binary File", ios::in|ios::binary); // one |, not two ||
    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.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by Salem
    > if(number==numarray[i])
    In order to print out the number of 255's, you have to find an entry which has 256 in it (to cause the else to happen).
    This of course isn't going to happen.

    You need to refine you logic to print out the final stat when you get to the end of your array.
    AAh i thought this was going to be the answer :-(, think it will be possible to add a bit on at the end just to count the 255s?


    Quote Originally Posted by Salem
    > ifstream file("Binary File", ios::in||ios::binary);
    Note that there is a world-o-difference between what you've written and what it should be
    ifstream file("Binary File", ios::in|ios::binary); // one |, not two ||
    thanks never noticed that

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I'm not familiar with file I/O, just noticed that 26214(3,4) is just a "large enough" buffer size. Isn't there a better way to allocate the buffer based on the size of the file, so you don't have to assume the file is "small enough"?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> a #define ARRAY_SIZE 262143 would make robatino's solution much simpler to manipulate
    Use const int instead.

    >> Don't forget to close the file stream.
    The file stream is already being closed in the original code.

    BTW, there is another way to do this that does not require you to read the entire file into a huge array. I think this other method might be actually better. Think about how you'd accomplish this task if you had to keep track of the counts as you read in the file, instead of reading in the file first and then doing the counting.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by Daved

    BTW, there is another way to do this that does not require you to read the entire file into a huge array. I think this other method might be actually better. Think about how you'd accomplish this task if you had to keep track of the counts as you read in the file, instead of reading in the file first and then doing the counting.
    would another way to possibly make the binary number equal to the index number of the array and add add one to each index??

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That sounds like what I was thinking, but your terminology is a little different than how I would phrase it. If you can give a little bit of code or a more thorough explanation of your plan for that method then I could confirm or deny.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by Daved
    That sounds like what I was thinking, but your terminology is a little different than how I would phrase it. If you can give a little bit of code or a more thorough explanation of your plan for that method then I could confirm or deny.
    i cracked it man thanks for your help!

    i made the binary number = index of the array
    then incremented the array

    so if a binary 6 was read in

    i=6
    array[i]++

    array would now have counted one 6

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Sounds good. That is how I would have done it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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