MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: [Help Needed] Converting C++ program into Mathematica Function


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



  • Prev by Date: Re: Re: Efficiency of repeatedly appending to a List
  • Next by Date: Typo in DedekindEta function definition?
  • Previous by thread: [Help Needed] Converting C++ program into Mathematica Function
  • Next by thread: Re: [Help Needed] Converting C++ program into Mathematica Function