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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
  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 <iostream>
#include <string>
#include <fstream>
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<DigitNum; i++ )
{
        cin >> 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

32
33
34
// in the for-loop
cin >> hugeInt[i];
frequency[hugeInt[i]]++;


1
2
3
4
5
6
7
8
9
//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?

1
2
3
4
5
6
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.