I am creating graph i get 5 values at run time so how should i know which is greater so that i may set max limit
I have following 5 varibale of values;
int value3=(int)roundf(appDelegate.itemOneValue);
int value4=(int)roundf(appDelegate.itemTwoValue);
int value5=(int)roundf(appDelegate.itemThreeValue);
int value6=(int)roundf(appDelegate.itemFourValue);
int value7=(int)roundf(appDelegate.itemFiveValue);
how to check of the above varible which has larger value.
NSArray *allValues = #[[NSNumber numberWithInt:(int)roundf(appDelegate.itemOneValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemTwoValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemThreeValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemFourValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemFiveValue)]];
int max = [[allValues valueForKeyPath:#"#max.intValue"] intValue];
or
NSNumber * max = [allValues valueForKeyPath:#"#max.intValue"];
An alternative that is less flexible but may be easier to understand:
int max = (value2 < value1) value2 : value1;
max = (value3 < max) value3 : max;
max = (value4 < max) value4 : max;
max = (value5 < max) value5 : max;
max = (value6 < max) value6 : max;
max = (value7 < max) value7 : max;
// now max contains the highest of all values.
You can read a great article on wikipedia: http://en.wikipedia.org/wiki/Selection_algorithm
Here the pseudo-code to get min/max from an array:
function select(list[1..n], k)
for i from 1 to k
minIndex = i
minValue = list[i]
for j from i+1 to n
if list[j] < minValue
minIndex = j
minValue = list[j]
swap list[i] and list[minIndex]
return list[k]
Algorithm for max and min? (Objective-C)
http://en.wikipedia.org/wiki/Maxima_and_minima
How would you define a simple "min" method in obj-c
http://goo.gl/BO5Qx
In objc, the pseudo code became something like this:
int value3=(int)roundf(appDelegate.itemOneValue);
int value4=(int)roundf(appDelegate.itemTwoValue);
int value5=(int)roundf(appDelegate.itemThreeValue);
int value6=(int)roundf(appDelegate.itemFourValue);
int value7=(int)roundf(appDelegate.itemFiveValue);
NSArray *values = #[[NSNumber numberWithInt:value3],
[NSNumber numberWithInt:value4],
[NSNumber numberWithInt:value5],
[NSNumber numberWithInt:value6],
[NSNumber numberWithInt:value7]];
NSNumber min=0;
NSNumber max=0;
for ( int i=0; i<[values count]; i++ ) {
min = [values objectAtIndex:i];
//etc...
for (int j=0; j<i+1; j++ ) {
if ( [values[j] > min] ) {
// etc..
}
}
}
hope this helps.
Related
I want to roundoff my number to greater value. for example if i have 234 i want to make it 300 and 4436 to 5000. I have tried it but i can roundoff my value not in hundred and thousants but in tens. like i have a value 7771 it roundoff to 7900 or 7800 but i want it in 8000.
int temp = lroundf([[arrayPercentage objectAtIndex:i]floatValue]);
maxPer = (((temp + 10)/10))*10;
How about this:
-(void)roundUpNumber:(NSInteger) num {
NSInteger numLength = [[NSString stringWithFormat:#"%ld",num] length];
NSInteger newNum = ceil(num/pow(10,numLength-1)) * pow(10,numLength -1);
NSLog(#"%ld",newNum);
}
int num, count = 0;
int originalNumber = 7771;
num = originalNumber;
while (num) {
num = num/10;
count ++;
}
int power = pow(10,(count -1));
int firstDigit = originalNumber / power;
int finalNumber = (firstDigit + 1)* power;
NSLog(#"final result : %d",finalNumber);
guys, I'm making simple graph drawer and want to find beautiful values for horizontal lines.
For example, if I have value equals to 72089.601562, beautiful is 70000, or 75000. So, I think that beautifulNumber%5 = 0.
Have you any ideas?
How about this?
#import <math.h>
#import <stdio.h>
#define ROUNDING 5000
int beautify(float input)
{
// Cast to int, losing the decimal value.
int value = (int)input;
value = (value / ROUNDING) * ROUNDING;
if ((int)input % ROUNDING > ROUNDING / 2 )
{
value += ROUNDING;
}
return value;
}
int main()
{
printf("%d\n", beautify(70000.601562)); // 70000
printf("%d\n", beautify(72089.601562)); // 70000
printf("%d\n", beautify(76089.601562)); // 75000
printf("%d\n", beautify(79089.601562)); // 80000
printf("%d\n", beautify(70000.601562)); // 70000
return 0;
}
It depends whether you want a floor value, a ceiling value or just to round to the nearest 5000.
For a floor value:
int beautiful = (int)(floor(ugly / 5000.0) * 5000.0);
For a ceiling value:
int beautiful = (int)(ceil(ugly / 5000.0) * 5000.0);
For rounding:
int beautiful = (int)(round(ugly / 5000.0) * 5000.0);
For making graph lines, I'd probably find the minimum and maximum values you have to graph, start with a floor value for the minimum value and then add your desired interval until you have surpassed your maximum value.
For instance:
float minValue = 2.34;
float maxValue = 7.72;
int interval = 1;
NSMutableArray *horizLines = [NSMutableArray array];
int line = (int)(floor(minValue / interval) * interval);
[horizLines addObject:[NSNumber numberWithInt:line]];
do {
line = (int)(ceil(minValue / interval) * interval);
[horizLines addObject:[NSNumber numberWithInt:line]];
if (minValue >= maxValue) break;
minValue = minValue + interval;
}
Use as needed!
Well, it seems like you'd want it to scale based on the size of the number. If the range only goes to 10, then obviously rounding to the nearest 5,000 doesn't make sense. There's probably a really elegant way to code it using bit shifting but just something like this will do the trick:
float value = 72089.601562
int beautiful = 0;
// EDIT to support returning a float for small numbers:
if (value < 0.2) beautiful = int(value*100)/100.;
else if (value < 2.) beautiful = int(value*10)/10.;
// Anything bigger is easy:
else if (value < 20) beautiful = (int)value;
else if (value < 200) beautiful = (int)value/10;
else if (value < 2000) beautiful = (int)value/100;
else if (value < 20000) beautiful = (int)value/1000;
// etc
Sounds like what you want to do is round to 1 or perhaps 2 significant digits. Rounding to n significant digits is pretty easy:
double roundToNDigits(double x, int n) {
double basis = pow(10.0, floor(log10(x)) - (n-1));
return basis * round(x / basis);
}
This will give you roundToNDigits(74518.7, 1) == 70000.0 and roundToNDigits(7628.54, 1) == 8000.00
If you want to round to 1 or 2 digits (only 2 where the second digit is 5), you want something like:
double roundSpecial(double x) {
double basis = pow(10.0, floor(log10(x))) / 2.0;
return basis * round(x / basis);
}
I am taking about 50 times as long as expected to loop through a simple assignment. My first reaction was that I had disordered my memory access in the arrays, resulting in cache misses. This doesn't seem the case, however.
The pixel value assignment and updating the arrays takes a dogs age. Do any one of you folks have an inclining as to why this is happening? (I am compiling for an iPod with an A4)
memset(columnSumsCurrentFrameA, 0, sizeof(unsigned int) * (_validImageWidth/numSubdivisions) );
memset(rowSumsCurrentFrameA, 0, sizeof(unsigned int) * (_validImageHeight/numSubdivisions) );
int pixelValue = 0;
int startingRow = 0;
int startingColumn = 0;
for (int i = 0; i < _validImageHeight/numSubdivisions; i++)
{
int index = (i + startingRow) * _imageWidth;
for( int j = 0; j < (_validImageWidth/numSubdivisions); j++)
{
pixelValue = imageData[index + startingColumn + j];
columnSumsCurrentFrameA[j] += pixelValue;
rowSumsCurrentFrameA[i] += pixelValue;
}
}
The result of _validImageWidth/numSubdivisions must be an integer, are you sure that is always the case?
Also, you should calculate _validImageWidth/numSubdivisions before entering the double loops, it's not safe to assume your compiler takes care of it.
int limit = _validImageHeight/numSubdivisions;
for (int i = 0; i < limit; i++)
{
int index = (i + startingRow) * _imageWidth;
for( int j = 0; j < limit; j++)
{
pixelValue = imageData[index + startingColumn + j];
columnSumsCurrentFrameA[j] += pixelValue;
rowSumsCurrentFrameA[i] += pixelValue;
}
}
I was initially thinking that the code below would return 0, my question, is there a function that I can use to only receive zero/positive results here?
NSUInteger pruneSize = 5 - 20; // Returns: -15
Currently I am just checking the result myself, but was wondering if I was missing something simpler.
NSUInteger pruneSize = 5 - 20;
if(pruneSize >= 0) {
// Do zero/positive Stuff ...
}
pruneSize >= 0 is always true as pruneSize is unsigned. You should get a warning here. You need to change the type to NSInteger, that is the signed integer. If you want to clip the lower value to zero for a signed int then you can do this:
NSInteger pruneSize = 5 - 20; // signed int
pruneSize = pruneSize < 0 ? 0 : pruneSize;
You can use abs(pruneSize) which will return you positive or zero number in any case.
EDIT:
NSUInteger pruneSize = 5-20;
if(pruneSize < 0)
{
pruneSize = 0;
}
NSLog(#"%d",pruneSize);
Hope this helps you.
If you want your function to return always zero if your result is in negative(less than 0) then return zero or else return result
int n=0;
if(result > 0){ //positive
n = result
else
n = 0
return n
or use the abs method
I am having problems with bottoms-up mergesort. I have problems sorting/merging. Current code includes:
public void mergeSort(long[] a, int len) {
long[] temp = new long[a.length];
int length = 1;
while (length < len) {
mergepass(a, temp, length, len);
length *= 2;
}
}
public void mergepass(long[] a, long[] temp, int blocksize, int len) {
int k = 0;
int i = 1;
while(i <= (len/blocksize)){
if(blocksize == 1){break;}
int min = a.length;
for(int j = 0; j < blocksize; j++){
if(a[i*j] < min){
temp[k++] = a[i*j];
count++;
}
else{
temp[k++] = a[(i*j)+1];
count++;
}
}
for(int n = 0; n < this.a.length; n++){
a[n] = temp[n];
}
}
}
Obvious problems:
i is never incremented.
At no point do you compare two elements in the array. (Is that what if(a[i*j] < min) is supposed to be doing? I can't tell.)
Why are you multiplying i and j?
What's this.a.length?
Style problems:
mergeSort() takes len as an argument, even though arrays have an implicit length. To make matters worse, the function also uses a.length and length.
Generally poor variable names.
Nitpicks:
If you're going to make a second array of the same size, it is common to make one the "source" and the other the "destination" and swap them between passes, instead of sorting into a temporary array and copying them back again.