Counting the repetition in an array

Hello friends, I need your kind help in the following:

I've a program that takes, the following:

3.25 2.54 2.00 2.54 3.25
2.54 1.70 1.00 1.70 2.54
2.00 100 0.00 1.00 2.00
2.54 1.70 1.00 1.70 2.54
3.25 2.54 2.00 2.54 3.25

and transfer it to

0.00
1.00
1.70
2.00
2.54
3.25

BUT I NEED IT TO CALCULATE THE "REPETITION AND ACCUMULATE", SO THE RESULT SHOULD LOOK LIKE:

0.00 0
1.00 4
1.70 8
2.00 12
2.54 20
3.25 25

I'll attach part of the program that does the sorting part and needs to be modified to look like the two columns above
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  vector<double> my;
    //vector<double> my1;
	for(i=0; i<npixels; i++){
		my.push_back(Final_T[i]);
	}
	//----------------
	myfile20 << setw(16)   ;
	for (int k=1; k<nz+1;k++){
		for (int j=1; j<ny+1;j++){
			for (int i=1; i<nx+1;i++){ 
				index= (k-1) * nx * ny +(j-1) * (nx) + (i-1) ;
				/*if(abs(dp_loc[index])>Pi){
				dp_loc[index]=Pi;
				}*/

				myfile20 <<Final_T[index]<<setw(16) ;
				//myfile20 <<velocityx[index]<< setw(16)   ;
				// save fine scale time
			if(Final_T[index]<threshold)
			{
					count_f += porosity[index]*dx[index]*dy[index]*dz[index];
                    //count_file<<count_f<<endl;
				}
			}
			myfile20 << endl  ;
		}
	}

	double  val,result, tmp, tmp1=0.0, ri=0, tc;
	int count=0;
	vector<double> t_;
	vector<double> Vp,Vp2;		
	//vector<double> ri;
	vector<double> volume;
	int jj=0;

	sort(my.begin(), my.end());

	val = my.at(0);

	//write in to gird.txt
	for(i=0;i<npixels;i++)
	{

		if(val < my.at(i))

		{
			grid<<my.at(i)<<endl;
			grid2<<my.at(i)*my.at(i)/4<<endl;
			t_final=my.at(i);
Last edited on
Let me know if you need the whole program
I honestly don't understand anything you said.
We've these values in following matrix:

3.25 2.54 2.00 2.54 3.25
2.54 1.70 1.00 1.70 2.54
2.00 100 0.00 1.00 2.00
2.54 1.70 1.00 1.70 2.54
3.25 2.54 2.00 2.54 3.25

and I need the program to print the result in the following format:

Values ----> #of repetition (cumulative)

Example:
from the matrix we've above the program should print the results as:

Values ----> #of repetition (cumulative)
0.00 ----> 0
1.00 ----> 4
1.70 ----> 8
2.00 ----> 12
2.54 ----> 20
3.25 ----> 25

I hope it's clear

You already know that you have to sort it. Do you know why?

    2 7 3 7 9 1 7 2 --> 1 2 2 3 7 7 7 9

How many 1's are there?
How many 2's are there?

The repetition count is one less than the number of items there are.

Sort first. Count repetitions second.

Hope this helps.
I did sort, as shown in the program above but I do not know how to have the repetition included, would you please check my program above and indicate change needed

Last edited on
Sort first.
Count repetitions second.
Pay attention.
Thanks for the information, I would like somebody to suggest the changes to the code above based on his understanding
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
36
37
38
39
40
41
42
43
double  val,result, tmp, tmp1=0.0, ri=0, tc;
	int count=0;
	vector<double> t_;
    vector<int> cumsort_1;
	vector<double> Vp,Vp2;		
	//vector<double> ri;
	vector<double> volume;
	int jj=0;
	//vector<double> my2 = my;

	sort(my.begin(), my.end());
	int ad_counter = 1;
	val = my.at(0);
//------------------------------------------------------
	for (int i=0; i<npixels; i++)
	{
		if(val <= my.at(i))
		//int ad_counter = 1;
		{
			double w = my.at(i+1) - my.at(i);
		while(  w == 0 )
		{
			ad_counter++;
			
			}
			cumsort_1.push_back(ad_counter);
			grid1<<cumsort_1[i]<<endl;
		}
	}
	//for (int i=0; i<npixels; i++)
		//grid1<<cumsort_1[i]<<endl;

//-------------------------------------------------
	//write in to gird.txt
	for(i=0;i<npixels;i++)
	{
		
			if(val <= my.at(i))

		{
			grid<<my.at(i)<<endl;
			grid2<<my.at(i)*my.at(i)/4<<endl;
			t_final=my.at(i);
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
#include <vector>
#include <map>
#include <iostream>
#include <iomanip>

int main()
{
    std::vector<double> vec = 
    {
        3.25, 2.54, 2.00, 2.54, 3.25,
        2.54, 1.70, 1.00, 1.70, 2.54,
        2.00, 1.00, 0.00, 1.00, 2.00,
        2.54, 1.70, 1.00, 1.70, 2.54,
        3.25, 2.54, 2.00, 2.54, 3.25
    };
    
    std::map<double, int> map;
    
    for (auto n : vec) {
        map[n]++;
    }
    
    for (auto p : map) {
        std::cout << std::fixed << std::setprecision(3)
                  << p.first << " occurs " << p.second << " times\n";
    }
}


http://coliru.stacked-crooked.com/a/c444296bdc045087
Thanks, I tried something like this but I do not get the results I'm looking for ? Any Idea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vector<double> my;
double  val, az=0.0;
int ad_counter = 1;
 //vector<int> repcount;
	
sort(my.begin(), my.end());
val = my.at(0);

for(i=0;i<npixels;i++)                //size=npixel=2500
	{
		 if(val <= my.at(i))       //the condition to have the number sorted increasingly
			{
				if ((my.at(i-1)-my.at(i))==0);  // the condition for counting the occurrence 
						ad_counter++;
						
   grid1<<my.at(i)<<endl;
}  
		 return 0 ;
You are missing something vital that Duoas was trying to point out. Look at this again, what do you notice about the values of the sorted list?

2 7 3 7 9 1 7 2 --> 1 2 2 3 7 7 7 9

Iterating from left to right, how would you count the number of occurrences of each value?

Hint: Use a while loop (While a == b ... increment counter)
Thanks, I did it using the following:

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
vector<double> unique_values;
	vector<int> vcount;
	bool valueFound;
	for(i=0;i<npixels;i++)
	{
		if(val <= my.at(i))
		{
			valueFound = false;
			for (j = 0; j < unique_values.size(); j++)
			{
				if (my[i] == unique_values[j]&& my[i]!=0 )
				{
					vcount[j]++;
					valueFound = true;
					break;
				}
			}
			if (valueFound == false) //cannot find the value in unique_values, so now add it to unique_values
			{
				unique_values.push_back(my[i]);
				vcount.push_back(1);
			}
		   //grid<<my.at(i)<<endl;
		   grid1<<my.at(i)<<endl;
		}  

Topic archived. No new replies allowed.