Concatenating strings

I'm trying to concatenate two arrays after their multiplied together, and not only can I not figure how to combine the two arrays, but I'm experiencing an error in the add function. I cannot figure out why this is.

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
#ifndef _MATRIX_
#define _MATRIX_
#include <string>

class Matrix
{

	private:
		int a, b;
		int column;
		int r_value;
		int c_value;
		int row;
		int pos;
		int value;
		/*double *p[];
		double *q[];*/
		double ** matrix;
		double ** temporary_matrix;

	public:
	    Matrix();
		Matrix (int row, int column);
		int get_size();
		void set_value(int pos, int value);
		void get_value(int pos);
		void print_matrix();
		void concat(int k, int l, int a, int b);
		Matrix add(const Matrix& object, const Matrix& source);
		Matrix subtract(const Matrix& object, const Matrix& source);
		int Matrix::get_a();
		int Matrix::get_b();
		

	//copy constructor
	Matrix::Matrix( const Matrix& other )
	{
		//
	}
	
	//overloaded assignment operator
	void operator = (const Matrix& source) 
	{
		//
	}

  //destructor
  //~Matrix( ) 

};

#endif 


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include "ex62n.h"
using namespace std;


	int Matrix::get_size()
	{
		//Preconditions: a and b must be intialized
		//Postconditions: return (a*b)-1

		return ((a*b)-1);
	}

	int Matrix::get_a()
	{
		

		return a;
	}

	int Matrix::get_b() 
	{
		

		return b;
	}
	
	void Matrix::set_value(int pos, int value)
	{

		r_value = pos / b;
		c_value = pos * b;

		//sets the value in the matrix at given position

		if (pos <= get_size())
		{
			matrix[r_value][c_value] = value;
		}

		else 
		{
			temporary_matrix = new double * [r_value+1];

				for (int k = 0; k < r_value; k++)
				{
					matrix[k] = new double[b];
				}

				for (int k = 0; k < r_value; k++)
					{
						for (int l = 0; l < row; l++)
						{
							matrix[k][l] = -1.0;
						}
					}

				for (int k = 0; k < a; k++)
				{
					for (int l = 0; l < b; l++)
					{
						temporary_matrix[k][l] = matrix[k][l];
					}
				}

				temporary_matrix[r_value][c_value] = value;

				for (int  k= 0; k < a; ++k)
				{
					delete[] matrix[k];
				}
				delete [] matrix;

				a = r_value +1;
				matrix = temporary_matrix;
	}

	}

	void Matrix::get_value(int pos)
	{

		//which returns the current value at given position
		int r_point;
		int c_point;

		r_point = pos / b;
		c_point = pos * b;

		if (pos <= get_size())
		{
			cout << "Position value" << pos << ":" << matrix[r_point][c_point] << endl;
		}
		else 
		{
			cout << "Position Value" << pos << "can't be displayed";
		}
	}

	int main ()
	{

		Matrix m(2,2);
		//m.get_value(2);
		m.print_matrix();

		Matrix p(3,4);
		m.print_matrix();

		system ("PAUSE");
		return 0;
	}


	void Matrix::print_matrix()
	{

	cout << "First Matrix \n";
     for (int k = 0; k < a; k++)
     { cout << "for loop" <<endl;
          for (int l = 0; l < b; l++)
          {
               cout << " t " << matrix[k][l];
          }
          cout << "\n";
     }
     cout << "Second Matrix \n";
     for (int k = 0; k < a; k++)
     {
          for (int l = 0; l < b; l++)
          {
               cout << " t " << matrix[k][l];
          }
          cout << "\n";
     }

	}

	Matrix::add(const Matrix& object, const Matrix& source)
	{

		Matrix result(a,b);

			if(a == object.a && b ==source.b)
			{
				for (int k = 0; k < a; k++)
				{
					for (int l = 0; l < b; l++)
					{
						result.matrix[k][l] = object.matrix[k][l] + matrix[k][l];
					}
				}
			}
			
			else 
			{
				cout << "This operation can not be performed.";
			}
	}

	Matrix Matrix::subtract(const Matrix& object, const Matrix& source)
	{

		Matrix result(a,b);

			if(a == object.a && b ==source.b)
			{
				for (int k = 0; k < a; k++)
				{
					for (int l = 0; l < b; l++)
					{
						result.matrix[k][l] = object.matrix[k][l] - matrix[k][l];
					}
				}
				return result;
			}
			
			else 
			{
				cout << "This operation can not be performed.";
			}
			return result;
	}


	void Matrix::concat(int k, int l, int a, int b)
	{

		//Which concatenates two matrices together!!
		int * result = new int[k + l];
		copy(a, a + k, result);
		copy(b, b + l, result + k);
	}

	Matrix::Matrix (int row, int column)
	{

		a = row;
		b = column;

		/*cout << "enter dimmensions of array";
			cin >> a,b;*/

		matrix = new double*[row];

		for (int k = 0; k < row; k++)
		{
			matrix[k] = new double[column];
		}

		for (int k = 0; k < row; k++)
		{
			for (int l = 0; l < row; l++)
			{
				matrix[k][l] = -1.0;
			}
		}
	}
	Matrix::Matrix()
	{
		Matrix (1,1);
	}
