TODO:
13 23 15 31 14
// phalanx.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
const int DIMENSION=5;
void initPhalanx(int (*p)[DIMENSION]){
int i,j,generated;
int *q;
time_t t;
q=*p;
//To generate random numbers
srand((unsigned)time(&t));
for(i=0;i<DIMENSION;i++){
for(j=0;j<DIMENSION;j++){
//To avoid duplicates here
Gen:
generated=int(rand()%100);
for(q=*p;q<*(p+i)+j;q++){
if(*q == generated){
//if there is an duplicate, to regenerate
goto Gen;
}
}
*(*(p+i)+j)=generated;
}
}
/*
srand( (unsigned)time( NULL ) );
i = int(rand());
j = int(rand());
*/
}
void exchangePos(int *p, int *q){
int temp;
temp=*p;
*p=*q;
*q=temp;
}
void sort(int (*p)[DIMENSION]){
int centerD=(DIMENSION-1)/2;
int i,j;
for(i=0;i<DIMENSION;i++){
for(j=0;j<DIMENSION;j++){
if(*(*(p+i)+j)>*(*(p+centerD)+centerD)){
exchangePos(*(p+i)+j,*(p+centerD)+centerD);
}else if(*(*(p+i)+j)<*(*(p+0)+0)){
exchangePos(*(p+i)+j,*(p+0)+0);
}else if(*(*(p+i)+j)<*(*(p+0)+(DIMENSION-1))){
exchangePos(*(p+i)+j,*(p+0)+(DIMENSION-1));
}else if(*(*(p+i)+j)<*(*(p+DIMENSION-1)+0)){
exchangePos(*(p+i)+j,*(p+DIMENSION-1)+0);
}else if(*(*(p+i)+j)<*(*(p+DIMENSION-1)+(DIMENSION-1))){
exchangePos(*(p+i)+j,*(p+DIMENSION-1)+(DIMENSION-1));
}
}
}
}
int main(int argc, char* argv[])
{
int i,j;
int init[DIMENSION][DIMENSION];
initPhalanx(init);
printf("Init Phalanx:/n");
for(i=0;i<DIMENSION;i++){
for(j=0;j<DIMENSION;j++){
printf(" %3d ",init[i][j]);
}
printf("/n");
}
printf("/nSorted Phalanx:/n");
sort(init);
for(i=0;i<DIMENSION;i++){
for(j=0;j<DIMENSION;j++){
printf(" %3d ",init[i][j]);
}
printf("/n");
}
return 0;
}