### Create a row and add it to matrix.

//matrix :
 ``12345`` ``````12 5 35 35 35 65 170 48 15 190 85 27 27 24 100 315 119 600 116 129 70 88 110 145 90 90 90 90 77 85 130 55 210 122 200``````

// the row :
 `` `` ``152 152 149 155 170``

// matrix after adding the previous row :
 ``123456`` ``````12 5 35 35 35 65 170 48 15 190 85 27 27 24 100 315 119 600 116 129 70 88 110 145 90 90 90 90 77 85 130 55 210 122 200 152 152 149 155 170``````

**NOTE : I'm beginner in c++ and in the uni. we use the pointer.

- Can I print out the irregular matrix?
Last edited on
You started anything?

Aceix.
Yes, It's part of final exam problem.
but I face the problem adding row to the matrix .
OK, that is just a try :

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include void main () { int **x; int y[5]; int row , col; cin >> row >> col; x = new int *[row]; for ( int r=0; r> x[r][c]; } for (int i=0; i<5; i++) { cin >> y[i]; } // TROUBLE HERE!!! x= new *[1]; for ( ii=0; ii<1; ii++) { x[ii]=new int [5]; } int k=0; for (i=0; i<1; i++) for ( c=0; c<5; c++) {y[k]=x[i][c]; k++; } } ``````

Without using pointer; might be helpful for you>
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include using namespace std; int main() { int matrix[5][7], add_row[7], i, j, k; //create the code with random number of rows & columns if you want... cout<<"Enter elements of the matrix:\n"; for(i=0; i<5; i++){ for(j=0; j<7; j++) cin>>matrix[i][j]; } cout<<"\nThe matrix is:\n"; for(i=0; i<5; i++){ for(j=0; j<7; j++) cout<>add_row[k]; cout<<"\nThe added row is:\n"; for(k=0; k<7; k++) cout<
Last edited on
Ok thank you.

But how I can print out the code if was in irregular matrix like that :
 ``1234`` ``````1 2 3 4 5 12 324 2456``````

* if I don't know the length of the matrix.
Maybe you can get a variable that update any time a line is added.

Aceix
@isimplyrefuse
If you dont know the number of elements in array, then you have to use dynamic memory allocation.
Learn it if you need.
You can use the following function which I wrote without testing.

 ``1234567891011121314151617181920`` ``````int ** AddRow( int ***a, size_t m, size_t n, int *r ) { int **new_a = new int *[m + 1]; for ( size_t i = 0; i < m + 1; i++ ) new_a[i] = new int[n]; for ( size_t i = 0; i < m; i++ ) { for ( size_t j = 0; j < n; j++ ) new_a[i][j] = ( *a )[i][j]; } for ( size_t j = 0; j < n; j++ ) new_a[m][j] = r[j]; for ( size_t i = 0; i < m; i++ ) delete [] ( *a )[i]; delete [] ( *a ); *a = new_a; return ( *a ); }``````
Last edited on
Topic archived. No new replies allowed.