counters

I need help with the counter. I am trying to count how many people are friends with the person. I'm not sure where I can put it to get the right results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void friendData(int numP, char names[][4], int table[][MAX_PEOPLE])
{
    int counter = 0;
    for(int i = 0; i < numP; i++)
    {

        cout << names[i] << " (" << counter << "): ";
        counter = 0;
        for(int k = 0; k < numP; k++)
        {
            if(table[i][k] == 1)
            {
                counter++;
                cout << names[k] << " ";
            }
        }
        cout << endl;
    }
}

output
Ami (0): Leo Sue
Ann (2): Luc Meg
Ben (2): Dan Luc
Ema (2):
Ira (0): Guy
and so on.

desired output
Ami (2): Leo Sue
Ann (2): Luc Meg
Ben (2): Dan Luc
Dan (0):
Luc (1): Leo
Last edited on
Try putting the counter on line 6, and initializing it to zero then (start the counter again for each person).
so I was able to reset it everytime but i need to shift the counter up one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void friendData(int numP, char names [][4], int table [][MAX_PEOPLE])
{
    for (unsigned i = 0; i < numP; ++i)
    {
        // count the number of friends.
        unsigned friendCount = 0;
        for (unsigned j = 0; j < numP; ++j)
            if (table[i][j] == 1)
                ++friendCount;

        // do the output.
        std::cout << names[i] << " (" << friendCount << "): ";
        for (unsigned j = 0; j < numP; ++j)
            if (table[i][j] == 1)
                std::cout << names[j] << " ";

        std::cout << '\n';
    }
}
thanks cire
Topic archived. No new replies allowed.