100% found this document useful (1 vote)
11 views9 pages

C++ Programming Homework Help

C++ Programming Homework Expert at cpphomeworkhelp.com.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
11 views9 pages

C++ Programming Homework Help

C++ Programming Homework Expert at cpphomeworkhelp.com.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

For any Homework related queries, Call us at : -  

+1 678 648 4277


You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/

Problem: Matrix Multiplication


Given an RA×CA matrix A and an RB ×CB matrix B, with 1 ≤
RA, RB ,CA,CB ≤ 1000, write a program that computes the
matrix product C = AB. All entries in matrices A and B are
integers with absolute value less than 1000, so you don’t need to
worry about overflow. If matrices A and B do not have the right
dimensions to be multiplied, the product matrix C should have its
number of rows and columns both set to zero.
Use the code provided in the file matrix2.data.zip as a basis for
your program–the input/output needed is already written for you.
Matrices will be stored as a structure which we’ll typedef as
Matrix. This structure will contain the size of our matrix along
with a statically-sized two-dimensional array to store the entries.

typedef struct Matrix s {


size t R, C;
int *index;
} Matrix;
In this problem, the memory for each matrix will be dynamically
allocated on the heap, and must be freed at the end of the program.
You will need to implement a function to allocate a matrix capable
of storing R × C elements, as well as a function that will destroy
the memory for such a matrix.
Do not submit your solution to problem ‘matrix’ for this
problem or use statically allocated memory; such solutions
will not receive any points for the assignment, even though
they would pass the grader’s tests.

Resource Limits

For this problem you are allotted 3 seconds of runtime and up


to 32 MB of RAM.
Input Format

Line 1: Two space-separated integers, RA and CA.


Lines 2 . . . RA + 1: Line i + 1 contains CA space-separated
integers: row i of matrix A.
Line RA + 2: Two space-separated integers, RB and CB .
Lines RA + 3 . . . RA + RB + 4: Line i + RA + 3 contains CB
space-separated integers: row i of matrix A.

Sample Input (file matrix2.in)

For any Homework related queries, Call us at : -  +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
3 2
1 1
1 2
-4 0
2 3
1 2 1
3 2 1

Output Format

Line 1: Two space-separated integers RC and CC , the


dimensions of the product matrix C . Lines 2 . . . RC + 1: Line
i + 1 contains CC space-separated integers: row i of matrix C .
If A and B do not have the right dimensions to be multiplied,
your output should just be one line containing 0 0.

Sample Output (file matrix2.out)

For any Homework related queries, Call us at : -  +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
We are given

so the product is the 3 × 3 matrix

For any Homework related queries, Call us at : -  +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Solution
/*PROG: matrix
LANG: C
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXN 300

typedef struct Matrix {
  size_t R, C;
  int index[MAXN]
[MAXN];
} Matrix;

void read_matrix( FILE *fin,
Matrix *matrix ) {
  fscanf( fin, "%zu %zu",
&matrix->R, &matrix->C );

 if( matrix->R >= MAXN || matrix->C >= MAXN ) {


    printf( "Error: tried to read matrix with a dimension larger
than %d\n", MAXN );
    exit( EXIT_FAILURE );

For any Homework related queries, Call us at : -  +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
}
 
  for( size_t r = 0; r < matrix->R; ++r ) {
    for( size_t c = 0; c < matrix->C; ++c )
{
      fscanf( fin, "%d", &matrix->index[r]
[c] );
    }
  }
}

void print_matrix( FILE *fout, Matrix
*matrix ) {
  fprintf( fout, "%zu %zu\n", matrix->R,
matrix->C );
  for( size_t r = 0; r < matrix->R; ++r ) {
    for( size_t c = 0; c < matrix->C - 1; ++c ) {
      fprintf( fout, "%d ", matrix->index[r][c] );
    }
    fprintf( fout, "%d\n", matrix->index[r]
[matrix->C - 1] );
  }
}

For any Homework related queries, Call us at : -  +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
void mult_matrix( Matrix *a,
Matrix *b, Matrix *prod ) {
  if( a->C != b->R ) {
    printf( "Error: tried to multiply
(%zux%zu)x(%zux%zu)\n", a->R,
a->C, b->R, b->C );
    exit( EXIT_FAILURE );
  }

size_t inner = a->C;
  prod->R = a->R;
  prod->C = b->C;

for( size_t r = 0; r < prod->R; ++r )


{
    for( size_t c = 0; c < prod->C; +
+c ) {
      prod->index[r][c] = 0;
      for( size_t i = 0; i < inner; ++i )
{
        prod->index[r][c] += a-
>index[r][i] * b->index[i][c];
      }
    }
  }
}
For any Homework related queries, Call us at : -  +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
int main(void) {
  FILE *fin
= fopen( "matrix.in", "r" ),
       *fout
= fopen( "matrix.out", "w" );
if( fin == NULL ) {
    printf( "Error: could not open
matrix.in\n" );
    exit( EXIT_FAILURE );
  }
 
if( fin == NULL ) {
    printf( "Error: could not open
matrix.out\n" );
    exit( EXIT_FAILURE );
  }

Matrix a, b, c;
 
  read_matrix( fin, &a );
  read_matrix( fin, &b );
  fclose( fin );

For any Homework related queries, Call us at : -  +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
  mult_matrix( &a, &b, &c );
 
  print_matrix( fout, &c );
  fclose( fout );
 
  return 0;
}

Below is the output using the test data:

matrix:

1: OK [0.004 seconds]
2: OK [0.004 seconds]
3: OK [0.004 seconds]
4: OK [0.013 seconds]
5: OK [0.009 seconds]
6: OK [0.006 seconds]
7: OK [0.011 seconds]
8: OK [0.011 seconds]
9: OK [0.012 seconds]
10: OK [0.004 seconds]

For any Homework related queries, Call us at : -  +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/

You might also like