Show object in NSMutableArray of matrix - objective-c

Following this code:
In ViewController.m
double kk[2][2] = {{1,2},{5,6}};
if (!matrix1Col) {
matrix1Col = [NSMutableArray array];
}
for (int i=0; i<2; i++) {
[matrix1Row removeAllObjects];
if (!matrix1Row) {
matrix1Row = [NSMutableArray array];
}
for (int j=0 ; j<2; j++) {
[matrix1Row insertObject:[NSNumber numberWithDouble:kk[i][j]] atIndex:j];
}
[matrix1Col insertObject:matrix1Row atIndex:i];
}
self.label100.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:0] objectAtIndex:0] doubleValue]];
self.label110.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:1] objectAtIndex:0] doubleValue]];
self.label101.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:0] objectAtIndex:1] doubleValue]];
self.label111.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:1] objectAtIndex:1] doubleValue]];`
I wanna show object in NSMutableArray of matrix which receive value from matrix of double in label.
And my all label must show as following ->label100 show 1 ->label110 show 5 ->label101 show 2 and ->label111 show 6
but It shows ->label100 show 5 ->label110 show 5 ->label101 show 6 and ->label111 show 6
How can I do?

The problem is that you're always overwriting already inserted objects, as you insert the row many times in the same loop. That's how I would write it:
double kk[2][2] = {{1,2},{5,6}};
NSMutableArray* matrix= [NSMutableArray new];
for(unsigned int i=0; i<2; i++)
{
NSMutableArray* row= [NSMutableArray new];
for(unsigned int j=0; j<2; j++)
{
[row addObject: #(kk[i][j]) ];
}
[matrix addObject: row];
}

Related

How to add elements in NSArray in Objective-C?

I want to add multiple elements in NSArray with #"title" and value.
for(int i = 0; i < size; i++){
BSDevice *d=[[DeviceManager devices] objectAtIndex:i];
self.dataArray =#[#{#"title":d.Name}];
}
But in this code it only adds one value. When I try with this method it doesn't work:
for(int i = 0; i < size; i++){
BSDevice *d=[[DeviceManager devices] objectAtIndex:i];
[self.dataArray addObject:#[#{#"title":d.buddyName}]];
}
I want to have multiple values with string and its value in NSArray like this #"title":d.Name,#"title":d.Name,#"title":d.Name
Your question isn't very clear and you didn't really explain what problem you are having with the second bit of code, but I think what you want is the following:
for (BSDevice *d in [DeviceManager devices]) {
[self.dataArray addObject:#{ #"title" : d.buddyName }];
}
This assumes that self.dataArray is an NSMutableArray and somewhere before this for loop you initialized self.dataArray as:
self.dataArray = [NSMutableArray array];

combine multiple array values into one string value?

I want to combine the array values into one string.
my arrays are like...
array1=[#"fizan",#"nike",#"pogo"];
array2=[#"round",#"rectangle",#"square"];
array3=[#"frame",#"frame",#"frame"];
I need like this...
value1 = fizan round frame
value2 = nike rectangle frame
value3 = pogo square frame
try this:
NSArray *array1= #[#"fizan",#"nike",#"pogo"];
NSArray *array2= #[#"round",#"rectangle",#"square"];
NSArray *array3= #[#"frame",#"frame",#"frame"];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:#[array1,array2,array3]];
NSMutableArray *output = [[NSMutableArray alloc] init];
NSString *a;
NSInteger count = array.count;
for (int i = 0; i<array1.count; i++) {
a = #"";
for (int j = 0; j<count; j++) {
a = [a isEqualToString: #""] ? [NSString stringWithFormat:#"%#",[[array objectAtIndex:j] objectAtIndex:i]] : [NSString stringWithFormat:#"%# %#",a,[[array objectAtIndex:j] objectAtIndex:i]];
}
[output addObject:a];
}
for (int i = 0; i < output.count; i++) {
NSLog(#"value %i -> %#",i+1,output[i]);
}
Hope this helps!
UPDATE:
NSArray *array1= #[#"fizan",#"",#"pogo"];
NSArray *array2= #[#"round",#"rectangle",#"square"];
NSArray *array3= #[#"frame",#"frame",#"frame"];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:#[array1,array2,array3]];
NSMutableArray *output = [[NSMutableArray alloc] init];
NSString *a;
NSInteger count = array.count;
for (int i = 0; i<array1.count; i++) {
a = #"";
for (int j = 0; j<count; j++) {
a = [a isEqualToString: #""] ? [NSString stringWithFormat:#"%#",[[array objectAtIndex:j] objectAtIndex:i]] : [NSString stringWithFormat:#"%# %#",a,[[array objectAtIndex:j] objectAtIndex:i]];
}
[output addObject:a];
}
for (int i = 0; i < output.count; i++) {
NSLog(#"value %i -> %#",i+1,output[i]);
}
I have tested this code. It works perfect. Check again and reconsider the issue.
Do this
NSArray *array1 = #[#"fizan", #"nike", #"pogo"];
NSString *value = [array1 componentsJoinedByString:#" "];
NSLog(#"value = %#", value);
Output will get like
value = fizan nike pogo
For your case
NSArray *completeArray = #[#[#"fizan",#"nike",#"pogo"], #[#"round",#"rectangle",#"square"], #[#"frame",#"frame",#"frame"]];
NSMutableArray *resultArray = [NSMutableArray array];
unsigned long count = 1;
for (int i = 0; i< count; i++) {
NSMutableArray *listArray = [NSMutableArray array];
for (NSArray *itemArray in completeArray) {
count = MAX(count,itemArray.count);
if (i < itemArray.count) {
[listArray addObject:itemArray[i]];
}
}
[resultArray addObject:listArray];
}
for (NSArray *itemArray in resultArray) {
NSString *value = [itemArray componentsJoinedByString:#" "];
NSLog(#"value = %#", value);
}
output
value = fizan round frame
value = nike rectangle frame
value = pogo square frame

Replacing multiple strings with Objective-C

I am currently doing an experiment where I input text into a UITextField, and the text is searched to see if it has certain strings in it. If it finds the certain text, it should replace it, and send it to a UITextView. (Think of a super-simplified translator)
The problem I am having with it is that it only sends the text I last paired.
For exammple,
NSString *mainString = [[NSString alloc] initWithString:field.text];
NSArray *stringsToReplace = [[NSArray alloc] initWithObjects:#"The",#"dog",#"cried", nil];
NSArray *stringsReplaceBy = [[NSArray alloc] initWithObjects:#"ehT",#"god",#"deirc", nil];
for (int i=0; i< [stringsReplaceBy count]; i++)
{
look.text = [mainString stringByReplacingOccurrencesOfString:[stringsToReplace objectAtIndex:i] withString:[stringsReplaceBy objectAtIndex:i]];
}
When I type in, "The dog cried." it should be saying "ehT god deirc." However, it is responding with "The dog deirc."
Please help.
You are calling stringByReplaceingOccurencesOfString three times on the same string:
for (int i=0; i< [stringsReplaceBy count]; i++)
{
look.text = [mainString stringByReplacingOccurrencesOfString:[stringsToReplace objectAtIndex:i] withString:[stringsReplaceBy objectAtIndex:i]];
}
Instead, save the result into another string object:
NSString *modifiedString = mainString;
for (int i=0; i< [stringsReplaceBy count]; i++)
{
modifiedString = [modifiedString stringByReplacingOccurrencesOfString:[stringsToReplace objectAtIndex:i] withString:[stringsReplaceBy objectAtIndex:i]];
}
look.text = modifiedString;

Iterating backwards over an array throwing exception

I am trying to make an add method that works like long addition, so I want to start the addition from the end and work my way backwards so I can get the carrys right and etc. So I am currently trying to start working backwards over the array.
For example what im trying to do.
two arrays with the character 123456789
and i want to add them starting at 9 + 9 then move to 8+8
So I'm pretty sure I'm using the right way to iterate backwards over an array, but everytime I try I get just the runtime error, index out of bounds, and I can't figure out why. Any help would be great, I just cant figure out why it keeps throwing the exception.
-(MPInteger *) add: (MPInteger *) x
{
NSMutableArray *a = self->intString;
NSMutableArray *b = x->intString;
NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];
//for (int i = 0; i < [a count]; i++) {
for (NSInteger i = [a count] - 1; i > 0; i--) {
int num = 10;
NSNumber *ourNum = [NSNumber numberWithInt:num];
NSNumber *total = [NSNumber numberWithInt:[[a objectAtIndex:i] intValue] + [[b objectAtIndex:i] intValue]];
if ([total intValue] >= [ourNum intValue]) {
total = [NSNumber numberWithInt:([total intValue] - [ourNum intValue])];
[c addObject:[NSNumber numberWithInt:([total intValue])]];
} else {
[c addObject:[NSNumber numberWithInt:[[a objectAtIndex:i] intValue]+[[b objectAtIndex:i] intValue]]];
}
NSLog(#"%#", c[i]);
}
return x;
}
First, let's clean up this code.
- (MPInteger *)add:(MPInteger *)x {
NSMutableArray *a = self->intString;
NSMutableArray *b = x->intString;
NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];
for (NSInteger i = [a count] - 1; i > 0; i--) {
int num = 10;
NSNumber *ourNum = #(num);
NSNumber *total = #([a[i] intValue] + [b[i] intValue]);
if ([total intValue] >= [ourNum intValue]) {
total = #([total intValue] - [ourNum intValue]);
[c addObject:#([total intValue])];
} else {
[c addObject:#([a[i] intValue] + [b[i] intValue])];
}
NSLog(#"%#", c[i]);
}
return x;
}
Next, let's remove redundant/duplicate code.
- (MPInteger *)add:(MPInteger *)x {
NSMutableArray *a = self->intString;
NSMutableArray *b = x->intString;
NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];
for (NSInteger i = [a count] - 1; i > 0; i--) {
int num = 10;
NSNumber *total = #([a[i] intValue] + [b[i] intValue]);
if ([total intValue] >= num) {
total = #([total intValue] - num);
}
[c addObject:total];
NSLog(#"%#", c[i]);
}
return x;
}
Now we can clearly see all of the issues.
You're going from [a count] - 1 to 1. You should be going all the way to 0.
a and b might have different sizes, so if you only do [a count] - 1 to 0, then if for example [b count] < [a count], you'll get an index out of bounds error when you try to access b[i].
You're adding stuff to the end of c, but you should be adding it to the beginning of c since you're iterating backwards.
You don't store the carry anywhere.
You are accessing c[i], which doesn't exist.
You are starting with an empty array 'c', and you NSLog c[i] which is obviously out of bounds on the first iteration.

Displaying an array in xcode

I am trying to display the an array with different factors of a number ("prime"). But instead of giving me the int numbers I always get 0,1,2,3,4,5,... .
factors.text = #"";
int factorsNumber;
NSMutableArray *array;
array = [NSMutableArray arrayWithCapacity:5];
for (factorsNumber=1; factorsNumber<=prime; factorsNumber++) {
if (prime%factorsNumber == 0) {
[array addObject:[NSString stringWithFormat:#"%d", factorsNumber]];
}
}
for (int i = 0; i < [array count]; i++) {
[array replaceObjectAtIndex:1 withObject:#"4"];
NSString *temp = [NSString stringWithFormat:#"%d, ", i, [[array objectAtIndex:i] intValue]];
factors.text = [factors.text stringByAppendingString:temp];
}
Replace
NSString *temp = [NSString stringWithFormat:#"%d, ", i, [[array objectAtIndex:i] intValue]];
with
NSString *temp = [NSString stringWithFormat:#"%d, ", [[array objectAtIndex:i] intValue]];
The problem was you were only printing the array index, not the value.