#import "MatrixViewController.h"
@interface MatrixViewController ()
{
NSMutableString *str;
}
@end
@implementation MatrixViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.arrayFirstElem =[[NSMutableArray alloc]init];
self.resultArray =[[NSMutableArray alloc]init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)multiplyMatrix:(id)sender{
if ([self.rowFirst.text isEqualToString:@""]||[self.ColFirst.text isEqualToString:@""]||[self.rowSecond.text isEqualToString:@""]||[self.ColSecond.text isEqualToString:@""])
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Enter no of Row &coloums First and Second Matrix" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Ok action example
}];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
}
else if ([self.rowFirst.text isEqual:self.ColSecond.text])
{
NSString *str1 = [self.firstMatElements text];
NSArray *firstMatrix = [str1 componentsSeparatedByString:@","];
NSArray *secondMatrix = [self.secondMatrElements.text componentsSeparatedByString:@","];
int firstRow = [self.rowFirst.text intValue];
int firstCol = [self.ColFirst.text intValue];
int secondRow = [self.rowSecond.text intValue];
int secondCol = [self.ColSecond.text intValue];
NSLog(@"firstMatrix is %@",firstMatrix);
NSLog(@"secondMatrix is %@",secondMatrix);
int multiply[100][100]; //Asuming max value is 10, 10
int sum = 0;
for (int c = 0; c < firstRow; c++) {
for (int d = 0; d < secondCol; d++) {
for (int k = 0; k < firstCol; k++) {
// NSLog(@"%d",sum);
//
// NSLog(@"%d",[firstMatrix[c*firstRow+k] intValue]);
// NSLog(@"%d",[secondMatrix[k*secondCol+d] intValue]);
if (firstRow == firstCol )
{
sum =sum+ [firstMatrix[c*firstRow+k] intValue]*[secondMatrix[k*secondCol+d] intValue];
}
else if (firstRow < firstCol )
{
if (c==0) {
sum =sum+ [firstMatrix[c*firstRow+k] intValue]*[secondMatrix[k*secondCol+d] intValue];
}if (c>0) {
sum =sum+ [firstMatrix[c*firstRow+k+(firstRow-1)] intValue]*[secondMatrix[k*secondCol+d] intValue];
}
}
if (firstRow > firstCol )
{
if (c==0) {
sum =sum+ [firstMatrix[c*firstRow+k] intValue]*[secondMatrix[k*secondCol+d] intValue];
}if (c>0) {
sum =sum+ [firstMatrix[c*(firstRow-1)+k] intValue]*[secondMatrix[k*secondCol+d] intValue];
}
}
NSLog(@"%d",sum);
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (int c = 0; c < firstRow; c++) {
for (int d = 0; d < secondCol; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
else
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"First Matrix Row and Second Matrix column must be Equal" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Ok action example
}];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon