So the user has a choice of 3 possible outputs:
- Draw a triangle
- Draw a rectangle
- Draw a house
I can draw all three but the output is not quiet right.
As you see in the code it draws a triangle but I need it to move more to the right.
if (keuze == 1)
{
int n = 4;
int i, j, k = 0;
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
{
Console.Write(" ");
}
while (k != (2 * i - 1))
{
if (k == 0 || k == 2 * i - 2)
Console.Write("*");
else
Console.Write(" ");
k++;
;
}
k = 0;
Console.WriteLine();
}
for (i = 0; i < 2 * n - 1; i++)
{
Console.Write("*");
}
Console.WriteLine();
}
Try This:
if (true)
{
int n = 4;
int i, j, k = 0;
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
{
Console.Write(" ");
}
while (k != (2 * i - 1))
{
if (k == 0) Console.Write(" "); //Added
if (k == 0 || k == 2 * i - 2)
Console.Write("*");
else
Console.Write(" ");
k++;
}
k = 0;
Console.WriteLine();
}
Console.Write(" "); //Added
for (i = 0; i < 2 * n - 1; i++)
{
Console.Write("*");
}
Console.WriteLine();
}
Related
i´d like to build a pascal´s triangle as a square matrix in c# like this.
1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
But the following code didn´t perform, could you please help me?
Console.Write("Size of Matrix: ");
int size = Convert.ToInt32(Console.ReadLine());
int[,] pascal = new int[size, size];
for (int i = 0; i < pascal.GetLength(0);i++)
{
for (int j = 0; j < pascal.GetLength(1); j++)
{
if (j > i )
{
pascal[i, j] = 0;
}
if (i == j || j == 0)
{
pascal[i, j] = 1;
}
if (i !=j)
{
pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
}
Console.Write($"{pascal[i,j ],5 }");
}
Console.WriteLine();
}
thx
You are forgot else if and rewrite data if (i != j)
Console.Write("Size of Matrix: ");
int size = Convert.ToInt32(Console.ReadLine());
int[,] pascal = new int[size, size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == j || j == 0)
{
pascal[i, j] = 1;
}
else if (j > i)
{
pascal[i, j] = 0;
}
else if (i != j)
{
pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
}
Console.Write($"{pascal[i, j],5 }");
}
Console.WriteLine();
}
Your problem is your ifs, need to be else if otherwise you will be trying to reference negative indexes in the array with pascal[i - 1, j - 1] etc.
for (int i = 0; i < pascal.GetLength(0); i++)
{
for (int j = 0; j < pascal.GetLength(1); j++)
{
if (j > i)
pascal[i, j] = 0;
else if (i == j || j == 0)
pascal[i, j] = 1;
else if (i != j)
pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
Console.Write($"{pascal[i, j],5}");
}
Console.WriteLine();
}
Another way you could achieve this is with good old fashioned math
for (var i = 0; i < rows; i++)
for (var j = 0; j <= i; j++)
if (j != 0 && i != 0)
pascal[i, j] = val = val * (i - j + 1) / j;
else
pascal[i, j] = 1;
To Output
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < rows; j++)
Console.Write($"{pascal[i, j]} ");
Console.WriteLine();
}
A new array always contain zeros. You could seed the 1's outside the loops, then only loop through the rest.
I would also separate the construction of the array, from the printing method. If you only wanted to print it, you'd only need one array.
int[,] pascal = new int[size, size];
pascal[0, 0] = 1;
for (int i = 1; i < size; i++)
{
pascal[i, 0] = pascal[i, i] = 1;
for (int j = 1; j <= i - 1; j++)
pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
}
To help to grasp this dynamic programming concept the following is my recursive solution.
for(var i = 0; i < 5; ++i)
{
for(var j = 0; j < i + 1; ++j)
{
Console.Write($"{p(i, j)} ");
}
Console.WriteLine();
}
static int p(int i, int j)
=> (j == 0 || i == j)
? 1
: p(i - 1, j) + p(i - 1, j - 1);
I've got this problem as an assignement at the University. The problem is how to make numbers printed like this, where digits are lined with each other, automatically like seen on the picture below.
That's my code right now.
public class Tabliczka
{
private int n;
public Tabliczka(int n)
{
this.n = n;
}
public void wyswietl()
{
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
if (i * j <= 9) Console.Write(" {0}", i * j);
if (i * j > 9 && i * j <= 99) Console.Write(" {0}", i * j);
if (i * j > 99) Console.Write(" {0}", i * j);
}
Console.Write("\n");
}
}
}
It's not a good solution because it'll format it correctly only to 999. Thanks in advance for any tips :).
We just need to properly format the numbers:
public void wyswietl()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
Console.Write(string.Format("{0, 10}", i * j));
}
Console.Write(Environment.NewLine);
}
}
I want to print something like this
54321
5432
543
54
5
4
3
2
1
int n = 5;
Console.WriteLine();
for (int i = n; i >= 0; i--)
{
for (int j = 1; j <= i; j++)
Console.Write(i.ToString());
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
I would solve this with while-loops
int i = 5, input = 0;
while (i > 0)
{
input *= 10;
input += i--;
}
while (input > 10)
{
Console.WriteLine(input);
input /= 10;
}
while (input > 0)
{
Console.WriteLine(input--);
}
https://dotnetfiddle.net/1L6j6A
can you help me to create a logic for magic square metric. In given example, I have created a code for generate Magic Square for odd numbers like 3x3, 5x5, 7x7 metric and double even numbers like 4×4 , 8×8 but unable to found a proper solution for create single even value magic square metric like 6x6, 10x10 etc.
In current implementation anyone can enter a number (n) in input and it will create a nxn magic square metric. But not working fine with single even numbers
class Program
{
public static void Main(string [] args )
{
Console.WriteLine("Please enter a number:");
int n1 = int.Parse(Console.ReadLine());
// int[,] matrix = new int[n1, n1];
if (n1 <= 0)
{
Negativ();
}
else if (n1 == 2)
{
Zwei();
}
else if ((n1 != 2) && !(n1 < 0) && (n1 % 2 != 0))
{
Odd (n1 );
}
else if ((n1 != 2) && !(n1 < 0) && ((n1 - 2) % 4 == 0))
{//singl Even
SingleEven(n1);
}
else if ((n1 != 2) && !(n1 < 0) && (n1 % 4 == 0))
{
DoubleEven (n1);
}
}
private static void Negativ(){
Console.WriteLine("Sorry, the number must be positive and greater than 3 ");
Console.ReadLine();
}
public static void Zwei(){
Console.WriteLine("Sorry,there is no magic square of 2x2 and the number must be and greater than 3 ");
Console.ReadLine();
}
public static void Odd ( int n)// odd method
{
int[,] magicSquareOdd = new int[n, n];
int i;
int j;
// Initialize position for 1
i = n / 2;
j = n - 1;
// One by one put all values in magic square
for (int num = 1; num <= n * n; )
{
if (i == -1 && j == n) //3rd condition
{
j = n - 2;
i = 0;
}
else
{
//1st condition helper if next number
// goes to out of square's right side
if (j == n)
j = 0;
//1st condition helper if next number is
// goes to out of square's upper side
if (i < 0)
i = n - 1;
}
//2nd condition
if (magicSquareOdd[i, j] != 0)
{
j -= 2;
i++;
continue;
}
else
{
//set number
magicSquareOdd[i, j] = num++;
//1st condition
j++; i--;
}
}
// print magic square
Console.WriteLine("The Magic Square for " + n + " is : ");
Console.ReadLine();
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
Console.Write(" " + magicSquareOdd[i, j] + " ");
Console.WriteLine();
Console.ReadLine();
}
Console.WriteLine(" The sum of each row or column is : " + n * (n * n + 1) / 2 + "");
Console.ReadLine();
}
public static void SingleEven(int n )
{
// int n = magic .Length ;
int[,] magicSquareSingleEven = new int[n, n];
int halfN = n / 2;
int k = (n - 2) / 4;
int temp;
int[] swapcol = new int[n];
int index = 0;
int[,] minimagic = new int[halfN, halfN];
*Odd(minimagic) ;* // here is the problem
for (int i = 0; i < halfN; i++)
for (int j = 0; j < halfN; j++)
{
magicSquareSingleEven[i, j] = minimagic[i, j];
magicSquareSingleEven[i+ halfN , j+halfN ] = minimagic[i, j]+ halfN *halfN ;
magicSquareSingleEven[i, j + halfN] = minimagic[i, j] +2* halfN * halfN;
magicSquareSingleEven[i + halfN, j] = minimagic[i, j] +3* halfN * halfN;
}
for (int i =1; i< k ;i ++)
swapcol [index ++]=i ;
for (int i = n-k+2; i <= n ; i++)
swapcol[index++] = i;
for (int i =1; i<=halfN ;i ++)
for (int j = 1; j<= index ; j ++)
{
temp = magicSquareSingleEven[i - 1, swapcol[j - 1] - 1];
magicSquareSingleEven[i-1,swapcol[j-1]-1]=magicSquareSingleEven[i +halfN-1,swapcol[j-1]-1];
magicSquareSingleEven[i+halfN-1,swapcol[j-1]-1]=temp;
}
//swaping noses
temp=magicSquareSingleEven[k,0];
magicSquareSingleEven[k,0]=magicSquareSingleEven[k+halfN,0];
magicSquareSingleEven[k+halfN,0]=temp;
temp=magicSquareSingleEven[k+halfN,k];
magicSquareSingleEven[k+halfN,k]=magicSquareSingleEven[k,k];
magicSquareSingleEven[k,k]=temp;}
//end of swaping
// print magic square
Console.WriteLine("The Magic Square for " + n + " is : ");
Console.ReadLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write(" " + magicSquareSingleEven[i, j] + " ");
Console.WriteLine();
Console.ReadLine();
}
Console.WriteLine(" The sum of each row or column is : " + n * (n * n + 1) / 2 + "");
Console.ReadLine();
}
i want to write a shape with " * " and " | " the shape is below.
The program must take height and width from user.Width is column number without ' | '.I tried to write but confused.My code sometimes works great and sometimes being stupid.For example when i enter height : 13, width : 4 it writes one more,if witdh is 1 it enters infinite loop.While trying to solve it became too conflicted.Must i fix it or rewrite ? Here is the code : height =10, width = 5
|*____|
|_*___|
|__*__|
|___*_|
|____*|
|___*_|
|__*__|
|_*___|
|*____|
|_*___|
private static void Function()
{
int height, width;
if (width == 2)
while (height > 0)
{
FirstPart(width, height);
height -= width;
}
else
while (height > 0)
{
if (height > 1)
{
FirstPart(width, height);
height -= width;
}
if (height > 0)
{
SecondPart(width, height);
height -= width - 2;
}
}
}
private static void FirstPart(int width,int height)
{
if(height > width)
for (int i = 0; i < width; i++)
{
for (int j = 0; j < width+2; j++)
{
if (j == 0 || j == width + 1)
Console.Write("|");
else
if (i + 1 == j)
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine();
}
else
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width + 2; j++)
{
if (j == 0 || j == width + 1)
Console.Write("|");
else
if (i + 1 == j)
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
private static void SecondPart(int width,int height)
{
if(height > width)
for (int i = 0; i < width-2; i++)
{
for (int j = 0; j < width+2; j++)
{
if (j == 0 || j == width + 1)
Console.Write("|");
else
if (i + j == width-1)
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine();
}
else
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width + 2; j++)
{
if (j == 0 || j == width + 1)
Console.Write("|");
else
if (i + j == width - 1)
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
private static void WriteStars(int width, int height)
{
int j = 0;
for (int i = 0; i < height; i++)
{
Console.Write("|");
for (int f = 0; f < width; f++)
{
if (f == Math.Abs(j))
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
j++;
if (Math.Abs(j) == width - 1)
{
j *= -1;
}
Console.WriteLine("|");
}
}
Probably going to get downvoted for giving you a complete answer, but maybe it'll show you one correct approach and you can learn something from it...
I see a
while (Height > 0)
so your infinite loop is coming from Height never getting less or equal to 0.
It's better to rewrite. When you do, decouple the code into several functions so that one function draws a single line, and another one calls the former to draw all the lines.
void WriteStars(int Width,int Height)
{
int _sp=1; //Star Pos
bool _left = false;
for(int i =0;i<Height;i++)
{
Console.Write("|");
int j;
for(j=1;j<Width-1;j++)
{
if(j==_sp)
{
Console.Write("*");
if(_left)
{
_sp--;
}
else
{
_sp++;
}
j++;
break;
}
else
{
Console.Write("_");
}
}
for(;j<Width-1;j++)
{
Console.Write("_");
}
Console.WriteLine("|");
if(_sp==0)
{
_left = false;
}
else if(_sp==Width)
{
_left = true;
}
}
}
Try if it works, wrote it right here.
even shorter:
static void Variante_2(int height, int width)
{
byte[][] arr = new byte[height][];
int pos = 0;
int mov = 1;
for (int line = 0; line < height; line++)
{
arr[line] = new byte[width];
for (int col = 0; col < width; col++) { arr[line][col] = 45; }
arr[line][pos] = 42;
pos += mov;
if (pos == 0 || pos == (width - 1)) { mov *= -1; }
Console.WriteLine("|" + ASCIIEncoding.ASCII.GetString(arr[line]) + "|");
}
string temp = Console.ReadLine();
}
and it is possible to do it with less code:
static void Variante_3(int height, int width)
{
int pos = 1;
int mov = 1;
for (int line = 0; line < height; line++)
{
Console.WriteLine("|" + "*".PadLeft(pos, '_') + "|".PadLeft(width - pos, '_'));
pos += mov;
if (pos == 1 || pos == (width - 1)) { mov *= -1; }
}
string temp = Console.ReadLine();
}
Sorry to all not doing others homework, but I couldn´t sleep without showing this g