-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcross_square_pattern.c
52 lines (44 loc) · 1.68 KB
/
cross_square_pattern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
int currentRow, currentColumn, patternSize;
char userChoice;
int inputIsValid;
do {
// Clear screen (more portable approach)
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
printf("\n================== Pattern: Cross Inside a Square ==================\n");
printf("\nPlease enter the size of the pattern: ");
// Improved input validation
inputIsValid = scanf("%d", &patternSize);
while(getchar() != '\n'); // Clear input buffer
if(inputIsValid != 1 || patternSize < 1) {
printf("\nError: Please enter a positive number\n");
} else {
// Print the pattern
for(currentRow = 1; currentRow <= patternSize; currentRow++) {
for(currentColumn = 1; currentColumn <= patternSize; currentColumn++) {
// Square outline or diagonal crosses
if(currentRow == 1 || currentColumn == 1 ||
currentRow == patternSize || currentColumn == patternSize ||
currentRow == currentColumn || currentColumn == (patternSize - currentRow + 1)) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
}
printf("\nPress 0 to exit or any other key to continue: ");
userChoice = getchar();
while(getchar() != '\n'); // Clear input buffer again
} while(userChoice != '0');
return 0;
}