Question:
Need help in C using array with functions?
Ankit
2009-09-04 05:00:44 UTC
I want to make a program of matrix addition,subtraction,multiplication and transpose using functions and using switch case.
Please tell me the program.
Three answers:
cja
2009-09-04 07:31:33 UTC
Here's something to get you started. I don't see much use for a switch statement in the matrix manipulation logic. Maybe you can use one in the user interface portion of your test driver.



#include

#include



#define ROWS 2

#define COLS 2



typedef struct {

    size_t _rows;

    size_t _cols;

    int **_m;

} IntMatrix;



IntMatrix *newIntMatrix(const size_t, const size_t);

void delIntMatrix(IntMatrix *);

IntMatrix *addIntMatrix(const IntMatrix *m1,

                                                const IntMatrix *m2);

IntMatrix *multIntMatrix(const IntMatrix *m1,

                                                  const IntMatrix *m2);

void printIntMatrix(const char*, const IntMatrix*);



int main(int argc, char *argv[]) {

    IntMatrix *A = newIntMatrix(ROWS,COLS);

    IntMatrix *B = newIntMatrix(ROWS,COLS);

    IntMatrix *C;

    size_t i,j, k = 0;

 

    /* initialize for matrix add test */

    for (i = 0; i < A->_rows; i++) {

        for (j = 0; j < A->_cols; j++) {

            A->_m[i][j] = ++k;

            B->_m[i][j] = k + (A->_rows * A->_cols);

        }

    }



    printIntMatrix("A:",A);

    printIntMatrix("B:",B);



    /* compute C = A + B, and print result */

    C = addIntMatrix(A,B);

    printIntMatrix("A + B:",C);



    delIntMatrix(A);

    delIntMatrix(B);

    delIntMatrix(C);



    A = newIntMatrix(2,3);

    A->_m[0][0] = 1; A->_m[0][1] = 0; A->_m[0][2] = -2;

    A->_m[1][0] = 0; A->_m[1][1] = 3; A->_m[1][2] = -1;

    B = newIntMatrix(3,2);

    B->_m[0][0] = 0; B->_m[0][1] = 3;

    B->_m[1][0] = -2; B->_m[1][1] = -1 ;

    B->_m[2][0] = 0; B->_m[2][1] = 4;



    printIntMatrix("A:",A);

    printIntMatrix("B:",B);



    /* compute C = A * B, and print result */

    C = multIntMatrix(A,B);

    printIntMatrix("A * B:",C);

   

    delIntMatrix(A);

    delIntMatrix(B);

    delIntMatrix(C);

    return 0;

}



IntMatrix *newIntMatrix(const size_t rows, const size_t cols) {

    IntMatrix *m = (IntMatrix*)NULL;

    size_t i;



    if ((rows > 0) && (cols > 0)) {

        m = (IntMatrix*)calloc(sizeof(IntMatrix),1);

        m->_rows = rows;

        m->_cols = cols;

        m->_m = (int**)calloc(m->_rows,sizeof(int*));

        for (i = 0; i < m->_rows; i++) {

            m->_m[i] = calloc(m->_cols,sizeof(int));

        }

    }

    return m;

}



void delIntMatrix(IntMatrix *m) {

    size_t i;

    if ((m != (IntMatrix*)NULL) && (m->_m != (int)NULL)) {

        for (i = 0; i < m->_rows; i++) {

            free(m->_m[i]);

        }

        free(m->_m);

    }

}



IntMatrix *addIntMatrix(const IntMatrix *m1,

                                                const IntMatrix *m2) {

    IntMatrix *sum = (IntMatrix*)NULL;

    size_t i,j;



    if ((m1 != (IntMatrix*)NULL) &&

            (m2 != (IntMatrix*)NULL) &&

            (m1->_rows == m2->_rows) &&

            (m2->_cols == m2->_cols)) {

        sum = newIntMatrix(m1->_rows,m1->_cols);

        for (i = 0; i < m1->_rows; i++) {

            for (j = 0; j < m1->_cols; j++) {

                sum->_m[i][j] = m1->_m[i][j] + m2->_m[i][j];

            }

        }

    }

    return sum;

}



IntMatrix *multIntMatrix(const IntMatrix *m1,

                                                  const IntMatrix *m2) {

    IntMatrix *prod = (IntMatrix*)NULL;

    size_t i,j,k;



    i
Sukhadev
2009-09-04 05:17:44 UTC
I am giving main Steps hereunder



main( )

{

declare arrays;

read array elements;

read users choice either addition, subtraction, multi etc;

switch case( ) statement

{

now to pass the entire array to the function, say for example



addition( ) is the function name then the calling statement would be



addition(n,m, A[][],B[][]);



here n and m are rows and columns and A[] and B[] are two arrays



do not specify the size inside square brackets just pass the array name with empty square brackets



}



now in function definition



int addition(int r, int c, int a[][], int b[][])

{

addition of matrix steps goes here;

}







hope this would help u
anonymous
2016-12-01 12:57:55 UTC
Yeah.... you would be utilising education. quite, defining some component like 'char grid[]' is defining a pointer. The syntax 'char *grid' is exactly an comparable project. attempt defining grid as 'char **grid' (a pointer to a pointer, that's what an array of arrays is). you should opt to have the skill to artwork with it then.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...