-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw1.c
50 lines (43 loc) · 1.03 KB
/
hw1.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
// Kim Merchant
// Assignment 1: Diamond
// Create a diamond of characters with a user-specified number of lines
#include <stdio.h>
int askUser() {
int userNum;
printf("Enter non-negative, odd integer n: ");
scanf("%d", &userNum);
if (userNum <= 0 || userNum % 2 == 0) {
printf("Error: ");
askUser();
}
else return userNum;
}
void addSpaces(int spc) {
for (int i = 0; i < spc; i++) {
printf(" ");
}
}
void addSymbol(char sym, int number) {
for (int i = 0; i < number; i++) {
printf("%c", sym);
}
printf("\n");
}
int main() {
char symbols[] = "!@#$\%^&*-/\\=)(~`\";:?";
int number = askUser();
int spaces = number / 2;
int quantity = 1;
for (int i = 0; i < number; i++) {
int sym = i % 20;
addSpaces(spaces);
addSymbol(symbols[sym], quantity);
if (i < number / 2) {
spaces--;
quantity = quantity + 2;
} else {
spaces++;
quantity = quantity - 2;
}
}
}