Wrong output when printing a tent shape with stars - c

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
*
* *
* *
* *
* *

Related

Printing a christmas tree using stars and dots in 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;
}

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

C programming two-hop neighbors

I am not sure how to get my two-hop neighbors correctly. It's almost correct but on my output, I don't want to include the same vertex. For my output now if vertex 0 is 0, it says "vertex 0: 0.....
I want to skip the vertex it is currently looking at.
Please help me, are my codes for two hop wrong?
this is my codes:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define M 20
#define N 20
int main()
{
int i, j, x, a, b;
int G[20][20] = { { 0 } };
/*creaate random adjaceney matrix*/
printf("==================================================\n");
printf("Welcome to my Graph Processing tool!\n\n");
srand(time(NULL));
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
if (i == j) {
G[i][j] = 0;
}
else {
G[i][j] = rand() % 2;
G[j][i] = G[i][j];
}
}
}
/*check whether the whole row equals to 0*/
for (j = 0; j < N; j++) {
if (G[j] == 0) {
x = rand() % 20 + 1;
G[x][j] = G[j][x] = 1;
}
/*print the matrix G*/
else
{
printf("The adjacency for graph G is\n");
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
printf("%d ", G[i][j]);
}
printf("\n");
}
}
}
/*all one-hop neighbors*/
printf("\nList of one-hop neighbors:");
for (i = 0; i < M; i++) {
printf("\nVertex %d: ", i);
for (j = 0; j < N; j++) {
if (G[i][j] == 1) {
printf("%d ", j);
}
}
}
printf("\n===================================\n\n");
/*two-hop neighbors*/
for (i = 0; i < M; i++) {
printf("\nVertex %d: ", i);
for (j = 0; j < N; j++) {
if (G[i][j] == 0) {
printf("%d ", j);
}
}
}
}
printf("\n============================================\n");
system("pause");
return 0;
}
This is my output:
One hop
Two hop
The answer provided by #Jake only works for node 0. For only looking at different nodes you need to do
for (j = 0; j < N; j++) {
if (i != j && G[i][j] == 0) {
printf("%d ", j);
}
}
Furthermore, you are assuming that all nodes without an edge are two-hop neighbors. This is not correct. One way to calculate the actual two-hop neighbors would be ((A + I)^2 > 0) - ((A + I) > 0), where I is the identity matrix.
Also, you can code this via a three-layer loop:
int node, neighbor, neighbor2;
for (node = 0; node < N; node++) {
printf("\nVertex %d: ", node);
for (neighbor = 0; neighbor < N; neighbor++) {
if (G[node][neighbor] == 1) {
for (neighbor2 = 0; neighbor2 < N; neighbor2++) {
if (node != neighbor2 && G[neighbor][neighbor2] == 1) {
printf("%d ", neighbor2);
}
}
}
}
}
Note that per definition M=N, so I've just used N. Also, this might print some 2-hop neighbors twice. You might want to do some filtering before printing.
Couple things to note here.
Be more descriptive with your variable naming, it would have made this a lot easier to read.
M-ROWS, N-COLS, G-Graph
When you loop through each row, you initialize j to 0. This includes the vertex that you are wanting to leave out.
for (j = 1; j < N; j++)

Creating just the border of a pyramid with asterisks in C

OK. So I'm just trying to do some practice with simple algorithms and asterisks in C. This is not a homework problem. In my book there is a project that asks you to create a program that makes "these shapes" out of asterisks. One of them is a pyramid. While I can find numerous examples of how to make pyramid on the web, I can't find one that helps me figure out the algorithm for just the border of one. And I have not seen one on here either.
Now here is the code I have already.
#include <stdio.h>
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 1; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
for( j = 1; j <= 2*i-1; j++)
{
printf("*");
}
printf("\n");
}
for ( i = 6; i < 10; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
for( j = 1; j <= i+1-3*h; j++)
{
printf("*");
}
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
When you run the program it should, since I've ran it numerous times with no errors, give you this shape:
....*
...***
..*****
.*******
*********
.*******
..*****
...***
....*
I keep thinking that the loop that has the asterisk would probably need more loops to determine the location of the spacing between the asterisks. But I can't really begin to think of how to do this. What I want to end up with is this:
....*
...*.*
..*...*
.*.....*
*.......*
.*.....*
..*...*
...*.*
....*
Any help with this would be greatly appreciated.
I am a beginner programmer. In my first University level programming class. We're only learning C in this class. We've learned up to Multidimensional Arrays, and started learning functions on Thursday.
Here is a hint. You want the "first and last *" - so look at the loop
for( j = 1; j <= 2*i-1; j++)
{
printf("*");
}
And change it to something like:
printf("*"); // first *
for( j = 2; j<= 2*i-2; j++) printf(" "); // spaces until...
printf("*"); // last *
See how you get on with that… this is not the only place where you need to make this change.
EDIT in response to the request by #KalaJ I provide complete code - once with the first and last star, and once without. You can pick which one you want to use by changing the condition in the #if 0 preprocessor directive. If you set it to #if 1 you get the original pattern.
#include <stdio.h>
#if 0
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 1; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
printf("*");
for( j = 2; j <= 2*i-2; j++)
{
printf(" ");
}
if(i>1) printf("*");
printf("\n");
}
for ( i = 6; i < 10; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
printf("*");
for( j = 1; j <= i-3*h-1; j++)
{
printf(" ");
}
if(i<9) printf("*");
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
#else
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 2; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
printf("*");
for( j = 2; j <= 2*i-2; j++)
{
printf(" ");
}
if(i>1) printf("*");
printf("\n");
}
for ( i = 6; i < 9; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
printf("*");
for( j = 1; j <= i-3*h-1; j++)
{
printf(" ");
}
if(i<9) printf("*");
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
#endif
This produces either
* *
* *
* *
* *
* *
* *
* *
or
*
* *
* *
* *
* *
* *
* *
* *
*
Define each edge as a vector
find max(Y)
iterate from max(Y) down
for each Y find the matching X then print dots or star if you intersect a vector until max(X) on that line

Resources