Reading an integer vs a character

Today i had my final exam in computer science.
I did great , but i got a question regarding the last exercise.
It was something like this :
A sub-number is a two digit number of another number.
Example: 21 is a sub-number of 2145, 3215 , 21 etc. but not of 123.

In a file you got at most 1000000 numbers between [10,10^9] and you need to output the sub-numbers that appear the most in them.
And it need to be time efficient.
Example:
If the file contains:
393 17775787 72194942 12121774
Then the answer is (not necessary in this order): 77 21 because they both appear 3 times.

Everybody read every number , and then made every possible sub-number of it. And in an array v[100] they would keep the frequency.(v[77]=3. v[21]=3. v[94]=2)

I did something like this but using a bidimensional array T[10][10] where for example T[1][3] means the frequency of the number 13.
I read a character before named c1 and then using while(f>>noskipws>>c2) the second(c2) and at every step c1c2 will be my sub-number. I read using noskipws so i can mark off every number asking if( c1!=' ' and c2!=' ').

My question is Which is more efficient:
Reading an integer or a character?
I mean what happens when an integer is read in the background?
I belive it reads every possible character and builds the number untill it reaches space but i am not sure.

If that's so then my sollution should be far more efficient because for example for number 123456789 when they read it as an integer in the background will run the same procces that i did, reading charcacter by charcater but in addition they will destroy the number so they can form the sub-numbers resulting in 9 more steps. (123456789-12345678-1234567-....12)

I made an account just to ask this because i am very nervous. The results will not appear untill monday :(
Last edited on
"Which approach is more efficient" is virtually impossible to answer (unless one of the approaches is clearly inefficient). Anything anyone here will tell you will be a guess, and will not reflect reality.

The only way to know for sure which is faster is to try them both and measure how long they each take.

Some things I will say:

- Reading a character is generally going to be faster than reading an integer, because the input data is in text... so reading to a character requires no conversion (just pull the character from the file), whereas reading an integer requires conversion.

- 2D arrays are not going to be any faster than 1D arrays and may be slower.



If I had to guess, I would think your approach would be ever-so-slightly faster, but not by much. But again, that's a total guess.
I see.
But in conclusion, both solutions are linear( O(n) ) and efficient,right?
So(of course if the entire solution is corect) I should get maximum points like others that used the other aproach(that's also linear) ?
Last edited on
But in conclusion, both solutions are linear( O(n) )


Yes.

and efficient,right?


I'd say yes. I can't imagine there'd be a very big performance difference between the two. But again, the only way to really know for sure is to try it and see.

So(of course if the entire solution is corect) I should get maximum points like others that used the other aproach(that's also linear) ?


Depends on your teacher. As long as the code is clear, produces the correct output, and doesn't take exceedingly long, I don't see why you wouldn't.
Heh i got maximuuum points( 100 p) !!!
Topic archived. No new replies allowed.