Click here to Skip to main content
16,004,882 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C++
#include<iostream>
using namespace std;
void line(int,int,int);
int main()
{int i;
do
{cout<<"How many rows? ";
cin>>i;
if(i<0)
    cout<<"must be nonnegative\n";
}while(i<0);
line(i,1,i);
system("pause");
return 0;
}
void line(int m,int n,int p)
{int i;
if(m>p)
     return;
for(i=0;i<m;i++)
    cout<<"*";
if(m>0)
    cout<<endl;
if(m==0||n==0)
   line(m+1,0,p);
else
    line(m-1,n,p);
}

---------------------------------

#include<iostream>
using namespace std;
void line2(int,int,int);
int main()
{int i;
do
{cout<<"How many rows? ";
cin>>i;
if(i<0)
    cout<<"must be nonnegative\n";
}while(i<0);
line2(1,1,i);
system("pause");
return 0;
}

void line2(int m,int n,int p)
{int i;
if(m<n)
     return;
for(i=0;i<m;i++)
    cout<<"*";
if(m>0)
    cout<<endl;
if(m==p||n==0)
   line2(m-1,0,p);
else
    line2(m+1,n,p);
}  


What I have tried:

I have no idea about the C++ code
Posted
Updated 5-Apr-21 20:28pm
v2
Comments
Rick York 5-Apr-21 21:22pm    
Sure, we'll help but YOU need to do the work. Helping does not mean doing your work for you.
Richard MacCutchan 6-Apr-21 3:46am    
the logic is perfectly simple to follow, so you just need to write the C code which incorporates the bits that print the asterisks.

The C equivalent of iostream is stdio.h. It looks like most of the work involves rewriting the lines that use cin and cout to use stdio.h. See here[^] and here[^].
 
Share this answer
 
Nope. This isn't a code conversion service.

To convert the code, you have to understand what the original code does in C++ and write equivalent C code to do the same thing.

Looking at the C++ code, as badly formatted as it is, that shouldn't be too hard.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900