### Reading in integers to an array

I cant figure out why my read in array wont work :/
This is a predetermined lab and so its a bit weirder to work with. Dont i need an int number so that i can read it in from the file thats hardcoded into this lab program?

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````/* Problem description: Write a program to read a huge integer value with 50 digits, find the digit that appears most frequently in the huge integer, and print the digit and the number of its occurrences. If two digits appear the same amount of times in the huge integer, output the larger digit. */ #include #include #include using namespace std; int main( ) { const int DigitNum = 50; //array capacity int hugeInt[DigitNum]; //array holding the huge integer int frequency[10]; // occurrence frequency of each digit 0..9 // i.e. frequency[0] the number of times 0 appears in hugeInt // i.e. frequency[1] the number of times 1 appears in hugeInt // ... // i.e. frequency[9] the number of times 9 appears in hugeInt char digit; // holding current digit during the reading int freqIndex; //holding the digit occuring most frequently //read digits in the huge integer value into the array hugeInt //read digits in the huge integer value into the array hugeInt for( int i=0; i> hugeInt[i]; }``````
Last edited on
bummmp
As you read the numbers, increment the index of each number using the frequency array to do this:

`int frequency[10] = {0}; // initialise to zero `

 ``323334`` ``````// in the for-loop cin >> hugeInt[i]; frequency[hugeInt[i]]++;``````

 ``123456789`` ``````//later int max_value(-1), max_count(0); for (int v = 0; v < 10; ++v) { if (frequency[v] > max_count) { // do something here } }``````
Thanks, cant say i understand it. so youre basically incrementing the the position INSIDE of the array. But when i compile that it gives me an endless loop problem?
Last edited on
How can the following be an endless loop?

 ``123456`` ``````for (int v = 0; v < 10; ++v) if (frequency[v] > max_count) { // do something here } }``````

Unless you're doing something silly to change the value of v within the "Do something here" bit? Assuming you're not, then v will simply increment from 0 to 10, at which point it will fail the condition and the loop will exit.

frequency[] is storing the number of times each number occurs. So frequency[0] stores the number of times '0' occurs, frequency[1] stores the number of times '1' occurs, and so on.
Topic archived. No new replies allowed.