Adding two arrays, beginner program!

// The program below should add two matrices using function.
// I have problem in the third function (the summing function)
// It's saying that something wrong with the array! But I can't figure out
// how to fix that. I'm new to arrays and functions
// Any help will be appreciated! Thanks in advance!





#include<iostream>
#include<fstream>
#include<conio.h>

using namespace std;

int matrix1 ();
int matrix1 ();
int add ();

int main()
{

int matrix1 ();
int matrix1 ();
int add (int a, int b);


return 0;
}



int matrix1 ( )
{
const int i=3;
const int j=3;
int a[i][j];

cout<<"\n\nEnter elements for Matrix A :::\n\n";

for(int x=0;i<3;x++)
{
for(int y=0;j<3;y++)
{
cin>>a[i][j];
}
cout<<"\n";
}
return (a[i][j]);
}



int matrix2 ()
{
const int i=3;
const int j=3;
int b[i][j];

cout<<"\n\nEnter elements for Matrix A :::\n\n";

for(int x=0;i<3;x++)
{
for(int y=0;j<3;y++)
{
cin>>b[x][y];
}
cout<<"\n";
}
return (b[i][j]);
}






int add (int a ,int b)
{

const int i=3;
const int j=3;

int c[i][j];

cout<<"\n\nAddition of the two Matrices :\n\n";

for(int x=0;i<3;x++)
{
for(int y=0;j<3;y++)
{
cout<<"\t"<<(c[x][y])= (a[x][y])+ (b[x][y]);
}
cout<<"\n\n";
return (c[x][y]);
}
}



You have not clearly defined a "matrix". You need to do so.

1
2
3
const int NROWS = 3;
const int NCOLS = 3;
typedef int matrix_t[ NROWS ][ NCOLS ];

Now you can create functions that do things to or with a matrix (or matrices).

1
2
3
4
5
6
7
8
9
10
11
12
13
void get_matrix_from_user( const string& name, matrix_t m )
{
  cout<<"\n\nEnter elements for Matrix " << name << " :::\n\n";

  for(int x=0;i<NROWS;x++)
  {
    for(int y=0;j<NCOLS;y++)
    {
      cin>>m[x][y];
    }
  cout<<"\n";
  }
}
1
2
3
4
5
6
7
8
9
10
11
void add_matrices( matrix_t a, matrix_t b, matrix_t m )
// m = a + b
{
  for (int x=0; ...)
  {
    for (int y=0; ...)
    {
      m[x][y] = a[x][y] + b[x][y];
    }
  }
}

You'll also want a function to display a matrix to the user:
1
2
3
4
void display_matrix( matrix_t m )
{
  for ...
}

Now all you need to do is have a main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  matrix_t a;
  matrix_t b;
  matrix_t c;

  get_matrix_from_user( "A", a );
  get_matrix_from_user( "B", b );
  add_matrices( a, b, c );  // c = a + b

  cout << "\n\nThe sum of Matrices A and B is:\n\n";
  display_matrix( c );
}

Be careful with your scratch variable names and with your loops. You didn't use [code] tags, but from the placement of the return statement in your add() function, I suspect you are not being careful with indentation.

You don't need <conio.h> in there. Please get rid of it.

Hope this helps.
Thanks for your help, but I'm sorry I can't clearly understand your reply. Can you please use my variables so I know exactly my errors.

Also,
* I did define all the matrices, why you ask me to define them?
* I have a cout statement in the add function to display the matrix why you want me to do a for loop?


However, I made some changes can you see this code now, please.
I think my main problem are

a) the function add "header"
b) calling the function add in the main


Thank you again!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include<fstream>


using namespace std;

int matrix1 ();
int matrix2 ();
void add ( int [], int []);

int main()
{

matrix1 ();
matrix2 ();
add ( matrix1[],  matrix2 [] );


return 0;
}



int matrix1 ( )
{
	const int i=3;
	const int j=3;
	int a[i][j];
	
 cout<<"\n\nEnter elements for Matrix A :::\n\n";
 
    for(int x=0;i<3;x++)
    {
        for(int y=0;j<3;y++)
        {
            cin>>a[i][j];
        }
        cout<<"\n";
    }
	return (a[i][j]);
}

 

int matrix2 ()
{
	const int i=3;
	const int j=3;
	int b[i][j];
	
 cout<<"\n\nEnter elements for Matrix A :::\n\n";
 
    for(int x=0;i<3;x++)
    {
        for(int y=0;j<3;y++)
        {
            cin>>b[x][y];
        }
        cout<<"\n";
    }
	return (b[i][j]);
}






void add ( a[],  b[] )
{

	const int i=3;
	const int j=3;
	int c[i][j];



	cout<<"\n\nAddition of the two Matrices  :\n\n";

    for(int x=0;i<3;x++)
    {
        for(int y=0;j<3;y++)
        {
            cout<<"\t"<<(a[x][y])+ (b[x][y]);
        }
        cout<<"\n\n";
    }
}




Last edited on
Topic archived. No new replies allowed.