Thread: How do I print this matrix defined with pointers? PLEASE HELPPP

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    17

    How do I print this matrix defined with pointers? PLEASE HELPPP

    I defined this matrix with pointers, but I couldn't figure out how to print like a normal matrix print. like :


    Code:
    for (i = 0; i < row; i++)
    	{
    		for (j = 0; j < col; j++)
    			cout << M[i][j] << "\t";
    
    
    		cout << "\n";
    	}


    /////////////////////MY CODE/////////////////////////////

    Code:
    #include<iostream>
    #include<math.h>
    #include<time.h>
    using namespace std;
    #define SIZE 20
    
    
    void print_matrix2(int M[][SIZE], int a);
    
    
    int main() {
    	int M[SIZE][SIZE] = {};
    	int N[SIZE][SIZE] = {};
    	int P[SIZE][SIZE] = {};
    	int* ptrMatris = &M[0][0];
    	int* ptrMatris2 = &N[0][0];
    	int* ptrMatris3 = &P[0][0];
    	int size, a,i;
    	srand(time(NULL));
    
    
    	cout << "enter size: " << endl;
    	cin >> size;
    
    
    	a = sqrt(size);
    
    
    	for (i = 0;i < size;i++)
    	{
    		*(ptrMatris+i) = rand() % 11;
    		*(ptrMatris2 + i) = rand() % 11;
    	}
    	
    	
    	/*cout << "----MATRIX - 1----" << endl;
    	print_matrix2(M, a);
    	cout << endl;
    	cout << "----MATRIX - 2----" << endl;
    	print_matrix2(N, a);
    	cout << endl;
    	*/
    	
    	system("pause");
    }
    
    
    void print_matrix2(int M[][20],int a)
    {
    	int i, j;
    	for (i = 0; i < a; i++)
    	{
    		for (j = 0; j < a; j++)
    			cout << M[i][j] << "\t";
    
    
    		cout << "\n";
    	}
    }

  2. #2
    Registered User
    Join Date
    Dec 2019
    Posts
    17
    I've been trying to solve this problem for 2 days but I couldn't find it. Please help!!!!!!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    You've "flattened" your matrix into a vector, so if you want to print the vector as a matrix again, you need to compute the index of each entry in the matrix. It seems like a waste of effort when you can directly use the 2D array as the matrix.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    17
    yes I think you are right. So how do I get the product of two matrices in this way with the help of pointers?
    Quote Originally Posted by laserlight View Post
    You've "flattened" your matrix into a vector, so if you want to print the vector as a matrix again, you need to compute the index of each entry in the matrix. It seems like a waste of effort when you can directly use the 2D array as the matrix.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    > int M[SIZE][SIZE]
    You have a block of memory that looks like this, if you were to subscript it.
    In actuality, it's all just one contiguous block of SIZE*SIZE*sizeof(int) bytes.

    This is what it looks like for SIZE=10
    Code:
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    > cin >> size;
    > a = sqrt(size);
    OK, say you input 9 for size, so a will be 3.
    Then run your loops.

    If you flatten the matrix, this is what you get in memory.
    Code:
    RRRRRRRRR.
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    If you didn't flatten it, you would have this instead.
    Code:
    RRR.......
    RRR.......
    RRR.......
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    Now this is something you could just pass to
    void print_matrix2(int M[][20],int a)
    without any further effort.

    So why can't you do this in main?
    Code:
    for (i = 0; i < a; i++)
    	for (j = 0; j < a; j++)
    		M[i][j] = rand() % 11;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print the matrix in diagonal order - C program
    By saran.geevee in forum C Programming
    Replies: 2
    Last Post: 05-11-2012, 06:02 AM
  2. using user-defined function, do/while loop, to print name
    By jackson6612 in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2011, 06:02 AM
  3. Print matrix
    By coden00b in forum C Programming
    Replies: 4
    Last Post: 10-20-2009, 11:57 AM
  4. Sum of User Defined Matrix Powers
    By QuaX in forum C Programming
    Replies: 1
    Last Post: 04-19-2009, 11:33 PM
  5. Create a matrix with user-defined dimensions
    By EliasK in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 07:09 AM

Tags for this Thread