Hey folks,

I am trying to normalize a view on a given output. This is how it works, I need to print several characters but the maximum chars i can print have to be 40 per line.

aaaaaaaaaaaaaaaaaaaaaaaaaaaaa...till 40
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbb

The algorithm works as follows:

I have to find the highest number on the array (letters[]) then if this number is higher than 40 then I have to scale it. scaleFactor = (40 / max_count) Then multiply each number on letters[] by the scaling factor so that when I print the letters they come up in a nice scale. This works flawlessly, however, There are some letters that do not print because there aren't many occurrences for them.

I would like to at least print these letters once to indicate that they are there.Further explanation:

scaleFactor = 0.002952

So if I have 8000 A's it normalizes to 23.16 which is fine
But if I have only 10 B's it normalized to 0.02952 so no B's get printed.

max_count = getMaxCount(letters);
if (max_count > 40) {
	float factor = max_count; // Convert to float
	scaleFactor = (40 / factor);	
	for(int i = 0; i < 26; i++) {	
		letters[i] = (letters[i] * scaleFactor);
	}
}

I managed to fix some of it. I created a condition to check if there are any occurrences that are less than one to just add one count to the array.

However! My only problem with this is... what if in fact that letter does not exist?

The following code checks if the letter exist and occures less then 40 times. Maybe this is what you want ?

if( lettes[i] > 0 && letters[i] < 40 ) 
letters[i] = 1;

No that doesn't quite do it. If anything

for(int i = 0; i < 26; i++) {
        letters[i] = (letters[i] * scaleFactor);
	if (letters[i] < 1)
		letters[i] = 1;


}

But that will even add one if there are no letters. I think the problem lies that is a float value while reading an integer.

Yeah I created a small float test = letters[1] (where B's are) just to see what value I was getting. And it's 0.00000 and should be 0.00344 or something similar depending on how many B's there are.

That's the problem, so in my condition when checking for > 0 it does not see it because it's 0.000. Damn

This is driving me insane :S

Ok it's done, this looks messy as HELL but it gets to job done. If you guys think of an easier way to do this please do share.

float temp;
	int tempInt;
	/**Normalized histogram */
	max_count = getMaxCount(letters);
	if (max_count > 40) {
		float factor = max_count; // Convert to float
		scaleFactor = (40 / factor);	
		for(int i = 0; i < 26; i++) {
			tempInt = letters[i];
			temp = tempInt * scaleFactor;
			if (temp < 1 && temp != 0){
				temp = 1;
				letters[i] = 1;
			}
			else {
				letters[i] = letters[i] * scaleFactor;
			}
			
		}
	}

<Sorry, my ISP is acting up. Please delete this>

Your problem is that your scale was constant. Stats charts have this problem, and when they do, they use a logarithmic scale to solve it.

No, I don't have code for it, but Google surely will have examples, this is very common.

Yes but I do not have control over it, my scale will always be constant. Logarithmic scale? Interesting, I'll look into this but I'm happy I got my code to work, I've cleaned it up quite a lot so now it looks like a simple function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.