Printing a christmas tree using stars and dots in C - c

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;
}

Related

How to successfully take a step back and preserve the shape? C programming - loops

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.

How to print diagonal star pattern in C

I'm confused to print this type of pattern given below:
* *
* *
*
* *
* *
I've tried this:
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of base\n>>> ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j + n; j++)
{
if (// ??? )
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
but I don't know what is the condition of if statement
Help Me to solve this please
The pattern consists of exactly n * 2 - 1 rows and columns. So you can run an outer loop to iterate through rows with structure for(i=1; i<= count; i++); where count = n * 2 - 1. Each row contains exactly n * 2 - 1 columns. So, run inner loop as for(j=1; j<=count; j++). And inside this loop we need stars to be printed when row and column number both are equal (i.e. print star whenever if(i == j)) and if(j == count - i + 1). For example:
#include <stdio.h>
int main() {
int i, j, n;
int count;
printf("Enter n: ");
scanf("%d", &n);
count = n * 2 - 1;
for (i = 1; i <= count; i++) {
for (j = 1; j <= count; j++) {
if (j == i || (j == count - i + 1)) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
To not change any code and answer your question "what goes in the if"
n - 1 - i == j || j == i
this does
This will work fine !! :)
#include<iostream>
using namespace std;
int main()
{
int n,m=0;
int m2=0;
cin>>n;
int f=-1,l=n;
for(int i=0;i<n;i++)
{
f=f+1;
l=l-1;
for(int j=0;j<n;j++)
{
if(j==f || j==l) cout<<"*";
else cout<<" ";
}
cout<<endl;
}
}

How to print diamond shape pattern in C?

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;
}

doesn't count extra numbers [magic square]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N_MAX 10
#define N_MIN 2
#define TEMPSIZE 1024
float RowSum(float **matrix, int sizearray,int row) {
float sum;
for (int j=0 ; j <= row; j++) {
sum = 0;
for (int i=0 ; i < sizearray; i++) {
sum = sum + matrix[j][i];
}
}
return sum;
}
float ColSum(float **matrix, int sizearray, int col) {
float sum;
for (int j = 0; j <= col; j++) {
sum = 0;
for (int i = 0; i < sizearray; i++) {
sum = sum + matrix[i][j];
}
}
return sum;
}
int RepNum(int arraysize, float **matrix) {
int i, j, counter = 0;
int temparray[N_MAX*N_MAX];
for (i = 0; i < N_MAX*N_MAX; i++) {
temparray[i] = 0;
}
for (i = 0; i < arraysize; i++) {
for (j = 0; j < arraysize; j++) {
temparray[(int)matrix[i][j]]++;
}
}
for (i = 1; i < arraysize*arraysize; i++) {
if (temparray[i] > 1)
counter++;
}
return counter;
}
void PrintArray(float **matrix, int arraysize) {
for (int i = 0; i<arraysize; i++)
{
for (int j = 0; j<arraysize; j++)
printf("%3d ", (int)matrix[i][j]);
printf("\n");
}
}
int CheckInt(float **matrix, int arraysize) {
int counter = 0;
for (int i = 0; i < arraysize; i++) {
for (int j = 0; j < arraysize; j++) {
if (((int)matrix[i][j]) != matrix[i][j])
counter++;
}
}
return counter;
}
void main() {
printf("Hello! this program will help you to find out if you have a magic square!\nPlease enter your matrix order and following it the numbers you'd like to check: \n");
float **matrix;
char input[TEMPSIZE];
int sizear = 0;
float correctsum = 0.0;
int counter = 0, row, column;
fgets(input, TEMPSIZE, stdin);
char* str = strstr(input, " ");
if (str == 0)
return;
str[0] = 0;
str++;
sizear = atof(input);
if (sizear > N_MAX || sizear < N_MIN)
{
printf("ERROR: The matrix size cannot be %d size. \n", sizear);
exit(0);
}
matrix = (float**)calloc(1, sizear * sizeof(float*));
for (column = 0; column < sizear; column++)
{
matrix[column] = (float*)calloc(1, sizear * sizeof(float));
}
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
char* temp = strstr(str, " ");
if (temp == 0) /*gets the last number*/
{
++counter;
matrix[row][column] = atof(str);
goto end;
}
if (atof(temp) <= sizear*sizear && atof(temp) > 0) { /*puts all numbers in matrix*/
temp[0] = 0;
matrix[row][column] = atof(str);
str = temp + 1;
++counter;
}
else {
printf("you cannot enter the number %.3f \n", atof(temp));
exit(0);
}
}
}
end:
if (counter > sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter < sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter == sizear*sizear) {
correctsum = (float)((sizear*(sizear*sizear + 1)) / 2);
float row = RowSum(matrix, sizear, 0);
float coul = ColSum(matrix, sizear, 0);
if (row == coul && row== correctsum && coul==correctsum && RepNum(sizear, matrix) ==0 && CheckInt(matrix,sizear)==0) {
printf("It's a magic matrix!\n");
PrintArray(matrix, sizear);
}
else {
printf("Not a magic square:\n");
if (row != coul && row != correctsum && coul != correctsum) {
printf("* Coloums and rows sum do not match.\n");
}
if (RepNum(sizear, matrix) != 0) {
printf("* There are repeating numbers.\n");
}
if (CheckInt(matrix, sizear) != 0) {
printf("* One of the numbers or more you've entered isn't integer.\n");
}
}
}
for (column = 0; column < sizear; column++)
{
free(matrix[column]);
}
free(matrix);
}
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
The exercise asked to check if the input is a magic square (sum rows=columns=diagonals), making sure there are no duplicates, only int numbers and that I don't enter more or less than sizearrayXsizearray numbers. The input from the user should look like 3 1 2 3 4 5 6 7 8 9 where the first number (here it is 3) is the array size and the rest are the numbers that would be checked as a magic square.
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
Sure, it's simple: the counter is incremented in the loops
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
…
}
}
- the inner statements are run through (at most) sizear × sizear times, so the counter, starting from 0, cannot reach a higher value.

