I am trying to take a string (#"12345") and extractor each individual character and convert to it's decimal equivalent.
#"1" = 1
#"2" = 2
etc.
Here is what I have this far:
...
[self ArrayOrder:#"1234"];
...
-(void)ArrayOrder(Nsstring *)Directions
{
NSString *singleDirections = [[NSString alloc] init];
//Loop Starts Here
*singleDirection = [[Directions characterAtIndex:x] intValue];
//Loop ends here
}
I have been receiving type errors.
The problem with your code is that [Directions characterAtIndex:x] returns a unichar, which is a Unicode character.
Instead, you can use NSRange and substrings to get each number out of the string:
NSRange range;
range.length = 1;
for(int i = 0; i < Directions.length; i++) {
range.location = i;
NSString *s = [Directions substringWithRange:range];
int value = [s integerValue];
NSLog(#"value = %d", value);
}
Another approach would be to use / 10 and % 10 to get to each number from the string individually. Such as:
NSString* Directions = #"1234";
int value = [Directions intValue];
int single = 0;
while(value > 0) {
single = value % 10;
NSLog(#"value is %d", single);
value /= 10;
}
However, that goes through your string backwards.
I have an NSArray with the numbers {0,1,2,3}
Calculating the factorial of 4 (the count of the array), I have 24 possible permutations of 0,1,2,3
I would like to know if there is a way to calculate all of these possible permutations and place them in a separate array.
For example, given the numbers above, {0,1,2,3}, the resulting permutations would be:
0123, 0132, 0213, 0231, 0312, 0321,
1023, 1032, 1203, 1230, 1302, 1320,
2013, 2031, 2103, 2130, 2301, 2310,
3012, 3021, 3102, 3120, 3201, 3210
Any help is greatly appreciated. Thank you so much!
I was looking for code, but I managed to figure it out :) If anyone else needs it, the code is as follows:
static NSMutableArray *results;
void doPermute(NSMutableArray *input, NSMutableArray *output, NSMutableArray *used, int size, int level) {
if (size == level) {
NSString *word = [output componentsJoinedByString:#""];
[results addObject:word];
return;
}
level++;
for (int i = 0; i < input.count; i++) {
if ([used[i] boolValue]) {
continue;
}
used[i] = [NSNumber numberWithBool:YES];
[output addObject:input[i]];
doPermute(input, output, used, size, level);
used[i] = [NSNumber numberWithBool:NO];
[output removeLastObject];
}
}
NSArray *getPermutations(NSString *input, int size) {
results = [[NSMutableArray alloc] init];
NSMutableArray *chars = [[NSMutableArray alloc] init];
for (int i = 0; i < [input length]; i++) {
NSString *ichar = [NSString stringWithFormat:#"%c", [input characterAtIndex:i]];
[chars addObject:ichar];
}
NSMutableArray *output = [[NSMutableArray alloc] init];
NSMutableArray *used = [[NSMutableArray alloc] init];
for (int i = 0; i < chars.count; i++) {
[used addObject:[NSNumber numberWithBool:NO]];
}
doPermute(chars, output, used, size, 0);
return results;
}
use
getPermutations(input, size)
to get an NSArray with the permutations stored.
For Example:
NSLog(#"%#", getPermutations(#"0123", 4));
//console log
RESULTS: (
0123,
0132,
0213,
0231,
0312,
0321,
1023,
1032,
1203,
1230,
1302,
1320,
2013,
2031,
2103,
2130,
2301,
2310,
3012,
3021,
3102,
3120,
3201,
3210
)
It's working perfect for me now :)
I would like to know if there is a way to calculate all of these possible permutations
Sure (although they're not combinations but rather permutations):
unsigned long long factorial(unsigned long long n)
{
return n > 1 ? n * factorial(n - 1) : 1;
}
unsigned long long perms = factorial(array.count);
and place them in a separate array.
Sure, there are excellent algorithms for making permutations (too), for example the Johnson-Trotter algorithm.
I'm finding this a bit counter intuitive. If I want to do the equivalent of scanf and assign that input to a variable or array how do I do that and then print it in objective-c.
You can use NSMutableArray and loop each character in NSString test that you have then use
I have already tested it ..
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSString *str = [NSString stringWithFormat:#"masdasdada"];
int length = [str length];
for (int i = 0 ; i < length; i++) {
NSLog(#"%hu",[str characterAtIndex:i]);
unichar utf8char = [str characterAtIndex:i];
char chars[2];
int len = 1;
if (utf8char > 127) {
chars[0] = (utf8char >> 8) & (1 << 8) - 1;
chars[1] = utf8char & (1 << 8) - 1;
len = 2;
} else {
chars[0] = utf8char;
}
NSString *string = [[NSString alloc] initWithBytes:chars
length:len
encoding:NSUTF8StringEncoding];
NSLog(#"%#",string);
[arr addObject:[NSString stringWithFormat:#"%#",string]];
}
for (NSMutableString *s in arr) {
NSLog(#"%#",s);
}
I have an array width this values:
array: (
{
id = 1;
name = "Cursus Nibh Venenatis";
value = "875.24";
},
{
id = 2;
name = "Elit Fusce";
value = "254.02";
},
{
id = 3;
name = "Bibendum Ornare";
value = "123.42";
},
{
id = 4;
name = "Lorme Ipsim";
value = "586.24";
}
)
What I need to do is get each 'value' and sum it all. Im declaring a new array to take each value:
self.valuesArray = [[NSArray alloc] init];
But how can I do it? Thanks for your answer!
double sum = [[array valueForKeyPath:#"#sum.value"] doubleValue];
You can read more on collection operators here
You have already declared array so i will use your. I also assume your first array(which contains data set above) is an array called myFirstArray(of type NSArray)
int sum =0;
self.valuesArray = [[NSMutableArray alloc] init];
for(NSDictionary *obj in myFirstArray){
NSString *value =[obj objectForKey:#"value"];
sum+= [value intValue];
[self.valuesArray arrayWithObject:value];//this line creates a new NSArray instance which conains array of 'values'(from your dictionary)
}
NSLog("The sum of values is: %d", sum);
NSLog("The array of \'values\' is : %#",self.valuesArray );
double sum=0.0;
for (YourDataObject *d in array) {
sum+=[[d getValue] doubleValue];
}
try this -
float totalValue = 0.0f;
for (int i = 0 ; i< [array count]; i++) {
totalValue +=[[[array objectAtIndex:i] objectForKey:#"value"] floatValue];
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert NSString to ASCII Binary Equivilent (and then back to an NSString again)
I would like to know how I can get a character and convert it to binary string.
Let's say I have a string :
NSString* test = #"Test";
How can I print this string as an ASCII binary codes for each character in order from left to right:
T = decimal 84 = binary 1010100
e = decimal 101= binary 1100101
I then want to print the sum sequence of all character codes
NSString binaryTest = #"10101001100101...";
This should do what you want:
NSMutableString *finalString = [[NSMutableString alloc] init];
NSMutableString *binary;
NSMutableString *binaryReversed;
// The original string
NSString *test = #"Test";
char temp;
int i, j, digit, dec;
// Loop through all the letters in the original string
for (i = 0; i < test.length; i++) {
// Get the character
temp = [test characterAtIndex:i];
dec = (int)temp;
binary = [[NSMutableString alloc] initWithString:#""];
// Convert the decimal value to binary representation
// getting the remainders and storing them in an NSString
while (dec != 0) {
digit = dec % 2;
dec = dec / 2;
[binary appendFormat:#"%d", digit];
}
// Reverse the bit sequence in the NSString
binaryReversed = [[NSMutableString alloc] initWithString:#""];
for (j = (int)(binary.length) - 1; j >= 0; j--) {
[binaryReversed appendFormat:#"%C", [binary characterAtIndex:j]];
}
// Append the bit sequence to the current letter to the final string
[finalString appendString:binaryReversed];
[binaryReversed release];
[binary release];
}
// Just show off the result and release the finalString
NSLog(#"%# in binary: %#", test, finalString);
[finalString release];