Finding number of occurences in a list of numbers
-
hey guys, I was wondering what the most efficient way of finding the number of occurences in a list is. Say I have a list.... struct data{ int number; int occurences; }; data list[7]; list[0].number = 5; list[1].number = 6; list[2].number = 6; list[3].number = 8; list[4].number = 9; list[5].number = 9; list[6].number = 9; how would i sort in such a way that I eliminate the numbers that occur more than once like this, list[0].number = 5; list[1].number = 6; list[2].number = 8; list[3].number = 9; and saves the number of occurence for each number list[0].occurences = 1; list[1].occurences = 2; list[2].occurences = 1; list[3].occurences = 3; I thought of every possible way, I can do it for numbers that appear twice but three times, i duno how to deal with it..... Someone please help me out :doh::doh::doh::doh::doh::doh::confused::confused::confused:
-
hey guys, I was wondering what the most efficient way of finding the number of occurences in a list is. Say I have a list.... struct data{ int number; int occurences; }; data list[7]; list[0].number = 5; list[1].number = 6; list[2].number = 6; list[3].number = 8; list[4].number = 9; list[5].number = 9; list[6].number = 9; how would i sort in such a way that I eliminate the numbers that occur more than once like this, list[0].number = 5; list[1].number = 6; list[2].number = 8; list[3].number = 9; and saves the number of occurence for each number list[0].occurences = 1; list[1].occurences = 2; list[2].occurences = 1; list[3].occurences = 3; I thought of every possible way, I can do it for numbers that appear twice but three times, i duno how to deal with it..... Someone please help me out :doh::doh::doh::doh::doh::doh::confused::confused::confused:
-
hey guys, I was wondering what the most efficient way of finding the number of occurences in a list is. Say I have a list.... struct data{ int number; int occurences; }; data list[7]; list[0].number = 5; list[1].number = 6; list[2].number = 6; list[3].number = 8; list[4].number = 9; list[5].number = 9; list[6].number = 9; how would i sort in such a way that I eliminate the numbers that occur more than once like this, list[0].number = 5; list[1].number = 6; list[2].number = 8; list[3].number = 9; and saves the number of occurence for each number list[0].occurences = 1; list[1].occurences = 2; list[2].occurences = 1; list[3].occurences = 3; I thought of every possible way, I can do it for numbers that appear twice but three times, i duno how to deal with it..... Someone please help me out :doh::doh::doh::doh::doh::doh::confused::confused::confused:
If you can use an STL container, do so and then you can use the
sort
andunique
methods.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
hey guys, I was wondering what the most efficient way of finding the number of occurences in a list is. Say I have a list.... struct data{ int number; int occurences; }; data list[7]; list[0].number = 5; list[1].number = 6; list[2].number = 6; list[3].number = 8; list[4].number = 9; list[5].number = 9; list[6].number = 9; how would i sort in such a way that I eliminate the numbers that occur more than once like this, list[0].number = 5; list[1].number = 6; list[2].number = 8; list[3].number = 9; and saves the number of occurence for each number list[0].occurences = 1; list[1].occurences = 2; list[2].occurences = 1; list[3].occurences = 3; I thought of every possible way, I can do it for numbers that appear twice but three times, i duno how to deal with it..... Someone please help me out :doh::doh::doh::doh::doh::doh::confused::confused::confused:
Make a new array for the initial list of numbers then use a for loop to start adding the numbers to the array, use an embedded for loop to check whether the number has already been added.
for(int i=0; l <7; i++) { list[i].number = 0; list[i].occurences = 0; } int ListIndex = 0; for(i=0; i <7; i++) { int index = i; bool IsRepeat = false; for(int j=0; j <7; j++) { if(array[index] == list[j].number) { IsRepeat = true; list[j].occurences++; } } if(!IsRepeat) { list[ListIndex].number = array[i]; list[ListIndex].occurences = 1; ListIndex++; } }
I just stuck that togeather now, if it don't work, then don't complain about it ;P -- modified at 6:27 Friday 10th November, 2006 The code does work, im so happy :) -
Why did you use the name 'list' for a variable which is an array ? Could you post your current algorithm to correct it ?
Am i missing soemthing? Why not call an array a list, thats basically what it is.
-
Am i missing soemthing? Why not call an array a list, thats basically what it is.
The Undefeated wrote:
Why not call an array a list, thats basically what it is.
I'd like to be shown where 'Array' publically derives from ("is-a") 'List'. ;) Also, in C++, 'Array' has the distinct connotation of 'contiguous memory'. A 'List' has definitly not. If he would have called his
std:vector
'Array'... While on a very zoomed-out, global point of wiew you are right, for a beginner it is definitely better to separate the concepts of the two container types.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.
-
Make a new array for the initial list of numbers then use a for loop to start adding the numbers to the array, use an embedded for loop to check whether the number has already been added.
for(int i=0; l <7; i++) { list[i].number = 0; list[i].occurences = 0; } int ListIndex = 0; for(i=0; i <7; i++) { int index = i; bool IsRepeat = false; for(int j=0; j <7; j++) { if(array[index] == list[j].number) { IsRepeat = true; list[j].occurences++; } } if(!IsRepeat) { list[ListIndex].number = array[i]; list[ListIndex].occurences = 1; ListIndex++; } }
I just stuck that togeather now, if it don't work, then don't complain about it ;P -- modified at 6:27 Friday 10th November, 2006 The code does work, im so happy :) -
thanks, thats great i got a bit confused when I first saw your code, but....... it works!!! THANKS !!!!!!! :-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:rose::rose:
Just so long as you make sure you understand it, all will be good ;)