Write a program that inputs an integer and a character. The output should be a diamond composed of the character and extending the width specified by the integer. For example, if the integer is 11 and the character is an asterisk, the diamond would look pic related.
Not trying to get Jow Forums to do my homework. I'm a NEET learing to c++ and I just want to see other people's approaches. Can you do it with nested for loops? Thanks.
This is C, not C++, It's ugly, does not check for errors, but it works. #include #include
void print_line(const int w, const char c, const int i) { char out[w+1]; const int num_chars = 2 * (i-1) + 1; const int l = (w - num_chars) / 2; const int m = l + num_chars; const int r = w;
for (int k = 0; k < l; ++k) out[k] = ' '; for (int k = l; k < m; ++k) out[k] = c; for (int k = m; k < r; ++k) out[k] = ' '; out[w] = '\0'; printf("%s\n", out); }
int main(int argc, char **argv) { int i = atoi(argv[1]); char c = *argv[2]; const int end = i/2 + 1; for (int k = 1; k < end; ++k) print_line(i, c, k); for (int k = end; k > 0; --k) print_line(i, c, k); return 0; }
Charles Rodriguez
#include
int main() { int i, j, k; int n = 11; char character = '*';
k = n / 2; for (i = 1; i < n; i = i + 2) { for (j = k; j > 0; j--) { std::cout
Quite impressive. Here is my solution: import sys c, n = sys.argv[1], int(sys.argv[2]) x = ['{:{}^{}}'.format(c*(2*i+1), ' ', n) for i in range(n//2)] [print(i) for i in x + [c*n]*(n%2) + x[::-1]]
Nolan Richardson
Rate a=:' '&,@[{~(>:[:+/~|@i:)@
Connor Carter
Not the best solution but it werks
import System.Environment
triangleUp :: Int -> Char -> [String] triangleUp size char = [s ++ c ++ s | x