Last edited on
> copy(a, a + k, result);

The above statement is of signature: copy(int, int, int *). It is incorrect way to use copy.

Signature of std::copy is
template <class II, class OI> OI copy (II first, II last, OI result);

Example for right use of copy:
int iarr1[]={1,2,3},iarr2;
std::copy ( iarr1, iarr1+3, iarr2);
@107 Matrix p(3,4);
@108 m.print_matrix();
print_matrix of Matrix object m is being used. Looks like the intent was to print Matrix object p.

@213 for (int l = 0; l < row; l++)
Incorrect logic. Use "column" instead of "row".
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
Matrix addition program. Uses vector internally.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

class matrix
{
  private:
    vector< vector<int> > two_d_arr;
    unsigned int get_no_of_rows() const { return two_d_arr.size(); };
    unsigned int get_no_of_cols() const { return (two_d_arr.front()).size(); };
  public:
    matrix(){}
    matrix(int row, int col) : two_d_arr(row, vector<int>(col) ) {}
    void init_with_rands();
    void print();
    matrix operator+ (const matrix&);
};

void matrix::init_with_rands()
{
  for (vector< vector<int> >::iterator i = two_d_arr.begin(); i != two_d_arr.end(); ++i)
    for (vector<int>::iterator j = (*i).begin() ; j != (*i).end(); ++j)
      *j = rand() % 9 + 2;
}

void matrix::print()
{
  cout << endl;
  for (vector< vector<int> >::iterator i = two_d_arr.begin(); i != two_d_arr.end(); ++i)
  {
    for (vector<int>::iterator j = (*i).begin(); j != (*i).end(); ++j)
      cout << "\t" << *j;      
    cout << endl;
  }
}

matrix matrix::operator+(const matrix &add_this)
{
  unsigned int row = two_d_arr.size();
  unsigned int col = (two_d_arr.front()).size();
  const unsigned int add_this_row = add_this.get_no_of_rows();
  const unsigned int add_this_col = add_this.get_no_of_cols();

  if (row != add_this_row || col != add_this_col)
  {
    cerr << "Matrix sizes do not match. Addition impossible.";
    return (*this);
  }
  
  matrix new_mat(row,col);
  
  vector< vector<int> >::iterator l = new_mat.two_d_arr.begin();
  for (vector< vector<int> >::const_iterator i = two_d_arr.begin(),
       k = add_this.two_d_arr.begin(); i != two_d_arr.end(); ++i, ++k, ++l)
  {
    vector<int>::iterator n = (*l).begin();
    for (vector<int>::const_iterator j = (*i).begin(), m = (*k).begin(); 
         j != (*i).end(); ++j, ++m, ++n)
           *n = *j + *m;
  }
  return (new_mat);
}

int main()
{
  srand(time(NULL)); // Initialize random number generator

  matrix m1(3,3); // Create 3x3 Matrix
  m1.init_with_rands(); // Fill the matrix with random numbers.
  cout << "Matrix m1:";
  m1.print(); // Print the above matrix
  
  matrix m2(3,3); // Create 3x3 Matrix
  m2.init_with_rands(); // Fill the matrix with random numbers.
  cout << endl << "Matrix m2:";
  m2.print(); // Print the above matrix
  
  matrix m3(3,3); // Create 3x3 Matrix
  m3 = m1 + m2; // Assign the result of addition of matrix 1 and matrix 2.
  cout << endl << "Matrix m3: (m1 + m2):";
  m3.print();
  
  return 0;
}
i want to learn c++ could any one teach me?
Topic archived. No new replies allowed.