Please help me out!. Answer should be in C!. no C++ or any other language!. Than
ID: 3544132 • Letter: P
Question
Please help me out!. Answer should be in C!. no C++ or any other language!.
Thanks
/*
* prints the given character the given number of times
* on the screen.
* Parameter: length - the number of characters to print
* Parameter: theChar - the characters to print
*/
void printChars (int length, char theChar )
{
int countChars = 0;
while ( countChars < nChars)
{
printf( "%c" , theChar );
countChars++;
}
}
Add a function called printTri to your program that takes the size of a triangle ( positive int ) as its only parameter and that prints a triangle of the specified size on the screen using alternating rows of ' o ' and ' x ' characters as shown below. You'll need to modify main( ) function so that you can test your printTri function. Your function must work for any reasonable size!. Your printTri function should make appropriate use of the printChars function!.
eg:
size 4
printTri (4 )
o
xx
ooo
xxxx
Explanation / Answer
#include<stdio.h>
/*
* prints the given character the given number of times
* on the screen.
* Parameter: length - the number of characters to print
* Parameter: theChar - the characters to print
*/
void printChars (int length, char theChar )
{
int countChars = 0;
while ( countChars < length)
{
printf( "%c" , theChar );
countChars++;
}
}
void printTri(int n)
{
int i;
for(i=0; i<n; i++)
{
printChars(i+1,(i%2==0?'o':'x'));
printf(" ");
} //end for
}
int main()
{
int k;
printf("Enter size of Triangle :");
scanf("%d",&k);
printf(" ");
printTri(k);
return 0;
}