rendered paste body#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIMSIZE 5
void fillTriangularMatrixRandom(int A[][DIMSIZE]);
void display2DMatrix(int A[][DIMSIZE]);
void transformTriangular(int A[][DIMSIZE], int B[]);
void setValue(int i, int j, int val, int B[]);
void display1DMatrix(int B[], int length);
void display1DTriangularTo2DMatrix(int B[]);
main()
{
int A[DIMSIZE][DIMSIZE];
int B[DIMSIZE*(DIMSIZE+1)/2]; //yparxoyn N(N+1)/2 stoixeia poy briskontai
//apo th diagonio kai kato
int index,value;
srand(time(0));
printf("PINAKAS A");
fillTriangularMatrixRandom(A);
printf("\nArxikopoihsh me random times oloklhrothike\n");
printf("\nPINAKAS A\n");
printf("Ta stoixeia tou pinaka einai:\n");
display2DMatrix(A);
transformTriangular(A,B);
printf("\nPINAKAS B monodiastatos");
display1DMatrix(B, DIMSIZE*(DIMSIZE+1)/2);
printf("\n\nPINAKAS B antistoixos kato trigonikos");
display1DTriangularTo2DMatrix(B);
do {//to index prepei na einai entos orion toy B[]
printf ("\nDose th thesh toy stoixeioy sto monodiastato pinaka poy thes na allakseis: ");
scanf("%d", &index);
printf ("\nDose thn timh poy thes na exei: ");
scanf("%d", &value);
}
while (index<0 || index>=DIMSIZE*(DIMSIZE+1)/2);
B[index]=value; //allazoyme to stoixeio ston pinaka B
printf("\nPINAKAS B monodiastatos");
display1DMatrix(B, DIMSIZE*(DIMSIZE+1)/2);
printf("\n\nPINAKAS B antistoixos kato trigonikos");
display1DTriangularTo2DMatrix(B);
}
void fillTriangularMatrixRandom(int A[][DIMSIZE])
{
int i,j;
for(i=0; i<DIMSIZE; i++)
for(j=0; j<DIMSIZE; j++)
if(i>=j)
A[i][j] = rand()%10;
else A[i][j] = 0;
}
void display2DMatrix(int A[][DIMSIZE])
{
int i,j;
for(i=0; i<DIMSIZE; i++){
for(j=0; j<DIMSIZE; j++)
printf("%d ",A[i][j]);
printf("\n");
}
}
void transformTriangular(int A[][DIMSIZE], int B[])
{
int i,j;
for(i=0; i<DIMSIZE; i++)
for(j=0; j<DIMSIZE; j++)
if(i>=j)
setValue(i,j,A[i][j],B);
}
void setValue(int i, int j, int val, int B[])
{
B[i*(i+1)/2+j] = val;
}
void display1DMatrix(int B[], int length)
{
for(length=0; length<=DIMSIZE*(DIMSIZE+1)/2; length++)
printf("%d ",B[length]);
}
void display1DTriangularTo2DMatrix(int B[])
{
int i,j;
for(i=0; i<DIMSIZE; i++){
printf("\n");
for(j=0; j<DIMSIZE; j++)
if(i>=j) printf("%d ",B[i*(i+1)/2+j]);
else printf("%d ",0);
}
}