Re: [Help Needed] Converting C++ program into Mathematica Function
- To: mathgroup at smc.vnet.net
- Subject: [mg71201] Re: [mg71156] [Help Needed] Converting C++ program into Mathematica Function
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Fri, 10 Nov 2006 06:38:03 -0500 (EST)
- References: <200611090839.DAA15731@smc.vnet.net>
On Nov 9, 2006, at 3:39 AM, 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. Print in Mathematica always adds a new line at the end. I would recommend building the complete string and then calling Print printstart:=Block[{n}, n=Input("Enter the number of lines:"]; Do[Print[StringJoin[Table[" ",{n-i}],Table["*",{i 2-1}]]],{i,n}]] Regards, Ssezi
- References:
- [Help Needed] Converting C++ program into Mathematica Function
- From: a1b2c3d4 <ms-z-@hotmail.com>
- [Help Needed] Converting C++ program into Mathematica Function