Wrong output when printing a tent shape with stars

I'm trying to print out a hollow, open tent shape using asterisk stars "*". The code uses two for loops, the first for the rows, and the other for the columns.
following is my code:
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
} else {
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf(" ");
}
if(j == n) {
printf("*");
for(j = 1; j <= n; j++) {
printf(" ");
}
}
}
}
}
int main()
{
printTent(4);
}
Output obtained:
* * * *
Desired output:
*
* *
* *
* *
I don't think you will need that
if (n == 1) {
printf("*");
}
We can take care of that in what you've written in the else part.
For n=4, the number of spaces to be printed at the start of each line is 3, 2, 1 & 0.
You seem to be trying to accomplish that with your first inner loop. But
for(j = 0; j < n; j++) {
printf(" ");
}
will always print n spaces. We need to reduce the number of spaces printed by 1 on each iteration of the outer loop.
Coming to your second loop,
for(j = 1; j <= n; j++) {
printf(" ");
}
This has a similar problem only difference being the incrementation of the number of spaces printed.
Try something like this
void printTentNMMod(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
for(j = i; j < n; j++) {
printf(" ");
}
printf("*");
if(i!=0)
{
for(j=0; j<2*(i-1)+1; ++j)
{
printf(" ");
}
printf("*");
}
printf("\n");
}
}
Also, you could shorten this to
void printTent(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
printf("%*c", n-i, '*');
if(i!=0)
{
printf("%*c", 2*i, '*');
}
printf("\n");
}
}
The * in %*c will set the number of places occupied by the character printed by the %c.
I've finished it and I have written annotation.
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
}
else {
for (i = 0; i < n; i++) {
for (j = 0; j < n -i; j++) {// you should use n-i instead of n because the number of spaces is decreasing
printf(" ");
}
if (j == n-i) { //
printf("*");
for (j = 1; j <= i * 2 - 1; j++)//this loop outputs spaces between two "*"
{
printf(" ");
}
if (i != 0)//the first line only needs one "*"
printf("*");
printf("\n"); //Line breaks
}
}
}
}
Another way.
#include <stdio.h>
int main() {
int i, j;
int height = 5;
for(i = height; i > 0; i--) {
for(j = 1; j < height * 2; j++) {
if(j == i || j == height * 2 - i)
printf("*");
else
printf(" ");
}
puts("");
}
return 0;
}
Output
*
* *
* *
* *
* *

Resources