Function to print a diamond shape

Jay Norwood jayn at prismnet.com
Fri Mar 21 05:43:26 PDT 2014


This one calculates, then outputs subranges of the ba and sa char 
arrays.

int n = 11;
int blanks[];
blanks.length = n;
int stars[];
stars.length = n;
char ba[];
ba.length = n;
ba[] = ' '; // fill full ba array
char sa[];
sa.length = n;
sa[] = '*'; // fill full sa array

int c = n/2; // center of diamond
int cp1 = c+1;
blanks[c]=0;
stars[c]=n;

// calculate stars and blanks in each row
for(int i=1; i<cp1; i++){
     blanks[c-i] = blanks[c+i] = i;
     stars[c-i] = stars[c+i] = n - (i*2);
}

// output subranges of the ba and sa char arrays
for (int i=0; i<n; i++){
     write(ba[$-blanks[i]..$]);
     writeln(sa[$-stars[i]..$]);
}


More information about the Digitalmars-d-learn mailing list