Create a row and add it to matrix.

Apr 7, 2013 at 2:00pm
//matrix :
1
2
3
4
5
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 :
1
2
3
4
5
6
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 Apr 7, 2013 at 2:16pm
Apr 7, 2013 at 2:09pm
You started anything?

Aceix.
Apr 7, 2013 at 2:15pm
Yes, It's part of final exam problem.
but I face the problem adding row to the matrix .
Apr 7, 2013 at 4:24pm
OK, that is just a try :

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
#include <iostream.h>
void main ()
{
int **x;
int y[5];
int row , col;
cin >> row >> col;
x = new int *[row];
for ( int r=0; r<row; r++)
{
x[r]= new int [col];
}
for (r=0; r<n; r++)
for (c=0; c<m; c++)
{
cin >> 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++;
}
}


Apr 7, 2013 at 6:19pm
Without using pointer; might be helpful for you>
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
#include <iostream>
#include <conio.h>
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<<matrix[i][j]<<" ";
    cout<<endl;
    }
    
    cout<<"\n\nEnter elements of the row that you want to add:\n";
    for(k=0; k<7; k++)
             cin>>add_row[k];
             
    cout<<"\nThe added row is:\n";
    for(k=0; k<7; k++)
             cout<<add_row[k]<<" ";
             
    cout<<"\n\nMatrix after adding the row is:\n";
    for(i=0; i<6; i++){
             if(i==5){
             for(k=0; k<7; k++)
                      cout<<add_row[k]<<" ";
             }
             
             else{
             for(j=0; j<7; j++)
                      cout<<matrix[i][j]<<" ";
             }
    cout<<endl;
    }
    
    getch();
    system ("Pause");
    return 0;
}
Last edited on Apr 7, 2013 at 6:32pm
Apr 8, 2013 at 5:30am
Ok thank you.

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


* if I don't know the length of the matrix.
Apr 8, 2013 at 6:01am
Maybe you can get a variable that update any time a line is added.

Aceix
Apr 8, 2013 at 9:27am
@isimplyrefuse
If you dont know the number of elements in array, then you have to use dynamic memory allocation.
Learn it if you need.
Apr 8, 2013 at 10:39am
You can use the following function which I wrote without testing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 Apr 8, 2013 at 10:39am
Topic archived. No new replies allowed.