Stuck on this Program If someone could help me finish I would be very greatful i
ID: 3561643 • Letter: S
Question
Stuck on this Program If someone could help me finish I would be very greatful
input 1:
input 2:
Output should have the same format.
Program (we only can use C):
/*
* The following is a simple program in C that takes the name of a file as a
* command line argument and prints its contents into an output file. It uses
* fscanf() and fprintf() to format the input and output respectively.
*
*/
#include
#include
//prototypes
void **matrixMultiply(int **mat1,int **mat2);
int matSize(char *line);
int main(int argc,char *argv[])
{
FILE *fr1,*fr2,*fw;
int **mat; //To hold the matrix from the input file
int rc; //To hold the number of rows and columns
int toStdOut=1; //This is a flag variable. 0 for file output,1 for std out
char line[100];
//Check for the number of command line arguments
if (argc!=4) {
//Print output to standard output
toStdOut=0;
}
//To read a file use the fopen() function to open it
fr1=fopen(argv[1],"r");
//If the file fails to open the fopen() returns a NULL
if (fr1==NULL) {
printf("Cannot open %s. Program terminated...",argv[1]);
exit(1);
}
// Similar to the above method read the second file
fr2=fopen(argv[2],"r");
if (fr2==NULL) {
printf("Cannot open %s. Program terminated...",argv[2]);
exit(1);
}
/* INSERT CODE FOR MATRIX READING AND MULTIPLICATION */
/***********************************************************************/
//The next few lines will help you get started
//Read the first line of the input file and count the number of columns
fgets(line,100,fr1);
rc=matSize(line);
fclose(fr1);
//Create required matrix
mat=(int **)malloc(sizeof(int *)*rc);
fclose(fr2);
/***********************************************************************/
//Once your output matrix is made, check if it is a standard output
//or output to file
if (toStdOut==1) {
//Create the output file
fw=fopen(argv[3],"wx");
//Check if the file has been created successfully or not
if (fw==NULL) {
printf("File not created. Program terminated...");
exit(1);
}
//WRITE THE RESULT MATRIX TO THE OUTPUT FILE
//Enter code here to write the matrix into the file
fclose(fw);
} else {
/**DISPLAY THE OUTPUT MATRIX TO THE STANDARD OUTPUT i.e. THE CONSOLE*/
//Enter code here
printf(" DISPLAY MATRIX HERE (replace this printf with appropriate code!");
}
return 0;
}
//The following function will count the number of elements per line
//and return the number of rows and columns for the matrix
int matSize(char *line)
{
int count=0;
//Enter code here
return count;
}
//The following function will take two matrices as input and return the
//product matrix
void **matrixMultiply(int **mat1,int **mat2)
{
//Enter code here
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int a[10][10],b[10][10],c[10][10],n=0,m=0,i=0,j=0,p=0,q=0,k=0;
int *pt,*pt1,*pt2;
printf("Enter size of 1st 2d array : ");
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("Enter element no. %d %d :",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter size of 2nd 2d array : ");
scanf("%d %d",&p,&q);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("Enter element no. %d %d :",i,j);
scanf("%d",&b[i][j]);
}
}
if(m!=p)
{
printf("Multiplication cannot be done ");
exit (0);
}
pt=&a[0][0];
pt1=&b[0][0];
pt2=&c[0][0];
for(i=0;i<n;i++)
{
for(k=0;k<q;k++)
{
*(pt2+(i*10+k))=0;
for(j=0;j<m;j++)
{
*(pt2+(i*10+k))+=*(pt+(i*10+j))**(pt1+(j*10+k));
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<q;j++)
{
printf("%d ",c[i][j]);
}
printf(" ");
}
return 0;
}