-1

It's a c++ program that takes input for 3*3 matrices and displays the sum and product of those matrices. Now I have improved the program and it's working as expected, so is there anything that can be improved in this program.

#include <stdio.h>

//function prototypes

To get the input for an array.

void getarr(int *x);

To add the two matrices.


void addm(int *x, int *y, int *z);

To print an array.

void displaym(int *x);

To multiply two arrays.

void multm(int *x, int *y, int *z);

main function

int main() {

    //declaring arrays for matrices
    int a[3][3];
    int b[3][3];
    int c[3][3];
    int d[3][3];

    //getting input from user
    printf("\nEnter nine numbers as the values for first matrix:\n");
    getarr(a[0]);
    printf("\nThe matrix you entered is:\n");
    displaym(a[0]);
    printf("\nEnter nine numbers as the values for second matrix:\n");
    getarr(b[0]);
    printf("\nThe matrix you entered is:\n");
    displaym(b[0]);
    
    //calling function for addition
    addm(a[0], b[0], c[0]);

    //calling function for multiplication
    multm(a[0], b[0], d[0]);

    //printing the matrices
    printf("\nThe sum of the matrices is:\n");
    displaym(c[0]);
    printf("\nThe product of the matrices is:\n");
    displaym(d[0]);

    return 0;
}

definition of functions

void getarr(int *x) {
    for (int j = 0; j < 9; j++) {
        printf("%d:", j);
        scanf("%d", x);
        x++;
    }
}

void addm(int *x, int *y, int *z) {
    for (int i = 0; i < 9; i++) {
        *z = *x + *y;
        z++;
        x++;
        y++;
    }
}

void multm(int *x, int *y, int *z) {
    for(int j=0;j<3;j++){
    for (int i = 0; i < 3; i++, z++) {
        *z = (*x++)*(*y)+(*x++)**(y + 3)+(*x)**(y + 6);
        x -= 2, y += 1;
    }
    x += 3, y -= 3;
    }
}

void displaym(int *x) {
    printf("\n\n");
    for (int i = 0; i < 9; i++) {
        printf("%d  ", *x++);
        if (i == 2 || i == 5)
            printf("\n");
    }
}
SONU MALIK
  • 13
  • 4
  • What kind of environment is this program running within? A message like `RUN FAILED` is not familiar. – wallyk Dec 17 '20 at 16:27
  • Have you tried using a debugger? My suspicion would be something in `multm` going outside the bounds of the arrays – Alan Birtles Dec 17 '20 at 16:28
  • 2
    `d` is uninitialised in `main` – Alan Birtles Dec 17 '20 at 16:31
  • sigesgv means segmentation fault which means you are probably trying to access some uniinitalized pointer data – Coreggon Dec 17 '20 at 17:08
  • I am using Netbeans to run this program. – SONU MALIK Dec 17 '20 at 17:09
  • The statement `(*x++)*(*y)+(*x++)*(*y+3)+(*x)*(*y+6)` probably has undefined behavior. See here: https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior – jjramsey Dec 17 '20 at 17:35
  • `d` is still not initialised to a valid array. `multm` also modifies the global variable meaning you wont be able to call it more than once – Alan Birtles Dec 17 '20 at 19:26
  • 1
    `multm` is (a) wrong and (b) atrociuos. Is there a requirement for you to write it this way? Use indices please. By the way, a `[9]` array is not the same as a `[3][3]` array, the program has undefined behaviour. It also looks like a C program. C and C++ are different languages. C++ has a different set of native idioms. – n. m. could be an AI Dec 18 '20 at 19:50
  • I wanted to do all the calculations using the pointers. – SONU MALIK Dec 26 '20 at 08:57
  • Plesae note the warning and the different outcomes [here](https://godbolt.org/z/qbercP). – Bob__ Dec 26 '20 at 09:39

1 Answers1

0

The is a common error for uninitialized pointers. You declared int * d; earlier in the program but you never give it a memory that d should point to. And you need to initialize that memory.

You can add the following line inside main but before any use of d,

d = new int[9];
memset(d, 0, 9);
CS Pei
  • 10,869
  • 1
  • 27
  • 46