Created
September 24, 2017 08:08
-
-
Save thinkphp/8bb41f8cb1bca24db5325fdc5eb1ca8a to your computer and use it in GitHub Desktop.
Pointers to Matrix in C++. int (*mat)[10]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <ctime> | |
using namespace std; | |
void *readMatrix(int l,int c) { | |
srand (time(NULL)); | |
int (*mat)[10] = new int[10][10]; | |
for(int i = 0; i < l; ++i) { | |
for(int j = 0; j < c; ++j) { | |
int c = rand()%10; | |
mat[ i ][ j ] = c; | |
} | |
} | |
return mat; | |
} | |
void *sumMatrix(int (*mat1)[10], int (*mat2)[10], int l,int c) { | |
srand (time(NULL)); | |
int (*mat)[10] = new int[10][10]; | |
for(int i = 0; i < l; ++i) { | |
for(int j = 0; j < c; ++j) { | |
int c = rand()%10; | |
mat[ i ][ j ] = mat2[ i ][ j ] + mat2[ j ][ j ]; | |
} | |
} | |
return mat; | |
} | |
void printMatrix(int (*mat)[10],int l,int c) { | |
cout<<"Matrix"<<endl; | |
for(int i = 0; i < l; ++i) { | |
for(int j = 0; j < c; ++j) { | |
cout<<mat[i][j]<<" "; | |
} | |
cout<<endl; | |
} | |
} | |
int main( void ) { | |
int (*adr1)[10], (*adr2)[10], (*sum)[10]; | |
int l = 3, c = 3; | |
adr1 = (int(*)[10])readMatrix(3,3); | |
adr2 = (int(*)[10])readMatrix(3,3); | |
printMatrix(adr1, l, c); | |
printMatrix(adr2, l, c); | |
sum = (int(*)[10])sumMatrix(adr1, adr2, l, c); | |
printMatrix(sum, l, c); | |
return(0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment