how to print diamond shape pattern like this?
the pattern isn't commonly using "*" only but it't modified with "."
The input:
5
The output:
....0.....
...000....
..0.0.0...
.0..0..0..
000000000.
.0..0..0..
..0.0.0...
...000....
....0.....
Here my code:
#include <stdio.h>
int main()
{
int number, i, k, count = 1;
scanf("%d", &number);
count = number - 1;
for (k = 1; k <= number; k++)
{
for (i = 1; i <= count; i++)
printf(".");
count--;
for (i = 1; i <= 2 * k - 1; i++)
printf("0");
printf("\n");
}
count = 1;
for (k = 1; k <= number - 1; k++)
{
for (i = 1; i <= count; i++)
printf(".");
count++;
for (i = 1 ; i <= 2 *(number - k)- 1; i++)
printf("0");
printf("\n");
}
return 0;
}
Think of a Cartesian coordinate system whose origin is the center of symmetry of the diamond pattern to be drawn. The 0s reside in the lines y = x + (n - 1), y = -x + (n - 1), y = x - (n - 1), y = -x - (n - 1), and the axes y = 0, x = 0. Then we can easily codify this information to draw the diamond pattern:
#include <stdio.h>
void print_diamond (int n)
{
--n;
for (int y = n; y >= -n; --y) {
for (int x = -n; x <= n; ++x)
putchar(x == 0 || y == 0
|| y == x + n || y == -x + n || y == x - n || y == -x - n
? '0' : '.');
putchar('\n');
}
}
int main (void)
{
print_diamond(5);
}
The line
|| y == x + n || y == -x + n || y == x - n || y == -x - n
can be reduced to
|| abs(x - y) == n || abs(x + y) == n
after stdlib.h is #included.
I rapidly wrote this for fun keeping your approach, however this is not the most efficient nor the most elegant way of doing this
int number, i, k, count = 1;
scanf("%d", &number);
for (k = 0; k < number-1; k++){
i = 1;
while(i < number-k){
printf(".");
i++;
}
printf("0");
i++;
while(i< number){
printf(".");
i++;
}
if(i==number){
printf("0");
i++;
}
while(i<number+k){
printf(".");
i++;
}
if(k>0){
printf("0");
i++;
}
while(i<number*2){
printf(".");
i++;
}
printf("\n");
}
You will probably find better answers if you search a bit deeper
Also, you can just
void printpir(int number, int k){
int i = 1;
while(i < number-k){
printf(".");
i++;
}
printf("0");
i++;
while(i< number){
printf(".");
i++;
}
if(i==number){
printf("0");
i++;
}
while(i<number+k){
printf(".");
i++;
}
if(k>0){
printf("0");
i++;
}
while(i<number*2){
printf(".");
i++;
}
printf("\n");
}
int main(int argc, char** argv) {
int number, i, k, count = 1;
scanf("%d", &number);
for (k = 0; k < number-1; k++){
printpir(number, k);
}
for(i= 1; i<number*2; i++){
printf("0");
}
printf("\n");
k--;
for (; k >= 0; k--)
{
printpir(number, k);
}
return 0;
}
Here is a solution with only 2 for-loops. I can explain my answer if you need it.
Hope you can understand...
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, _i, i, _j, j, k;
printf("Number: ");
scanf("%d", &n);
for (_i = 1; _i < 2*n; _i++) {
i = abs(_i - n);
for (_j = 1; _j <= 2*n; _j++) {
j = abs(_j - n);
if (j < n-i) {
if (i==0 || j == 0 || (i+j==n-1)) {
printf("0");
} else {
printf(".");
}
} else {
printf(".");
}
}
printf("\n");
}
return 0;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I am trying to practice my coding skills and recreate the minesweeper game in visual studio 2022. I am having trouble coding how many mines are touching a square within my grid. It keeps coming up with random numbers instead of how many mine it is touching. Hoping someone can point me in the right direction of where I am currently going wrong.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLUMNS 10
#define MINES 10
int grid[ROWS][COLUMNS];
int revealed[ROWS][COLUMNS];
void init()
{
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLUMNS; j++)
{
grid[i][j] = 0;
revealed[i][j] = 0;
}
}
}
void placeMines()
{
int i, x, y;
for (i = 0; i < MINES; i++)
{
x = rand() % ROWS;
y = rand() % COLUMNS;
if (grid[x][y] == -1)
{
i--;
}
else
{
grid[x][y] = -1;
}
}
}
void countAdjacentMines()
{
int i, j, count;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLUMNS; j++)
{
if (grid[i][j] == -1)
{
continue;
}
count = 0;
if (i > 0 && j > 0 && grid[i - 1][j - 1] == -1) count++;
if (i > 0 && grid[i - 1][j] == -1) count++;
if (i > 0 && j < COLUMNS - 1 && grid[i - 1][j + 1] == -1) count++;
if (j < COLUMNS - 1 && grid[i][j + 1] == -1) count++;
if (i < ROWS - 1 && j < COLUMNS - 1 && grid[i + 1][j + 1] == -1) count++;
if (i < ROWS - 1 && grid[i + 1][j] == -1) count++;
if (i < ROWS - 1 && j > 0 && grid[i + 1][j - 1] == -1) count++;
if (j > 0 && grid[i][j - 1] == -1) count++;
grid[i][j] = count;
}
}
}
void printGrid()
{
int i, j;
printf(" ");
for (i = 0; i < COLUMNS; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 0; i < ROWS; i++)
{
printf("%d ", i);
for (j = 0; j < COLUMNS; j++)
{
if (revealed[i][j])
{
if (grid[i][j] == -1)
{
printf("* ");
}
else
{
printf("%d");
}
}
else
{
printf("X ");
}
}
printf("\n");
}
}
void main()
{
int x, y;
srand((unsigned int)time(NULL));
init();
placeMines();
countAdjacentMines();
while (1)
{
printGrid();
printf("Enter the coordinates of the cell to reveal (x y): ");
scanf("%d %d", &x, &y);
if (x < 0 || x >= ROWS || y < 0 || y >= COLUMNS)
{
printf("Invalid coordinates!\n");
}
else if (revealed[x][y])
{
printf("Cell already revealed!\n");
}
else
{
revealed[x][y] = 1;
if (grid[x][y] == -1)
{
printf("You hit a mine! Game over.\n");
break;
}
}
}
return 0;
}
Look at the print grid function, %d requires a value, in this case i suppose it is
printf("%d ",grid[i][j]);
judging by the rest of the code.
Also I recommend learning how to use gdb for these purposes, placing breakpoints and seeing how the code evolves, or even using the gcc flag -Wall, to find some minor errors that can cause these such anomalies.
I'm trying to write a program that prints out a christmas tree that looks like this:
The user inputs the height, in this example the height is 6. If the input is in range from 0 to 3, the height should be 3, because otherwise it's not printable, and if the input is less than 0, the program should terminate.
My code for some odd reason is infinitely printing the 'Input height'. Where is the error?
Here's my code snippet:
#include <stdio.h>
void main(){
int i, j, n, s;
while (1){
printf("Input height: ");
scanf("%d", &n);
if (n < 0) break;
if (n == 0 || n == 1 || n == 2 || n == 3)
s == 3;
else
s == n;
for (i = 0; i < s; i++){
for (j = 0; j < 2*s - 1; j++){
if (j > s - (i - 1) && j < (s + (i - 1)) - 1)
printf("*.");
if (j == s + (i - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}
for (j = 0; j < 2*s - 1; j++){
if (j == s - 1 || j == s || j == s + 1)
printf("*");
else
printf(" ");
}
}
}
The lines: s == 3; and s == n; do absolutely nothing.
== is a comparison, not an assignment.
Here is much better code:
#include <stdio.h>
int main(void) {
int n = 8;
char row[2*n];
for( int i=0; i<2*n-1; i+=2 )
{
strcpy(row+i, "*.");
}
for(int i=0; i<n; ++i)
{
printf("%*.*s\n", n+i+1, 2*i+1, row);
}
printf("%*s\n", n+2, "***");
return 0;
}
Result:
Success #stdin #stdout 0s 5464KB
*
*.*
*.*.*
*.*.*.*
*.*.*.*.*
*.*.*.*.*.*
*.*.*.*.*.*.*
*.*.*.*.*.*.*.*
***
With a little creativity, I made the program even shorter with only a single for-loop.
#include <stdio.h>
int main(void) {
int n = 8;
char row[2*n];
strcpy(row, "*");
for( int i=0; i<n; ++i )
{
printf("%*s\n", n+i, row);
strcat(row, ".*");
}
printf("%*s\n", n+1, "***");
return 0;
}
As mentioned by others there are some issues with you mixing up == and =.
I will be posting a version that prints out the christmas tree but leaves out the . that you also want to include, as you should be able to finish it yourself.
#include <stdio.h>
int main()
{
int i, j, n, s;
while (1)
{
printf("Input height: ");
scanf("%d", &n);
// if input is negative, exit
if (n < 0)
{
break;
}
// if input is 0,1,2 or 3 change to 3
if (n == 0 || n == 1 || n == 2 || n == 3)
{
s = 3;
}
else
{
s = n;
}
// loop through each row
for (i = 0; i < s; i++)
{
// loop through each column
for (j = 0; j < 2 * s - 1; j++)
{
// if column is within the tree print a star
if (j >= s - i - 1 && j <= s + i - 1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
// print base of tree
for (j = 0; j < 2 * s - 2; j++)
{
// if column is part of base print star
if (j == s - 2 || j == s - 1 || j == s)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
A simple solution:
#include <stdio.h>
int main(){
int i=0, j=0, n=0, s=0;
while (1){
printf("Input height: ");
scanf("%d", &n);
printf("\n");
if (n < 0) break;
s = (n <= 3) ? 3 : n;
for (i=0; i < s; ++i){ // rows
for (j=0; j < s-i; ++j) // white spaces
printf(" ");
for (int k=0; k < i; ++k) // *.
printf("*.");
printf("*\n"); // always, unique or last *
}
for (i=0; i < s-1 ; ++i)
printf(" ");
printf("***\n\n");
}
return 0;
}
I wrote this code, but I have one error that I can't fix, the problem is that the code works well but actually what my program prints to the number n: 3 should be to n: 2 you can see it in the picture. when 5 is actually 4 etc... When I fix the loops I move the look of the lines and then it's no longer a diamond.
#include <stdio.h>
int main() {
int i, j, n;
printf("n: ");
scanf("%d", &n);
for (j = 1; j < n; j++) {
printf(" ");
}
printf("+");
printf(" \n");
for (i = 2; i < n; i++) {
for (j = 1; j <= n - 1; j++) {
if ((i + j) == (n + 1))
printf("/");
else
printf(" ");
}
for (j = 1; j < n; j++) {
if (i == j)
printf("\\");
else
printf(" ");
}
printf("\n");
}
for (i = 2; i < n; i++) {
for (j = 1; j < n; j++) {
if (i == j)
printf("\\");
else
printf(" ");
}
for (j = 1; j <= n - 1; j++) {
if ((i + j) == (n + 1))
printf("/");
else
printf(" ");
}
printf("\n");
}
for (j = 1; j < n; j++) {
printf(" ");
}
printf("+");
return 0;
}
A quick and dirty fix is adding 1 to n after the scanf("%d", &n);
Here is a modified version taking advantage of the printf width feature:
#include <stdio.h>
int main() {
int i, n;
printf("n: ");
if (scanf("%d", &n) != 1)
return 1;
printf("%*s\n", n, "+");
for (i = 1; i < n; i++) {
printf("%*s%*s\n", n - i, "/", i * 2, "\\");
}
for (i = 1; i < n; i++) {
printf("%*s%*s\n", i, "\\", (n - i) * 2, "/");
}
printf("%*s\n", n, "+");
return 0;
}
It seems that just adding 1 to n would solve your problem.
I'm trying to decrements the space loop value by 2 at the row 6 position. After that, as it be like decremented by 1 at the 7th and 8th row so on.
I'm trying to print a pyramid structure with numbers.
#include <stdio.h>
main() {
int i, j, n, s, p, m;
scanf("%d", &n);
int num = 1;
for (i = 1; i <= n; i++) {
printf("row:%d ", i);
s = 0;
for (s = i; s <= n; s++) {
if (i == (n / 2) + 1 && j == (n / 2) + 1) {
s = s + 1;
printf("%d", s);
}
// if ((i == (n / 2) + 1)) {
// s = s + 2;
//// printf("in:%d ", s);
// }
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%d", num);
num = num + 1;
}
for (p = 1; p < i; p++) {
num = num - 1;
printf("%d", num);
}
printf("\n");
}
}
What type of pyramid you are printing?
If you want to decrease counter by 2 you can use following code:
for(int i=10; i>0 ; i-=2)
this will decrease counter by 2 in each loop.
The 2nd iteration of the for loop always skips the 1st iteration. In debugging mode, it starts with i=1, but as soon as it computes the if clause, i=2 and for the rest it goes nice. It's almost as if there's n hidden continue.
Original Code:
int n, k, i, j = 0;
int *key;
int x = 0;
int y = 0;
scanf ("%d%d", &n, &k);
key = malloc (n * sizeof (int));
for (i = 0; i < n; i++) {
scanf("%d", &key[i]);
}
for (i = 1; i <= n; i++) {
if (key[j % n] == i) {
x++;
} else {
y++;
j++;
}
}
The code this way works perfectly though:
while (1) {
for (i = 1; i <= n; i++) {
while (1) {
if (x == k) {
printf("%d",y);
return 0;
} else if (!(key[j % n] - i)) {
x++;
break;
} else {
y++;
j++;
}
}
}
}