### 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
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` `````` vector my; //vector my1; for(i=0; iPi){ dp_loc[index]=Pi; }*/ myfile20 < t_; vector Vp,Vp2; //vector ri; vector volume; int jj=0; sort(my.begin(), my.end()); val = my.at(0); //write in to gird.txt for(i=0;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
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````double val,result, tmp, tmp1=0.0, ri=0, tc; int count=0; vector t_; vector cumsort_1; vector Vp,Vp2; //vector ri; vector volume; int jj=0; //vector my2 = my; sort(my.begin(), my.end()); int ad_counter = 1; val = my.at(0); //------------------------------------------------------ for (int i=0; i
 ``123456789101112131415161718192021222324252627`` ``````#include #include #include #include int main() { std::vector 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 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
 ``123456789101112131415161718`` ``````vector my; double val, az=0.0; int ad_counter = 1; //vector repcount; sort(my.begin(), my.end()); val = my.at(0); for(i=0;i
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:

 ``12345678910111213141516171819202122232425`` ``````vector unique_values; vector vcount; bool valueFound; for(i=0;i

Topic archived. No new replies allowed.