Re: [Help Needed] Converting C++ program into Mathematica Function
- To: mathgroup at smc.vnet.net
- Subject: [mg71189] Re: [Help Needed] Converting C++ program into Mathematica Function
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Fri, 10 Nov 2006 06:37:45 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <eiursa$gj2$1@smc.vnet.net>
a1b2c3d4 wrote: > i've tried for quite some time but am still stuck at it. > > the C++ program is as follows: > > #include<stdio.h> > main() > { > int Current_line, Num_Of_Star, Num_Space; > int N, Num_Star_Current_line,m; > printf("\n How many Lines:"); > scanf("%d",&N); > for(Current_line = 1; Current_line <= N; Current_line=Current_line+1) > { > > Num_Space = N -Current_line; > for(m =1; m <= Num_Space;m = m+1) > { > printf(" "); > } > > Num_Star_Current_line = Current_line * 2 -1; > for(Num_Of_Star =1; Num_Of_Star <= Num_Star_Current_line; Num_Of_Star= Num_Of_Star + 1) > { > printf("*"); > } > > printf("\n"); > } > } > > > The Mathematica function i've tried to write is as follows: > > printstars := Block[{n, i, space1, star1}, > n = Input["Enter the number of lines:"]; > For[i = 1, i ¡<= n, i++, > (space1 = n - i; star1 = i 2 - 1); > Do[Print[" "], {space1}]; > Do[Print["*"], {star1}]; > > ]] > > However, Mathematica gives output for each * on a new line. I've tried to use the function "StringJoin" but still can't seem to make it work. > > Anyone has suggestions? Your help will be greatly appreciated. > The following expression should do what you are looking for: In[1]:= printstars := Block[{n, i, space1, star1, line}, n = Input["Enter the number of lines: "]; For[i = 1, i <= n, i++, space1 = n - i; star1 = 2*i - 1; line = StringJoin[Table[" ", {space1}]] <> StringJoin[Table["*", {star1}]]; Print[line]; ] ] (We answered 10 to the question "How many lines?") In[2]:= printstars * *** ***** ******* ********* *********** ************* *************** ***************** ******************* Regards, Jean-Marc