Status
Not open for further replies.

mak23101991

Active Member
34
2012
0
0
Matrix Addition

Problem Statement:
Develop a program to add two matrices. Program should accept input matrices to be added via files.
Absolute path of these file should be accepted through a command line argument.
The file format of the input files is described below. Your program should parse the input files and construct the input matrices.
In case of a bad input, program should output appropriate message as mentioned in output specification below.
Once the matrices are formed, apply appropriate logic to see if addition is possible.
If addition is possible, add the input matrices and output the result in format specified below.
Code:
Information:
Matrix addition is the operation of adding two matrices that in turn produces resultant third matrix which is a sum of the two input matrices.
Code:
File Format
Each row is delimited by new line character
Columns within each row are space delimited
e.g.
A 3x3 matrix will be represented in the file as :
1 2 3
4 5 6
7 8 9
Code:
Input Format
Line 1    Absolute path of the first input file
Line 2    Absolute path of the second input file
Code:
Output
1.    Input matrices which are inserted
2.    Resultant matrix or appropriate error message
Sample Output:

Code:
matrix1.txt :
3 3 1
5 9 2
6 4 2

matrix2.txt:
2 1 1
1 5 5
3 8 9
Input Matrix 1 :
3 3 1
5 9 2
6 4 2
Input Matrix 2 :
2 1 1
1 5 5
3 8 9
Valid Matrix dimensions for addition.
5 4 2
6 14 7
9 12 11
Code:
matrix1.txt :
7 3 1
7 2 5
2 6 4

matrix2.txt :
2 1
1 5
3 8    
Input Matrix 1 :
7 3 1
7 2 5
2 6 4
Input Matrix 2 :
2 1
1 5
3 8
Invalid Matrix dimensions for addition.
Cannot add matrix of dimensions 3 with 3x2
Code:
matrix1.txt :
9 4 8
2 3 7
9 2 1

matrix2.txt :
1 2 3
4 5 6
1 ? 3
INVALID INPUT
Code:
matrix1.txt:
a b c
d e f
g h i

matrix2.txt:
2 8 5
8 5 2
0 2 4    
INVALID INPUT
How to code this Please Help
Thanx in advance
 
3 comments
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
int i,j,c,r,r1,row,row1;
int st[9],st1[9],st2[9];
FILE *f = fopen(argv[1], "r");
FILE *f1=fopen(argv[2],"r");
r=9;
r1=9;
/*initialize array*/
for(i=0;i<r;i++)
{
st='\0';
}
for(i=0;i<r1;i++)
{
st1='\0';
}
/*storing values in array from file*/
for(i=0;i<r;i++)
{
fscanf(f,"%d",&st);
}
for(i=0;i<r1;i++)
{
fscanf(f1,"%d",&st1);
}
row=(sizeof(st)/sizeof(int));
row1=(sizeof(st1)/sizeof(int));
for(i=0;i<r;i++)
{
if((st=='\0')||(st1=='\0'))
{
printf("Invalid matrix dimension\n");
exit(0);
}
}
/*printing input matrix*/
printf("Input Matrix1:\n");
for(i=0;i<r;i++)
{
printf("%d\t", st);
}
printf("\n");
printf("Input Matrix2:\n");
for(i=0;i<r1;i++)
{

printf("%d\t", st1);
}
printf("\n");
/*printing output matrix*/
printf("Output Matrix:\n");
for(i=0;i<r;i++)
{
st2=st+st1;
printf("%d\t", st2);
}
printf("\n");
fclose(f);
fclose(f1);
return 0;
}
 
Status
Not open for further replies.
Back
Top