Im having difficulties in performing the frequency distribution of 6x6 matrix which was given..i solved it by the following way but it was wrong anyway because if i change any number in the matrix it will not calculate the number of times it occurs in the array.... need your kind guidance as im still a beginner...

#include <iostream>
using namespace std;
int main()


    {#include <iostream>
    using namespace std;
    int main()
    {
    int M[6][6]=    {{3,14,4,10,3,6},
                    {5,10,3,8,3,3},
                    {8,3,3,8,-3,3},
                    {10,10,-3,3,6,-3},
                    {-3,-3,6,8,5,5},
                    {6,5,5,8,5,5}};

    int n0 = -3;
    int n1 = 3;
    int n2 = 4;
    int n3 = 5;
    int n4 = 6;
    int n5 = 8;
    int n6 = 10;
    int n7 = 14;
    int count = 0;
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    int count4 = 0;
    int count5 = 0;
    int count6 = 0;
    int count7 = 0;

    for (int i = 0; i<6; i++)
    {
    for (int j = 0; j <6; j++)
    {
    if (n0 == M[i][j])
    {
    count++;
    }
    if (n1 == M[i][j])
    {
    count1++;
    }
    if (n2 == M[i][j])
    {
    count2++;
    }
    if (n3 == M[i][j])
    {
    count3++;
    }
    if (n4 == M[i][j])
    {
    count4++;
    }
    if (n5 == M[i][j])
    {
    count5++;
    }
    if (n6 == M[i][j])
    {
    count6++;
    }
    if (n7 == M[i][j])
    {
    count7++;
    }
    }
    }
    cout << n0 << " ---> " <<count << endl;
    cout << n1 << " ---> " <<count1 << endl;
    cout << n2 << " ---> " <<count2 << endl;
    cout << n3 << " ---> " <<count3 << endl;
    cout << n4 << " ---> " <<count4 << endl;
    cout << n5 << " ---> " <<count5 << endl;
    cout << n6 << " ---> " <<count6 << endl;
    cout << n7 << " ---> " <<count7 << endl;

    return 0;
    }

Edited 3 Years Ago by Dani: Formatting fixed

Hmm, so you want to count the amount of times an integer is present in a matrix without knowing upfront what integers can be in there? I don't think there's a very beginner-friendly solution for this. I'd probably use a map for it. Below an example:

#include <iostream>
#include <map>
#include <iomanip>

using namespace std;

int main()
{
    const unsigned int DIMENSION = 6;

    int M[][DIMENSION]= {{3,14,4,10,3,6},
                         {5,10,3,8,3,3},
                         {8,3,3,8,-3,3},
                         {10,10,-3,3,6,-3},
                         {-3,-3,6,8,5,5},
                         {6,5,5,8,5,5}};

    map<int, unsigned int> occurrences;

    // Process the matrix.
    for (int i = 0; i < 6; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            occurrences[M[i][j]]++;
        }
    }

    // Show the results:
    for (map<int, unsigned int>::iterator it = occurrences.begin(); it != occurrences.end(); ++it)
    {
        cout << setw(3) << (*it).first << " ---> " << (*it).second << endl;
    }

    return 0;
}

1.

If you know the range of values in the integer matrix 'm', say the values range form 10 to 1009
then you could create an array to hold the counts for each value

// initial all values to 0
int freq[1000] = {0};

//then you could run though the matrix
//and ...
++ freq[ m[i][j]-10 ];

/*

for each i, j in the nested inner loop

then skipping on 0 values in the freq array

just report the freq each value (index goes from 0, 999 but values are (index +10) )

*/

2.
Another way would be to use a struct that holds the value and the freq of that value
and a vector to hold the structs

the first time a values occurs (that is, that value is not already in the vector of struct)
push_back a struct with that value and the count = 1

every time an element in the matrix is aleady in the vector of struct, just ++ the count

Hi.. need your favor again.. can someone advise me on my intial coding in which i could run the same "for, if" without initializing the fixed numbers in the array... because if i initialize it any new number entered will not be available... im restricted to use any advance methods such as the map, iterators, algorithm, pointers, etc.. thanking you in advance...

The question is: Do you know what numbers can be stored in your matrix?

If yes, you can, as David said, use an array to store the frequency (or two arrays, one each for negative and nonnegative integers). Simply use the integer stored in the matrix as the index in your frequency array.

If no, how do you expect to cover everything than can be stored?

can you provide me the programme if the numbers are

{{3,14,4,10,3,6,},
{5,10,3,8,3,3},
{8,3,3,8,-3,3},
{10,10,-3,3,6,-3},
{-3,-3,6,8,5,5},
{6,5,5,8,5,5}}

as im still unsure of your explanation...

Let's say you limit your integers to be stored from -15 to 15 (31 numbers in all):
You create an array int freq[31]
You set all values in freq to 0 memset(freq, 0, sizeof(freq))
You create another array which points to the middle of freq for easy index substitution int* mid = freq + 15

Now all you have to do is: for every M[i][j], you just have to increment the corresponding mid value mid[M[i][j]]++. After all cells have been accounted for, output your mid array using index -15 to 15.

IF you do not know the limits of integers that can be stored in your matrix, perhaps it's better to use vector or struct, as David said. You'll have to sort your frequency counts if you want them to be in order for output.

Edited 3 Years Ago by scudzilla

i did this programming.. just want to know why does the negative number in the set of array not displayed?? can you point out where is my mistake???

#include<iostream>
using namespace std;

int main()
{

    int a[100] =    {3,14,4,10,3,6,
                    5,10,3,8,3,3,
                    8,3,3,8,-3,3,
                    10,10,-3,3,6,-3,
                    -3,-3,6,8,5,5,
                    6,5,5,8,5,5};;

    int count[60] = {0};
    int i;

    for(i = 0; i< 59; i++)
        count[a[i]]++;

    for(i = 1; i<59 ; i++)
    {
        if(count[i]==1) cout<<i<<"----->"<<"one time"<<endl;
        else if(count[i]!=0)
        cout<<i<<"----->"<<count[i]<<" times"<<endl;

    }
    return 0; 
}

Ahh, dangerous program right there. Based on line 18, you are editing memory locations that are possibly outside of your program's allocation boundaries.

If you must use a single array for your frequency, adjust the index so that negative numbers will be included. For example, you can assign count[30] to be the counter for 0, and start incrementing the index from that.

for(i = 0; i < 59; i++)
    count[a[i]+30]++;
.
.
.
    if(count[i]==1) cout << i-30 <<"----->"<<"one time"<<endl;
    else if(count[i]!=0)
        cout<<i-30<<"----->"<<count[i]<<" times"<<endl; 

Once again, you didn't mention what integers can be stored in your matrix. For this code, an integer greater than 29 or less than -30 will probably cross the allocation boundaries.

Edited 3 Years Ago by scudzilla

thank you so much!! need another favor.. how if im suppose to change this whole coding to 2D... will that be very much different?? pls correct my mistakes, i knew that i cant just add a "j" for every line.. but i cant imagine the loop in 2D ... pls advice...

#include<iostream>
using namespace std;
int main()
{
    int a[100][100] =   {{3,14,4,10,3,6,},
                        {5,10,3,8,3,3},
                        {8,3,3,8,-3,3},
                        {10,10,-3,3,6,-3},
                        {-3,-3,6,8,5,5},
                        {6,5,5,8,5,5}}; 

    int count[60][60] = {{0},{0}};
    int i,j;

    for(i = 0; i< 59; i++)
       for (j=0; j<59; j++)

       count[a[i][j]+30]++;

    for(i = 0; i<59 ; i++)
    {
    for (j=0; j<59; j++)
        if(count[i][j]==1) cout<<i-30<<"-----> "<<"one time"<<endl;
        else if(count[i]!=0)

        cout<<i-30<<"-----> "<<count[i]<< " times"<<endl;
    }
    return 0; 
}

This method only works for counting the frequency of int's
(or objects that can be mapped uniquely onto int's)

NOTE:

Your array to hold the frequency of the occurence of some value is going to be a 1 D array!
(SINCE you are mapping the values in your matrix, of any dimension, to the index of an int array.

THUS, you map ...

lowest_value_in_matrix -> 0 INDEX
highest_val_in_matrx -> highest_val_in_matrix - lowest_value_in_matrix

for example ...

if in your multi-D matrix, the int values range from -1 to 10
you would need an array of int initialized all to zero to count possible incidences of values
mapped onto the range of indicies from ...
0..11

-1 -> 0
0 -> 1
...
10 -> 11

so ... declare
int ary[12] = {0};

then when done traversing the int matrix ... and updating the int array, for the frequency of the values in the matrix ...

the count stored at ary[11] represents the number of occurances of the value 10
...
the count stored at ary[0] represents the number of occurances of the value -1

Edited 3 Years Ago by David W: corrected spelling

This article has been dead for over six months. Start a new discussion instead.