Question

In: Computer Science

Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS...

Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS [] !!

* DO not use [ ] 'brackets', focus on using malloc, calloc, etc...

Program will ask user for the name of the text file to be read
Read the datafile with format:
1
2
1
1 2
3
4
So the first three lines will represent m, n, p (Matrix A: m x n ||| Matrix B: n x p), follwing are the two matrices.
Representing:
A = |1 2|     B = |3|
                         |4|

------------------------------------------------
example input and output:
Matrix A contents:
    1    2
    3    4
    5    6

Matrix B contents:
    7    8    9   10
   11   12   13   14

Matrix A * B is:
   29   32   35   38
   65   72   79   86
101 112 123 134

The datafile read for this example is:
3
2
4
1 2
3 4
5 6
7 8 9 10
11 12 13 14

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include<stdio.h>

#include<stdlib.h>

int main(){

    char filename[50];

    int m, n, p, i, j, k;

    int **A, **B, **mult;

    printf("Enter filename: ");

    scanf("%s", filename);

    FILE *fp;

    fp = fopen(filename, "r");

    if(fp==NULL){

        printf("Unable to open file\n");

        return 0;

    }

    fscanf(fp, "%d %d %d", &m, &n, &p);

    A = (int **)malloc(m*sizeof(int *));

    for(i=0; i<m; i++){

        *(A+i) = (int *)malloc(n*sizeof(int));

        for(j=0; j<n; j++){

            fscanf(fp, "%d", (*(A+i)+j));

        }

    }

    B = (int **)malloc(n*sizeof(int *));

    for(i=0; i<n; i++){

        *(B+i) = (int *)malloc(p*sizeof(int));

        for(j=0; j<p; j++)

            fscanf(fp, "%d", (*(B+i)+j));

    }

    

    mult = (int **)malloc(m*sizeof(int *));

    for(i=0; i<m; i++)

        *(mult+i) = (int *)malloc(p*sizeof(int));

    

    printf("Matrix A contents:\n");

    for(i=0; i<m; i++){

        for(j=0; j<n; j++){

            printf("%4d", *(*(A+i)+j));

        }

        printf("\n");

    }

    printf("Matrix B contents:\n");

    for(i=0; i<n; i++){

        for(j=0; j<p; j++){

            printf("%4d", *(*(B+i)+j));

        }

        printf("\n");

    }

    for(i=0; i<m; i++){

        for(j=0; j<p; j++){

            *(*(mult+i)+j) = 0;

            for(int k=0; k<n; k++){

                *(*(mult+i)+j) += *(*(A+i)+k) * *(*(B+k)+j);

            }

        }

    }

    printf("Matrix A * B is:\n");

    for(i=0; i<m; i++){

        for(j=0; j<p; j++){

            printf("%4d", *(*(mult+i)+j));

        }

        printf("\n");

    }

    for(i=0; i<m; i++){

        free(*(A+i));

        free(*(mult+i));

    }

    for(i=0; i<n; i++)

        free(*(B+i));

    

    free(A);

    free(B);

    free(mult);

    return 0;

}


Related Solutions

Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++ programming language; #include<iostream>
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without thread AND with thread. [12:18, 10/2/2020] Mohan Reddy: ou are to implement (M by N matrix) times (N by 1 vector) operation and see how multiple threads can speed up the computation. The resulting vector will be (M by 1 vector). See the following steps/requirements. 1. Accept M and N as keyboard input. 2. Generate a random (M by N matrix) and a random...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard deviation of an array of exam scores and then add the standard deviation to each array element. It should print the original array, the standard deviation, and the new rounded adjusted array. I included my question as a comment in the line of code that is giving me an issue. (Removing the a from E-x-a-m as Chegg doesn't allow the word.) #include <stdio.h> #include...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Write 2 classes for each program Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix...
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix A; 1 2 3 4 5...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally, the result must be stored in...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers,...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers, and multi-dimensional dynamic arrays Create a simple makefile Write a robust input validation. The purpose of this lab is to get familiar with the software environment we will be using this term, and review some C++ concepts to get prepared for future projects and labs. This lab might be very time-consuming since there are a lot of concepts you need to pick up. For...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal). The function should allocate storage from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT