Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to write a program that:
1) Generates quadratic matrix of size (2Nx2N)
2) Prints this matrix
3) Swaps the quarters of the matrix clockwise (beginning from top left corner)
4) Prints changed matrix.

For example, the output of my program should be like this:

01 02 03 04 
05 06 07 08 
09 10 11 12
13 14 15 16

Matrix (changed)

09 10 01 02
13 14 05 06
11 12 03 03 
15 16 07 08

Here is my source code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;

const int n = 4; // size of matrix

void CreateMatrix(int matrix[][n], int n); // functions' prototypes
void PrintMatrix(int matrix[][n], int n);
void ProcessMatrix(int matrix[][n], int n);
int main()
    int matrix[n][n];


    CreateMatrix(matrix, n);    //create matrix
    PrintMatrix(matrix, n);     //print matrix
    ProcessMatrix(matrix, n);   //change matrix


    PrintMatrix(matrix, n);    // print changed matrix

void CreateMatrix(int matrix[][n],int n)
    for (int i =0; i<n; i++)
        for(int j = 0; j<n; j++)
            matrix[i][j] = rand()%10;
void PrintMatrix(int matrix[][n], int n)
    for(int i=0; i<n; i++)
        for (int j = 0; j < n; j++)
            cout<<matrix[i][j]<<" ";
void ProcessMatrix(int matrix[][n], int n) 
    // I don't know how to write this function

So my question is how to write ProcessMatrix function?
Any help is appreciated!
Updated 27-Dec-14 9:41am

Hey, I liked your question so I thought i should work on it. Here m adding the code to change matrix as shown by you in example.
Here suppose 'a' is your original matrix of size (2N x 2N) and I have take one `tmp` matrix to store value. it will also be of same size
initialize tmp matrix at the time of declaration, like int tmp[2*n][2*n]={0};
for(int i=0;i<2*n;i++){
	for(int j=0;j<2*n;j++)
		if(i<n && j<n)

		else if(i<n && j>=n)

		else if(i>=n && j<n)

		else if(i>=n && j>=n)
Share this answer
Kiber 1-Jan-15 10:21am    
Hi! Thanks, you helped me a lot
Member 10641779 1-Jan-15 22:58pm    
u r welcome, Happy New Year :)
Kiber 2-Jan-15 23:23pm    
Happy New Year to you, too!
Doraemon Kids 26-Nov-20 14:06pm    
can you do this in c++
Last few weeks i saw at least few of similar questions[^]. Please, follow them.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900