Inverted triangle homework question

  • Thread starter Thread starter FOIWATER
  • Start date Start date
  • Tags Tags
    Homework Triangle
Click For Summary
SUMMARY

The discussion centers on a homework problem requiring the implementation of a recursive function in C to print an upside-down triangle using a specified character. The user encountered issues with the scanf function when trying to read a character input. The solution involved adding a space before the %c format specifier in the scanf function call, resolving the input issue effectively.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with recursive function implementation
  • Knowledge of the scanf function for input handling
  • Basic concepts of loops and conditional statements in C
NEXT STEPS
  • Research C programming best practices for input handling with scanf
  • Explore recursive function design patterns in C
  • Learn about character manipulation and formatting in C
  • Study debugging techniques for common input/output issues in C programs
USEFUL FOR

C programming students, software developers learning recursion, and anyone troubleshooting input issues in C applications.

FOIWATER
Gold Member
Messages
434
Reaction score
12

Homework Statement


Write a recursive function void recurTriangle ( int n, char ch ) which prints out an upside-down triangle. The parameter ch is the character to be used for drawing the triangle, and n is the number of characters on the first row. For example, if n is 7 and ch is ’+’, then the output of the function should be:

+++++++
++++++
+++++
++++
+++
++
+

NOTE: I didn't actually use n or ch but it's the same idea


Homework Equations





The Attempt at a Solution


#include <stdio.h>
void triangle(int,char);
char b;
int main()
{
int a;
printf("how many rows in the triangle?\n");
scanf("%d",&a);
printf("what symbol?\n");
scanf("%c",&b);
triangle(a,b);
}
void triangle(int x,char y)
{
int c,d;
for (c=1;c<=x;c++)
{
for (d=x;d>=c;d--)
{
printf("%c",y);
}
printf("\n");
}
}

PROBLEM: I am having issues with scanf for a character. If I make the triangle function only have one input and remove the user's ability to edit the character and just input my own and not use scanf, it works fine. It seems to be somehow messing it up

any help appreciated.
 
Physics news on Phys.org
Actually solved it already sry...

the problem was scanf(" %c" <--- needs a space there.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
5K