Advertisement
teleias

diamond (sample for cs 101)

Jun 17th, 2021
1,379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Rextester
  8. {
  9.     public class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.            
  14.             const int MAX = 8;
  15.             const bool CENTERED = true;
  16.            
  17.             for(int i = 0; i <= MAX; i++)
  18.             {
  19.                 string output = "";
  20.                 int sequence = sequencify(i, MAX);
  21.                 int result = resultify(sequence);
  22.                
  23.                
  24.                 for(int j = 0; j < result; j++)
  25.                 {
  26.                     output += "*";
  27.                 }
  28.                 if(CENTERED)
  29.                 {
  30.                     for(int j = sequence; j < MAX/2; j++)
  31.                     {
  32.                         output = " "+output;
  33.                     }
  34.                 }
  35.                 Console.WriteLine(output);
  36.             }
  37.            
  38.            
  39.             Console.WriteLine($"[index]\t-> [sequence]\t-> [1+sequence*2]");
  40.             for(int i = 0; i < 9; i++)
  41.             {
  42.                 Console.WriteLine($"{i}\t->\t {sequencify(i,MAX)}\t->\t {resultify(sequencify(i,MAX))}");
  43.             }
  44.            
  45.         }
  46.         public static int sequencify(int i, int period)
  47.         {
  48.             //0 1 2 3 4 5 6 7 0
  49.             //0 1 2 3 4 3 2 1 0
  50.             int half = period / 2;
  51.             return i <= half ? i : period - i;
  52.         }
  53.         public static int resultify(int i)
  54.         {
  55.             //0 1 2 3 4 3 2 1 0
  56.             //1 3 5 7 9 7 5 3 1
  57.             return 1 + i * 2;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement