#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void nl(void) { //function to print new line
printf("\n");
}
int** allocate(_size) {
int** matrix=(int**)malloc(sizeof(int*)*_size);
for(int i=0;i<_size;i++) {
matrix[i]=(int*)malloc(sizeof(int)*_size);
}
return matrix;
}
void scanMatrix(_matrix,_size)
int** _matrix;
{
printf("Enter the values: ");
for(int i=0;i<_size;i++) {
for(int j=0;j<_size;j++) {
scanf("%d",&_matrix[i][j]);
}
}
}
void printMatrix(_matrix,_size)
int** _matrix;
{
for(int i=0;i<_size;i++) {
for(int j=0;j<_size;j++) {
printf("%d ",_matrix[i][j]);
}
nl();
}
}
void swap(num1,num2)
int* num1;
int* num2;
{
*num1=*num1+*num2;
*num2=*num1-*num2;
*num1=*num1-*num2;
}
void rotateMatrix(_matrix,_size)
int** _matrix;
{
for(int i=0;i<_size;i++) {
for(int j=i+1;j<_size;j++) {
swap(&_matrix[i][j],&_matrix[j][i]);
}
}
for(int i=0;i<_size;i++) {
for(int j=0;j<_size/2;j++) {
swap(&_matrix[i][j],&_matrix[i][_size-1-j]);
}
}
}
main(argc,argv)
const char** argv;
{
int size=0;
printf("Enter the size of the square matrix: ");
scanf("%d",&size);
int** matrix=allocate(size);
scanMatrix(matrix,size);
printMatrix(matrix,size);
nl();
rotateMatrix(matrix,size);
nl();
printMatrix(matrix,size);
return 0;
}
This code doesn't use extra memory and also uses run-time allocation using malloc
. However, has a time complexity of O(n^2). How do I further optimise it?
The C programming language
. Don't use it if possible :) \$\endgroup\$