counting certain elements from a file
ok i know this might sound wierd, but the problem comes down to knowing how to do this ...and i just don't see how i could do it. maybe there's someone out there who can enlighten me :D
ok the question is something like this (hope i'll be clear enough:-s ):
consider a text file in which we have a binary number with a maximum of 1000000 digits. (that means i have a randome repetition of "1" and "0", no blanks). how do i count the number of zeros which follow the last "1"?
example: my number 10011100011100000, and the number of zeros is 5. oh and i have to do this without copying the information of the file into a variable...without using extra memory :D
thanks! much apreciated, guys.
uf tried something else i think the counting's wrong
Code:
#include <fstream.h>
#include<conio.h>
unsigned long int nr,n;
char a[1000000];
void main(){
fstream f("nrbinar.in", ios::in);
f>>n;
for(int i=0;i<n;i++){
if(a[i]=='0') nr++;
if(a[i]=='1') nr=0;
}
cout<<nr;
getch();
}
i've added n as the number of digits of the binary number...to simplify things :S
but for, lets say, a test file wherein the first line we have n and in the next line the n digit binary number
19
1000000010001111000
what he "cout"s is 0 which means that he never gets to the next digit or i++ of the array :confused: