Inverted triangle homework question

In summary, the conversation discusses the creation of a recursive function that prints an upside-down triangle using a given character and number of rows. The function is implemented in C, with a main function that uses scanf to obtain user input. The code initially had a problem with scanf for a character, but it was resolved by adding a space after the %c in the scanf statement.
  • #1
FOIWATER
Gold Member
434
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
  • #2
Actually solved it already sry...

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

1. What is an inverted triangle homework question?

An inverted triangle homework question is a type of question that starts with the conclusion or answer, and then requires the student to work backwards to find the supporting evidence or steps to reach that conclusion.

2. Why are inverted triangle homework questions used?

Inverted triangle homework questions are used to encourage critical thinking and problem-solving skills. By starting with the conclusion, students are required to think about the steps and evidence needed to support it, rather than simply regurgitating memorized information.

3. How do I approach an inverted triangle homework question?

One approach to solving an inverted triangle homework question is to break down the conclusion into smaller parts, and then work backwards to find the evidence or steps needed for each part. Another approach is to start with the given information and use deductive reasoning to reach the conclusion.

4. Can inverted triangle homework questions be used in all subjects?

Yes, inverted triangle homework questions can be used in all subjects. They are particularly useful in subjects that require critical thinking, such as math, science, and philosophy.

5. How can inverted triangle homework questions benefit students?

Inverted triangle homework questions can benefit students by improving their critical thinking skills, encouraging deeper understanding of the subject matter, and promoting independent problem-solving abilities. They also mimic real-life situations where one must work backwards from a desired outcome to determine the necessary steps to achieve it.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
880
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
668
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
925
  • Engineering and Comp Sci Homework Help
Replies
3
Views
753
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top