Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have NSNumber objects stored in an NSMutableArray. I am attempting to perform a calculation on each object in the array.
What I want to do is:
1) Take a random higher number variable and keep subtracting a smaller number variable in increments until the value of the variable is equal to the value in the array.
For example:
NSMutableArray object is equal to 2.50.
I have an outside variable of 25 that is not in the array.
I want to subtract 0.25 multiple times from the variable until I reach less than or equal to 2.50. I also need a parameter so if the number does not divide evenly and goes below the array value, it resorts to the original array value of 2.50.
Lastly, for each iteration, I want to print the values as they are counting down as a string.
I was going to provide code, but I don't want to make this more confusing than it has to be.
So my output would be:
VALUE IS: 24.75
VALUE IS: 24.50
VALUE IS: 24.25
…
VALUE IS: 2.50
END
I'm on my iPad so this won't be as nice as I would like
float randomValue = 25; //or however you get the value
float subtractionValue = 0.25 // or whatever value or use a define
for (NSNumber *n in myArray)
{
while (randomValue > n.floatvalue)
{
randomValue -= subtractionValue;
NSLog(#"Value is: %f", randomValue);
if (randomValue < 2.5)
{
randomValue = n.floatvalue;
}
}
}
Well run the while loop and check like that below:-
Lets assume your variable here whose value is 25
float a=25;
while (a>=2.50)
{
a=a-0.25;
NSlog(#"count is %f",a);
}
Hope it helps, it is just the logic you can try with your code and if required please modify accordingly.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've been struggling with the the very first question of the exercise from opendatatructures.org regarding datastructures. I question goes like:
A Dyck word is a sequence of +1's and -1's with the property that the
sum of any prefix of the sequence is never negative. For example,
+1,−1,+1,−1 is a Dyck word, but +1,−1,−1,+1 is not a Dyck word since the prefix +1 − 1 − 1 < 0. Describe any relationship between Dyck
words and Stack push(x) and pop() operations.
How does one find the relation between the operation?
One way to represent check if a word if a Dyck word or not is to use a stack, where you push every time you encounter a +1 and pop every time you encounter a -1. If you ever try to pop from an empty stack, it's not a Dyck word.
Consider the following psuedocode (assume that a word is represented as a array of integers, since the question isn't really about parsing):
boolean isDyck(int[] word) {
Object dummy = new Object(); // Just so you have something to push
Stack stack = new Stack();
for (item : word) {
if (item > 0) {
stack.push(dummy);
} else {
if (stack.isEmpty()) {
return false;
}
stack.pop();
}
}
return true;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am struggling to see if there is an obvious advantage over which method to use when passing values into a function. My code below may not be the best example to explain the decision I'm trying to make, but it is, in my opinion, the easiest to understand.
Variadic Parameter Approach
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for value in numbers {
total += value
}
return total / Double(numbers.count)
}
arithmeticMean(5, 10, 15)
Array Parameter Approach
func arithmeticMean(numbers: [Double]) -> Double {
var total: Double = 0
for value in numbers {
total += value
}
return total / Double(numbers.count)
}
arithmeticMean([5, 10, 15])
Is either of the two techniques preferred? If so, why (speed, reliability or just ease of reading)? Thanks.
I think there is no speed difference.Because,inside the function,you use Variadic Parameter just as Array.
I think that if the parameters count is small,for example,less than 5,Variadic Parameter may be a better solution,because it is easy to read.
If the count of parameters is large. Array is better solution.
Also know that,Variadic Parameter have some limitation:
A function may have at most one variadic parameter, and it must always appear last in the parameter list, to avoid ambiguity when calling the function with multiple parameters.
If your function has one or more parameters with a default value, and also has a variadic parameter, place the variadic parameter after all the defaulted parameters at the very end of the list.
Just from my idea.Hopes helpful
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm developing an app that uses GPS position. For knowing position I get latitude and longitude.
I don't get position at regular intervals, but I want to know if user has moved. For that I store last position and current position (both latitude and longitude)
I was doing this:
if (latitudNew != latitudOld && longitudNew != longitudNew)
{
float R = 6378.137;
double dLat = ((latitudNew - latitudOld) * M_PI) / 180;
double dLong = ((longitudNew - longitudOld) * M_PI) / 180;
double a = sin(dLat/2) * sin(dLat/2) + cos((latitudOld*M_PI)/180) * cos((latitudNew*M_PI)/180) * sin(dLong/2) * sin(dLong/2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = R * c;
}
Where distance is stored in d.
My problem is that it never get past the if condition, so I know I'm not comparing it correctly.
I can't use this solution, as I can have negative numbers (one or both or none).
Any ideas??
You have the line:
if (latitudNew != latitudOld && longitudNew != longitudNew)
This is doing longitudNew != longitudNew. You have a typo there, you're testing to see if a variable is not equal to itself. It'll always be false. You meant to put longitudOld for one of them. Though you probably want to replace the && with ||, because you want to execute the code in the if block if either of these conditions is true.
Also, this is a bit of a nit... The proper spellings are longitude and latitude. You left the e off of both.
Comparing doubles isn't a good idea. What you'll need to do is to subtract the two values, then check if the difference between the two is greater than a threshold value you'll need to define, like 0.0001.
That looks correct to me, you'd compare doubles in Objective-C just as you would in C. The datasource you're getting the information from might be rounding the values so that they appear as equal or the data isn't updating often enough.
Also, you'd probably want to change the logic to have an || in the if instead of an &&. You'd still consider an update in location to be an update, even if it was just in one of the two.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
As I'm from other scripting language background, I'm not so much expert in objective-c & iPhone programming.
I've tried some code of objective-c myself, but they aren't working as I expected.
I have elaborated my requirements on this image:
So I'm seeking a help from the experts about my program-
I want to make a program for iPhone in objective-C that will generate random number & generate signs (like +, -, *, /).
Then it will subtract/add/multiply/divide with random values in 2 different sections.
Now users have to compare between two sections.
Is the result of 2 sections are = or < or > . There will be three individual buttons for the signs.
After getting the press on the button from the user a new label will show the answer, if it is right/wrong.
If the answer is wrong, it will show the correct answer also.
Any help on this program on objective-c language required.
Thanks in advance.
To generate a random number in a range use:
int randomInteger = arc4random_uniform(range);
To generate a random operator character:
NSArray *operators = #[#"+", #"-", #"*", #"/"];
int randomOperatorIndex = arc4random_uniform(4);
NSString *randomOperator = operators[randomOperatorIndex];
The rest of the code you need to figure out and write yourself. If you have problems post another question with the problem code for help with it.
Notes:
arc4random() is a high quality 32-bit pseudo-random number generator that does not need to be seeded, it seeds itself on a regular basis from the kernel strong random number subsystem.
arc4random_uniform(range) returns an integer in the range of 0 to (range-1). It has the added benefit that it avoids "modulo bias" that the mod (%) operator does.
Use arc4random() to generate random no.
Use following to generate random operator
-(NSString*) getRamdomOperator
{
int randNum = arc4random_uniform(4);
NSString *operator;
switch(randNum)
{
case 0:
operator = #"+";
break;
case 1:
operator = #"-";
break;
case 2:
operator = #"/";
break;
case 3:
operator = #"*";
break;
Default:
break;
}
return operator;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I round a float value to 2 post decimal positions?
Lets say I have a double number of 3.46.
How do I round it to 3.50?
I tried
NSLog(#"res: %.f", round(3.46));
but it return 3.
Do some calculations....
float f=3.46;
float num=f+0.05;//3.51
int intNum=num*10;//35
float floatNum=intNum/10.0;//3.5
NSLog(#"res: %.2f", floatNum); //3.50
Following code may help
i = roundf(10 * i) / 10.0;
where i is your float variable
If you're willing to live with the rounding rules from printf, then the following should suffice when rounding for presentation:
NSLog(#"res: %.1f0", 3.46);
Note that the 0 is just a normal character that is added after the value is formatted to the appropriate number (1) of decimal places. This approach should be usable with [NSString stringWithFormat:] as well.
The original code results in "3" because round always returns an integral value.
YMMV; I don't even use iOS.