Home > other >  Transfer the working code from the Main to the method for TowerBuilder in C#
Transfer the working code from the Main to the method for TowerBuilder in C#

Time:10-22

I have following code, which according to the input parameter nFloors (number of floors) when run in Main() creates a tree of *. For example:

int nFloors=6;
int blank=nFloors*2-1;
for(int j=1;j<nFloors*2;j =2)
     Console.WriteLine(((new string(' ', (blank-j)/2)) (new string('*', j))) (new string(' ', (blank-j)/2)));

Output:

     *     
    ***    
   *****   
  *******  
 ********* 
***********

Now I want to transfer that code in a separate method:

public static string[] TowerBuilder(int nFloors)
  {
    return new string[0];
  }

This method should return an array instead of printing it to Console. I tried this but that does not produce wanted output:

public static string[] TowerBuilder(int nFloors)
{
     int blank=nFloors*2-1;
     string [] array=new string[nFloors] ; //declare an array to be returned with
                                           //nFloors elements inside
     
     int j;
     for(j=0;j<nFloors;j =2) //array should be looped from 0, but if in that way
                             // we get even j values (0,2,4..) and j needs to be odd
                             // (1,3,5..)
     array[j]=(((new string(' ', (blank-j)/2)) (new string('*', j))) (new string(' ', (blank-j)/2)));
        
     return array;   

      }

The issue here is how to loop an array getting the odd values of j (odd values correspond to number of *) while correctly accessing array elements (from first to last element i.e. j<nFloors). I believe it is a trivial thing but I can't figure it out. Tried researching similar issues but with no results. Any suggestions?

CodePudding user response:

To avoid complicated indexing issues, I'd just add to a list instead:

public static string[] TowerBuilder(int nFloors)
{
     int blank = nFloors*2-1;
     var list= new List<string>();
     
     for(int j = 1; j < nFloors*2; j  = 2)
     {
         list.Add(((new string(' ', (blank-j)/2)) (new string('*', j))) (new string(' ', (blank-j)/2)));
     }
        
     return list.ToArray();   
}

If you insist on using an array, then you basically need a function that maps the interval [0..nFloors] => Odd Numbers.

For example : f(x) = 2*x 1 which gives you

0 => 1
1 => 3
2 => 5
...

Unrelated: I think the last (new string(' ', (blank-j)/2)) may be not neccessary? It's just trailing blanks ...

CodePudding user response:

(Good answer supplied w using list, here is an option if using array is required)

You should use different variables to keep track of the array index v/s the number of stars and blanks. This should work:

static string[] TowerBuilder(int nFloors)
{
     int blank=nFloors*2-1;
     string [] array=new string[nFloors] ; 
     for(int j=0;j<nFloors;j =1){ 
        int stars = (j*2) 1;
        array[j]=(((new string(' ', (blank-stars)/2)) (new string('*', stars))) (new string(' ', (blank-stars)/2)));
     }
     return array;   
}

CodePudding user response:

With meaning-full variables names, some coding convention (no type prefixed variables name) and good use of nice C# feature the code may looks like a readable thing:

private static IEnumerable<string> BuildTower(int floorCount)
{
    var towerWidth = floorCount * 2 - 1;
    for (var floor = 0; floor < floorCount; floor  )
    {
        var starCount = floor * 2   1;
        var blankCount = (towerWidth - starCount) / 2;

        var stars = new string('*', starCount);
        var blanks = new string(' ', blankCount);

        yield return blanks   stars   blanks;
    }
}
  •  Tags:  
  • c#
  • Related