Reputation: 57
My professor asked me to do this:
Input Number: 5
+++++
+++++
+++++
+++++
+++++
I've been trying so hard to get thi; I kept on ending up with a " + " with a huge blank and " + ".
Can you please help me fix this code in C?
#include <stdio.h>
#include <conio.h>
int space(int space1)
{
if(space1 == 0)
{
return 1;
}
else
{
return printf("\n") && space(space1 - 1);
}
}
int visual(int plus)
{
if (plus == 0)
{
return 1;
}
else
{
return printf("+") && visual (plus - 1) && space(plus - 1);
}
}
int main()
{
int number;
printf("Please give the number\n");
scanf("%d",&number);
visual(number);
getch();
}
A new edit; frustrating for me. It gives me 5 rows of + and a big space.
Upvotes: 3
Views: 2196
Reputation: 549
#include <stdio.h>
#include <conio.h>
int number;
int visual(int plus)
{
int i;
if (plus == 0)
{
return 1;
}
else
{
for(i=1;i<=number;i++)
printf("+");
printf("\n");
visual (plus - 1);
}
}
int main()
{
printf("Please give the number\n");
scanf("%d",&number);
visual(number);
getch();
}
This will help.
Upvotes: 0
Reputation: 1911
Complete Solution Updated without loop :::
#include <stdio.h>
#include <conio.h>
int space(int space1)
{
if(space1 == 0)
{
return 1;
}
else
{
return printf("+") && space(space1 - 1);
}
}
int visual(int plus,int add)
{
if (plus == 0)
{
return 1;
}
else
{
space(add);
printf("\n");
visual (plus - 1,add);
}
}
int main()
{
int number,i;
printf("Please give the number\n");
scanf("%d",&number);
visual(number,number);
getch();
}
Upvotes: 0
Reputation: 1963
Inside the recursion function in the if (plus == 0) section you should print the \n character and then use a loop to call visual (number) for number times.
If you really want to do it only with recursions, you also need another recursive function. One recursion function will call the other, one will print "+++++\n" and the other will call this function for x times to produce x number of lines.
function printpluses (int x){
if (x==0){
printf ("\n");
return;
}
else {
printf ("+");
printpluses (x-1);
}
}
and the other function would be
function printline (int x, int no_of_pluses){
if (x==0){
printf ("\n");
return;
}
else{
printpluses(no_of_pluses);
printline (x-1, no_of_pluses);
}
}
you can make no_of_pluses global so yo don't pass it along in every call.
Upvotes: 3
Reputation: 111
#include <stdio.h>
#include <conio.h>
int number;
int visual(int plus)
{
if(plus % number == 0 && plus!=number*number)
printf("\n");
if (plus == 0)
{
return 1;
}
else
{
printf("+");
visual (plus - 1);
}
}
int main()
{
printf("Please give the number\n");
scanf("%d",&number);
visual(number*number);
getch();
}
Upvotes: 2
Reputation: 111
#include <stdio.h>
#include <conio.h>
int number;
int visual(int plus)
{
int i;
if (plus == 0)
{
return 1;
}
else
{
for(i=1;i<=number;i++)
printf("+");
printf("\n");
visual (plus - 1);
}
}
int main()
{
printf("Please give the number\n");
scanf("%d",&number);
visual(number);
getch();
}
Upvotes: 0
Reputation: 1911
check this for the solution::
#include <stdio.h>
#include <conio.h>
int visual(int plus)
{
for(i=0;i<plus;i++)
{
for(j=0;j<plus;j++)
{
printf("+");
}
printf("\n");
}
int main()
{
int number;
printf("Please give the number\n");
scanf("%d",&number);
visual(number);
getch();
}
Upvotes: 2