Question:
how do i count numbers in file? c++?
Cornelius
2010-02-27 15:17:58 UTC
i need to use an array to count numbers and tell the occurences of them but all im getting is large numbers
Three answers:
Mark aka jack573
2010-03-02 21:07:11 UTC
From what I can see:



1. You are using the variable i for the loop, but then you are using variable index for the array.



You have not initialized index to be a number.



for(int i =0; i < 2000; i++)

{

cout << i << array[i] << endl;

}

if(array[index] == 0)

{

c0++;

}



So, I would change these 2 lines from:

for(int i =0; i < 2000; i++)

{

cout << i << array[i] << endl;

to

for(index =0; index < 2000; index++)

{

cout << i << array[index] << endl;



That should solve 1 problem.





Another problem is, are you sure there are 2000 entries in the file for the array?

Basically, you would be checking all 2000, even if the file only contained 2. So the count would be off.

See the next problem as well.



Next problem. You are not getting the information from the file and putting it in the array. At the end of the file, if the contents have not been 2000, then you could put in a value that says it is the end of the data. This could be -1, or something. Then if you find -1 you break out of the loop (using the break; statement.



When you read the contents of the file into the array, you need to make sure you are only putting 1 digit into each spot of the array. if you don't do this, then you may have 10 as the value and this would not count as either a 1 or a 0.



Also, you can use if - else if statements instead of just if, if, if, etc.

if(array[index] == 0)

{

c0++;

}

else if(array[index] == 1)

{

c1++;

}

else if(array[index] == 2)



You may want to use a switch statement instead if you have learnt them.





Hope that helps.
?
2016-11-09 12:02:45 UTC
Awesooooome!!! it quite is the main inefficient implementation ever! Given your implementation, you do not might desire to envision something to count selection the characters, in basic terms use fseek() to bypass to the top of the document, and then get the area employing ftell(). BTW, conio.h is a non-known header, you could steer away from it, as this is basically obtainable on some DOS and homestead windows SDKs, use getchar() quite of getch(). the different issue on your application is which you at the instant are not interpreting something, you're passing the pointer infile to countchar, so this is interpreting from a random reminiscence section... attempt another compiler+IDE, that properly comments the errors on your code, i % to propose Code::Blocks + MinGW, or bypass without delay to GNU/Linux the place are the final progression equipment. For counting the words/lines, you will possibly be able to desire to optimize it via interpreting via 4/8 bytes (according to while you're on x86 or x86_64) till the top of the string, the place you will possibly be able to desire to previously write zeros till it gets aligned (after the examine). simply by fact x86 and x86_64 are little-endian architectures, you basically learn the low 8bits, shift via 8bits, learn back... it is all, this is lots speedier :). additionally you will possibly be able to desire to apply XOR to discover the areas on the entire sign in, that could desire to strengthen the overall performance slightly greater, or perhaps use quite expert SIMD training (yet it is going too a techniques XD). i can't comprehend why anybody talks approximately "a million byte = a million char", particularly while no person makes use of 7bit ASCII or any 8bit encodings anymore. This implementation isn't properly suited with UTF-8, to make it properly suited you will possibly be able to desire to verify the lenght of each char.
Danish
2010-02-27 15:29:38 UTC
What exactly does the file contain? like space separated numbers?

in what way are you supposed to use the array? to read the numbers? can you put in your program, it'd be easier to debug it than write it afresh :|


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...