Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:50:12+00:00 2026-05-20T09:50:12+00:00

Source: #include <stdio.h> void main() { int i, j, tree_Size, spaces, total_Spaces, x; char

  • 0

Source:

#include <stdio.h>

void main()

{

int i, j, tree_Size, spaces, total_Spaces, x;
char tree_Characters;

printf("Enter characters to use --> ");
 scanf("%c", &tree_Characters);
printf("Enter size of tree --> ");
 scanf("%d", &tree_Size);

   //For handling top part of tree
   for (i = 0; i <= tree_Size; i++) {
      printf("\n");
      total_Spaces = tree_Size - i;

        //Determine spaces before each number for pyramid
      for (spaces = 1; spaces <= total_Spaces; spaces++) 
         printf(" ");

      //Make first line always one number
      for (j = -1; j < i; j++)
         if (j <= -1)
            printf("%c", tree_Characters);
         else if (j > -1)
            printf("%c%c", tree_Characters, tree_Characters);
   } 

   //For handling stem and base of tree
   for (i = 0; i <= tree_Size; i++) {

      //if handling stem
      if (i < tree_Size) {
         printf("\n");  
         for (spaces = 1; spaces <= tree_Size; spaces++)
            printf(" ");
         printf("%c", tree_Characters);
      }

      //else if handling base

      else if (i == tree_Size) {
         printf("\n");

         for (x = 0; x <= tree_Size * 2; x++) {
            printf("%c", tree_Characters);
         }
      }
   }

printf("\n");

}

Here is the output it produces so far
Here is a thread I made on reddit for better understanding (comments)

The goal of the assignment is for it to look like this:

*** Print a Tree ***

Enter characters to use ---   *|=+
Enter size of tree      ---   4

    *
   ***
  *****
 *******
*********
    |
    |
    |
    |
====+====

What I don’t understand is how to process characters. The teacher was mentioning putting characters into some buffer and then clearing it etc, I have no idea. Can anyone help? I have to use scanf, can’t convert anything to strings or use anything advanced.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:50:13+00:00Added an answer on May 20, 2026 at 9:50 am

    You need to make tree_Characters a array (char tree_Characters[5] for a five element array, which will hold your four tree chars and a nul terminator), and change the first scanf.

    scanf("%4s%*[^\n]",tree_Characters);
    //      vv\_____/  ^-- array will decay to char*
    //      ||   \-- discard the rest of the line
    //      ||         ("*" indicates discard, "[^\n]" matches everything but '\n')
    //      |\-- string (non-whitespace characters only)
    //      \-- at most four characters scanned
    

    See http://linux.die.net/man/3/scanf for more information.

    Edit: You will of course have to change the rest of the program to select the appropriate characters for each tree element. (See @eznme’s answer.)

    Edit: No-array Version:

    char tree_leaf,tree_stem,tree_ground,tree_root;
    scanf("%c%c%c%c%*[^\n]",&tree_leaf,&tree_stem,&tree_ground,&tree_root);
    

    This will react badly to too-short input lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The way you would normally include a script is with source eg: main.sh: #!/bin/bash
I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include
I need to include a copyright statement at the top of every Python source
I am going to paste here my code and errors: #include stdio.h #include winsock2.h
I have included <zlib.h> in my iphone application and the source code I was
Source RegexOptions.IgnoreCase is more expensive than I would have thought (eg, should be barely
Source: <TD> <A HREF=/home><IMG SRC=/images/home.gif></A> <IMG SRC=/images/spacer.gif> <A HREF=/search><IMG SRC=/images/search.gif></A> <IMG SRC=/images/spacer.gif> <A HREF=/help><IMG
My source code needs to support both .NET version 1.1 and 2.0 ... how
Can source code examples be kept in a SQL database while retaining all formatting
Open source implementation will be preferred.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.