3

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.

7
  • should the result be the square n x n of "+" , where n is entered number? there is no need for recursion in this case Commented Jan 16, 2012 at 8:06
  • The problem seems simple. I would suggest using a debugger and step into the code and understand what the program is doing. This would help you understand how the program execute. As for pointers to solve this bug, && means 'AND' - logical operator in C and isn't the same as in linux shell and printf returns the number of characters printed. Commented Jan 16, 2012 at 8:08
  • My prof only said to do it on recursions any nothing more. Commented Jan 16, 2012 at 8:11
  • 1
    You should ask yourself what && is doing, and what the return value of printf is..do you really need it? IMO it just complicates everything.. Commented Jan 16, 2012 at 8:13
  • I really don't know why I put && because I tend to experiment. Commented Jan 16, 2012 at 8:16

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Just a question about the first one. on the else, do you really have to use the "return".
you really want everything on a plate, my mistake for entering rough code.
No no no. I don't want to be spoon fed, Just give me clues.
Alright, Say If I would have given the number of pluses. Int main() { int numPlus; printf("Give the number"); scanf("%d",&numPlus); function printpluses("and so on") } How can I do that?
How it goes, call printpluses (1), ok is x==0? no then let's go to else, we print an x and we call printpluses(0). Ok, is x==0? YES then just return to the previous call and sequentially exit the recursive functions. It goes like that printpluses (3) + and then printpluses (2) + and then printpluses (1) 0 now exit recursion.
|
2

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();
}

4 Comments

I need a one with recursions.
Right... if you want to use recursion just call visual(number) using a loop and remove the first loop from visual function..
#include <stdio.h> #include <conio.h> int space(int space1) { if(space1 == 0) { printf("\n"); 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); for(i=0;i<number;i++)visual(number); getch(); }
He only said recursions only. No loops or anything else, just recursions.
2
#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();
}

Comments

0

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();
}

6 Comments

This is close but, you didnt use the int space.
He was very strict on the "only recursions rule".
I never knew that I was allowed to do " visual(number,number); "
of course not, and now you will accept this answer. Juan get a programming book and pay attention during the lectures!
Yes sir! I don't follow my professor code, because I like to do it my onw way.
|
0
#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();
}

1 Comment

Good luck buddy you need a lot of studying! Every loop can be expressed as a recursive function, that is your excercise!
0
#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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.