I'm trying to write a C program that prints a diamond-shaped pattern where the side length (measured in @ symbols) is provided by the user. The program should follow these rules:
The maximum side length is 20, and the minimum is 1. The pattern alternates between @, ., and o following a specific sequence. For example, for different lengths, outputs should be this ones:
Length 1 @:
@
Length 2 @'s:
@
@.@
@
Length 5 @'s:
@
@.@
@.o.@
@.o.o.@
@.o.@.o.@
@.o.o.@
@.o.@
@.@
@
Length 6 @'s:
@
@.@
@.o.@
@.o.o.@
@.o.@.o.@
@.o.@.@.o.@
@.o.@.o.@
@.o.o.@
@.o.@
@.@
@
This is what I've got for now:
#include <stdio.h>
int main(){
int length;
int counter;
printf("¿Length of the rhombus?: ");
scanf("%d", &length);
if(length > 20 || length < 1){
printf("The length must be at least 1 and maximum 20.");
return 1;
}
else if(length == 1){
printf("@");
return 0;
}
else{
/*-- Create as many lines as "length" value --*/
for(int i = 1; i <= length; i++){
/* Upper left triangle */
/* -------------------------------------------------------- */
/* Create enough blank spaces before the character sequence */
for(int j = 1; j <= length-i; j++){
printf(" ");
}
/* Create the sequence "@.o.@" */
counter = 0;
for(int j = 1; j <= i; j++){
if(counter % 4 == 0){
printf("@");
}
else if(counter % 4 == 1){
printf(".");
}
else if(counter % 4 == 2){
printf("o");
}
else{
printf(".");
}
counter++;
}
/* -------------------------------------------------------- */
/* Upper right triangle*/
/* -------------------------------------------------------- */
//I don't know what to do here
/* -------------------------------------------------------- */
printf("\n");
}
}
}
What could I add to build the right part of the rhombus? (And I would appreciate any suggestions to improve my way of reasoning for future problems)
This is what i get with length = 10,
@
@.
@.o
@.o.
@.o.@
@.o.@.
@.o.@.o
@.o.@.o.
@.o.@.o.@
@.o.@.o.@.
And what I what I don't know, is what should I add to the code now to get this:
@
@.@
@.o.@
@.o.o.@
@.o.@.o.@
@.o.@.@.o.@
@.o.@.o.@.o.@
@.o.@.o.o.@.o.@
@.o.@.o.@.o.@.o.@
@.o.@.o.@.@.o.@.o.@
Let's marry the two together. Assuming we already have a length
variable, this is how you can proceed:
int i, j, limit = 0;
//rows
for (i = 0; i <= 2 * length; i++) {
char alternating[] = {'.', 'o'};
int alternatingIndex = 0;
//columns
for (j = 0; j <= 2 * length; j++) {
//out of bounds
if ((j < length - limit) || (j > length + limit)) printf(" ");
//on bounds
else if ((j == length - limit) || (j == length + limit)) printf("@");
//alternating characters
else {
//display current character
printf("%c", alternating[alternatingIndex]);
//alternate to the next, since 1 - 0 = 1 and 1 - 1 = 0
alternatingIndex = 1 - alternatingIndex;
}
}
//moving limit to the right direction
limit += ((i < length) ? 1 : -1) * 1;
//newline
printf("\n");
}
Comments are in-between the lines. I'm not sure what is the criteria to display @
at non-bound positions, so I ignored that part until I get further clarification. So, with length being 10, this is what we see:
@
@.@
@.o.@
@.o.o.@
@.o.o.o.@
@.o.o.o.o.@
@.o.o.o.o.o.@
@.o.o.o.o.o.o.@
@.o.o.o.o.o.o.o.@
@.o.o.o.o.o.o.o.o.@
@.o.o.o.o.o.o.o.o.o.@
@.o.o.o.o.o.o.o.o.@
@.o.o.o.o.o.o.o.@
@.o.o.o.o.o.o.@
@.o.o.o.o.o.@
@.o.o.o.o.@
@.o.o.o.@
@.o.o.@
@.o.@
@.@
@
If from this you can figure out where else the @ is to be displayed, let me know in the comments. If you do not manage to implement it, then specify it in plain words.
EDIT
Applied the change for alternation:
int i, j, limit = 0, length = 10;
//rows
for (i = 0; i <= 2 * length; i++) {
char alternating[] = {'.', 'o', '.', '@'};
int alternatingIndex = 0;
//columns
for (j = 0; j <= 2 * length; j++) {
//out of bounds
if ((j < length - limit) || (j > length + limit)) printf(" ");
//on bounds
else if ((j == length - limit) || (j == length + limit)) printf("@");
//alternating characters
else {
//display current character
printf("%c", alternating[alternatingIndex]);
//alternate to the next (+ 1) or previous (+ 3)
alternatingIndex = (alternatingIndex + ((j < length) ? 1 : 3)) % 4;
}
}
//moving limit to the right direction
limit += ((i < length) ? 1 : -1) * 1;
//newline
printf("\n");
}