Hi.

I didn't declare a,b,c from the first program but i did after the first program. What am i missing?

#define m 2
#define n 2
 
void matrixaddition()
{
int i,j;
for (i=0; i<m; i++)
for (j=0; j<n; j++)
c[i] [j] = a[i] [j] + b[i] [j];
}

#include <stdio.h>
 
int a[m][n] = {1,1,1,1};
int b[m][n] = {2,2,2,2};
int c[m][n] ;
 
void printc ()
{
int i, j;
 
for(i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
printf("%d ", c[i] [j]);
printf("\n");
}
}
int main ( )
{
matrixaddition();
printc() ;
getchar();
}

Recommended Answers

All 4 Replies

Same as your last post. You declared a function that uses variables that are declared after the function.

Where do you define m and n?

I should still declare the function first then the variables? But i called in the procedure.

This is better. It compiles. Not sure if it is doing what you intend for it to do. I rearranged the top few lines to get it to compile.

#include <stdio.h>
#define m '2'
#define n '2'

using namespace std;

    int a[m][n] = {{1,1}, {2,2}};
    int b[m][n] = {{2,2}, {3,3}};
    int c[m][n];

void matrixaddition()
{
    int i,j;
    for (i=0; i<m; i++)
        for (j=0; j<n; j++)
            c[i] [j] = a[i] [j] + b[i] [j];
}


void printc ()
{
    int i, j;

    for(i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        printf("%d ", c[i] [j]);
        printf("\n");
    }
}
int main ( )
{


    matrixaddition();
    printc() ;
    getchar();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.