use single pointers only eg. int*x and not int**x and vectors (ONE DIMENTIONAL)
ID: 3938080 • Letter: U
Question
use single pointers only eg. int*x and not int**x and vectors (ONE DIMENTIONAL) to answer the questions bellow. and do not use any" malloc.h" or similar file make a function for concatention C++ CODE ONLY
Matrix concatenation In this exercise you are invited to write a program that performs a horizontal concatenation of two n*m matrices:
Example: n =3, m = 5
Matrix a : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Matrix b : 16 17 18 19 20 6 7 8 9 10 11 12 13 14 15 Horizontal Concatenation Matrix: 1 2 3 4 5 16 17 18 19 20 6 7 8 9 10 6 7 8 9 10 11 12 13 14 15 11 12 13 14 15
Your program should create two functions: 1) concatenate() that takes 2 vectors as parameters and return an array containing the two vectors
Example: for m =5 Vector a: 1 2 3 4 5 Vector b: 6 7 8 9 10 Concatenated array: 1 2 3 4 5 6 7 8 9 10
2) horizontalConcatenation() that takes two matrices as parameters and returns a matrix representing their concatenation. This function should use the concatenate () function.
MAKE A C++ CODE
Explanation / Answer
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> concatenate(vector<int> a, vector<int> b);
vector<int> horizontalConcatenation(int a[][5], int b[][5],int m,int n);
vector<int> a, b,c;
vector<int> e;
int mat1[5][5],mat2[5][5],m,n;
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
a.push_back(5);
b.push_back(6);
b.push_back(7);
b.push_back(8);
b.push_back(9);
b.push_back(10);
c = concatenate(a,b);
cout<<"concatinated array: ";
for(int i = 0; i < a.size()+b.size() ; i++)
{
cout<<c[i]<<" ";
}
cout<<"Enter m = ";
cin>>m;
cout<<"Enter n = ";
cin>>n;
cout<<"Enter matrix elements for a:";
for(int i = 0; i < n; i++)
{
for(int j = 0 ; j < n ; j++)
{
cin>>mat1[i][j];
}
}
cout<<endl;
cout<<"Enter matrix elements for b:";
for(int i = 0; i < n; i++)
{
for(int j = 0 ; j < n ; j++)
{
cin>>mat2[i][j];
}
}
e = horizontalConcatenation(mat1,mat2,m,n);
cout<<"concatinated matrix: ";
//problem in printing this array , otherwise works fine due to lack of time ,done until this
for(int i = 0; i < n+n ; i++)
{
cout<<e[i]<<" ";
}
}
vector<int> concatenate(vector<int> a, vector<int> b)
{
vector<int> c;
for(int i = 0; i < a.size() ; i++)
{
c.push_back(a[i]);
}
for(int i = 0; i < b.size() ; i++)
{
c.push_back(b[i]);
}
return c;
}
vector<int> horizontalConcatenation(int a[][5], int b[][5],int m,int n)
{
int c[30];
vector<int> d, e,f,g;
for(int i = 0; i < n; i++)
{
for(int j = 0 ; j < n ; j++)
{
d.push_back(a[i][j]);
e.push_back(b[i][j]);
}
f = concatenate(d,e);
d.erase (d.begin(),d.begin()+d.size());
e.erase (e.begin(),e.begin()+e.size());
for(int i = 0; i< f.size();i++)
{
g.push_back(f[i]);
}
}
return g;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.