0

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();
}
2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by coolbeanbob
0

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

0

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

0

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();
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.