473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

using recursion

Write a program that will print a figure, like the following, recursively. Prompt the user to enter n, the number of asterisks in the first (and last) row. In the example, n = 4.
****
***
**
*
**
***
****

------------------------------------
THIS IS WHAT i HAVE SO FAR:

Expand|Select|Wrap|Line Numbers
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include <math.h>
  4.  
  5. void print(int n);
  6.  
  7. int main(void)
  8. {
  9.  
  10.     int n;
  11.  
  12.     printf("Enter n with the keyboard>> ");
  13.     scanf("%d",&n);
  14.     //n = n-1;
  15.  
  16.     print(n);
  17.     printf("\n");
  18.     printf("*\n");//print one star
  19.  
  20.  
  21.     int i;
  22.  
  23.     for (i = 0; i <n; i++ )
  24.     {
  25.       printf("*");
  26.     }
  27.  
  28.  
  29. }
  30. void print(int n)
  31. {
  32.     int i = 0;
  33.     if (n ==1)
  34.     {
  35.         printf("\n*\n");
  36.     }
  37.     else {
  38.         for (i = 0; i <n; i++ )
  39.         {
  40.             printf("*");
  41.         }
  42.        printf("\n");
  43.  
  44.     }
  45.  
  46.     n = n-1;
  47.     //
  48.    // print(n);
  49.  
  50.     for (i = 0; i < n; i++ )
  51.         {
  52.             printf("*");
  53.         }
  54.         //n = n-1;
  55.  
  56.     //printf(n); <-----------------------HAVE NO IDEA TO DO RECURSION HERE!!!
  57.  
any help will be appreciated!!!
Nov 11 '13 #1
1 1124
Banfa
9,065 Expert Mod 8TB
The general pattern for a recursive function is

Expand|Select|Wrap|Line Numbers
  1. void recursiveFunction(<parameters>) // Note the reutrn value is not necessarily void
  2.                                      // But it makes sense in this case
  3. {
  4.   if (EndCondition Is True)
  5.   {
  6.     return;
  7.   }
  8.  
  9.   // Do some stuff here
  10.  
  11.   recursiveFunction(<Altered Parameters for next recursion>);
  12. }
  13.  
Also note that because of the pattern you are outputting knowing the line number in the pattern is not enough to know what to output, you will need to know the total number of lines or the number of asterisks on the first and last row (which are basically the same thing) so your recursive function will need at least 2 parameters, one of which wont change.

You could implement this with 2 recursive functions, 1 to recurs through the lines and another which it calls to print each individual line.
Nov 12 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: pembed2003 | last post by:
Hi all, As an exercise, I am trying to come up with a function to count the length of a char* string using recursion. I came up with this: int count_length(char* s){ if(s == 0) return 0;...
5
Rooro
by: Rooro | last post by:
For the List class, add a Boolean-valued function that determines whether the data items in the linked list are arranged in ascending order. How can i do this using recursion
3
by: greek | last post by:
Hi! I hav to generate fibonaaci series using recursion: 0,1,1,2,3,5,8,18,21... whr fibonacci(0)=0 fibonacci(1)=1 fibonacci(n)=fibonacci(n-1)+fibonacci(n-2) ive witten the code but having 2...
13
by: Raman | last post by:
Hi All, Could any one tell me How to concatenate two strings using recursion. And also how to trim a string using recursion.(in C, offcourse) Regards, Raman Chalotra
3
by: greek | last post by:
the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
6
by: Suyash Upadhyay | last post by:
Hi All, As a beginner in Computer Programming, I decided to create Linked List using recursion, but I am not getting right answer. I think it is fundamentally correct, but I am stuck. ...
2
by: guneet bhatia | last post by:
please help with fibonacci series using recursion..
2
by: wagn31 | last post by:
new to python and I have this assingment: I need to write a recursive function that takes paramaters (aString,nTimes) and it needs to print the aString parameter the number of times specified by...
6
by: Aniruddha123 | last post by:
this is the question:: Problem 2: Find the Numbers, (K Narayan Kumar, CMI) This is a rather simple problem to describe. You will be given three numbers S, P and k. Your task is to find if there are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.