6 star (David Star / david Shield) in asteriks and in string letters - c#

I would like to print a Star of David shape in a console application:
*
* *
* * * * *
* * * *
* * * * *
* *
*
I would like to support two modes:
Given an integer N, draw the star using N asterisks.
Given a string S, draw the star using the characters in the string.
Here's what I currently have:
static void Triangle(int nBase)
{
int i, j, move;
for (i = 1; i <= nBase; i++)
{
move = 0;
do
{
Console.Write(" ");
move++;
} while (move <= nBase * 3 - i + 1);
for (j = 1; j <= i; j++)
{
Console.Write("* ");
}
Console.WriteLine();
}
}
static void TriangleInverted(double nBase)
{
int i, j, move;
for (i = (int)nBase; i >= 0; i--)
{
move = 0;
do
{
Console.Write(" ");
move++;
} while (move <= nBase * 3 - i + 1);
for (j = 1; j <= i; j++)
{
Console.Write("* ");
}
Console.WriteLine();
}
}
As you can see I can draw a triangle and an inverted triangle (as per mode 1) but I don't know how to combine them into a star of david (which is basically having them overlapped properly).
I am also unsure as to how to implement mode (2).
Just to clarify my questions: Given a number of asteriks I would like to know what is the height and the base size of the each of the 2 opposite triangles. And in addition, where to start printing the opposite triangle? and how many asteriks should be in each printed line?

One option is to create a 2-dimensional array of where you'd like the stars to be positioned, then after they the array has been filled then iterate over it and write it to the screen. For example,
var points = new bool[10,10];
for (int x=0; x<10; x++)
for (int y=0; y<10; y++)
{
if (x == y) points[x,y] = true;
}
for (int y=0; y<10; y++)
{
for (int x=0; x<10; x++)
{
if (points[x,y]) Console.Write("*");
else Console.Write(" ");
}
Console.WriteLine();
}
Another option is to use SetCursorPosition to position the cursor where you'd like the * to be written.

I know this post is old but for coming developers.
The code maybe not beautiful but it's working.
`
public static void Main(string[] args)
{
int size=20;
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
if((i+j>=size/2-1 && i<=size/2-1 && j-i<=size/2-1) ||
(i==size/5 && j<size-1)||
(i-j<=size/5 && j<size/2 && i>size/5)||
(i+j<=size+((size-10)/5) && j>=size/2 && i>size/5))
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine("");
}
}
`
Change the size as you want (preference to even numbers).

Take a look on this to understand the basic form for your first question:http://jsfiddle.net/steinair/d6pe977v/
<html>
Width of David star: <input type=text size=2 id=starwidth onchange="check_and_draw();" value="5" />
<center>
<div id=star></div>
</center>
<script>
function check_and_draw(){
w=document.getElementById("starwidth").value;
drawstar(w);
}
function writetxt(t){
document.getElementById("star").innerHTML+=t;
}
function drawstar(w){
document.getElementById("star").innerHTML="<br>Star of David with width of "+w+" letters:<hr>";
h=Math.ceil(w/3);//since every phase is a third of the triangle
for (y=1;y<=h;y++){ //The first third of the upper triangle
for (x=1;x<=y;x++){
writetxt("A ");
}
writetxt("<br>");
}
for (y=0;y<h;y++){ //The base of the lower triangle and 2nd third of the upper triangle
for (x=(w-y);x>0;x--){
writetxt("B ");
}
writetxt("<br>");
}
//if width is not even = odd then draw another line in the middle:
if ( w % 2 ) {
for (x=(w-y);x>0;x--){
writetxt("C ");
}
writetxt("<br>");
}
for (y=h;y>0;y--){ //The base of the upper triangle and 2nd third of the lower triangle
for (x=(w-y)+1;x>0;x--){
writetxt("D ");
}
writetxt("<br>");
}
for (y=h;y>0;y--){ //The last third of the lower triangle
for (x=y;x>0;x--){
writetxt("E ");
}
writetxt("<br>");
}
}
</script>
</html>
And this answers your 2nd question: http://jsfiddle.net/steinair/qo575L9d/
<html>
String:
<input type=text size=30 id=starstring onchange="check_and_draw();" value="" />
<center>
<div id=star></div>
</center>
<script>
function check_and_draw() {
starstring = document.getElementById("starstring").value;
w = starstring.length;
drawstar(w, starstring);
}
function writetxt(t) {
document.getElementById("star").innerHTML += t;
}
function drawstar(w, starstring) {
var n = 0;
var spacer = " ";
document.getElementById("star").innerHTML = "<br>Star of David from string '" + starstring + "' (" + w + " letters):<hr>";
h = Math.ceil(w / 3); //since every phase is a third of the triangle
for (y = 1; y <= h; y++) { //The first third of the upper triangle
n = 0;
for (x = 1; x <= y; x++) {
charN = starstring.charAt(n);
n++;
writetxt(charN + spacer);
}
writetxt("<br>");
}
for (y = 0; y < h; y++) { //The base of the lower triangle and 2nd third of the upper triangle
n = 0;
for (x = (w - y); x > 0; x--) {
charN = starstring.charAt(n);
n++;
writetxt(charN + spacer);
}
writetxt("<br>");
}
//if width is not even = odd then draw another line in the middle:
if (w % 2) {
n = 0;
for (x = (w - y); x > 0; x--) {
charN = starstring.charAt(n);
n++;
writetxt(charN + spacer);
}
writetxt("<br>");
}
for (y = h; y > 0; y--) { //The base of the upper triangle and 2nd third of the lower triangle
n = 0;
for (x = (w - y) + 1; x > 0; x--) {
charN = starstring.charAt(n);
n++;
writetxt(charN + spacer);
}
writetxt("<br>");
}
for (y = h; y > 0; y--) { //The last third of the lower triangle
n = 0;
for (x = y; x > 0; x--) {
charN = starstring.charAt(n);
n++;
writetxt(charN + spacer);
}
writetxt("<br>");
}
}
</script>
</html>

Related

Magic Square Code Single Even number in C#

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();
}

Creating an isosceles triangle in C3

The code below only makes a right angle triangle, how would I make it into an isosceles triangle?
int height = 4;
string star = "";
for (int i = 0; int i < height; i++)
{
star += "*";
Console.WriteLine(star);
}
Console.ReadLine();
This only displays a right angle triangle. What I attempted to make was a pyramid.
Here you have a cleaner code:
int numberoflayer = 4;
int empty;
int number;
for (int i = 1; i <= numberoflayer; i++)
{
for (empty = 1; empty <= (numberoflayer - i); empty++)
Console.Write(" ");
for (number = 1; number <= i; number++)
Console.Write('*');
for (number = (i - 1); number >= 1; number--)
Console.Write('*');
Console.WriteLine();
}
This draws your christmas tree:
int height = 4;
for (int i = 0; i < height; i++)
{
int countSpaces = (int)Math.Ceiling((height * 2 / 2d) - i);
int countStars = 1 + (i * 2);
string line = new string(' ', countSpaces) + new string('*', countStars);
Console.WriteLine(line);
}
Dirty code but there you go
int height = 4;
string empty = " ";
String star = "";
for(int i = 0; i<height; i++)
{
star += " *";
empty = empty.Length > 0 ? empty.Remove(0,1) : " ";
Console.WriteLine(empty + star);
}
Console.ReadLine();

How to put char ('X') to int array?

Hi I have 2D array type of int where I have numbers, for example from 1 to 100.
It's a game - something like Tic-Tac-Toe - but I use bigger array. So I need to put a character 'X' or 'O' from user to this array. But problem is I can't (don't know) how to put these characters in that int array. I wan to use only the console.
I tried make the array type of char but then I can't fill the array with numbers.
I know how to do it if user would have want put some numbers but then It doesn't look good...
I would be happy for any advice how to do it.
public void Napln () { //filling the array
int poc = 1;
for (int i = 0; i < pole.GetLength(1); i++)
{
Console.Write(" ");
for (int j = 0; j < pole.GetLength(0); j++)
{
if (poc < 10)
Console.Write(" " + (pole[j, i] = poc++) + " | ");
else if ( poc < 100 )
Console.Write( (pole[j,i] = poc ++) + " | ");
else
Console.Write((pole[j, i] = poc++) + " | ");
}
Console.WriteLine();
for ( int v = 0; v < roz1; v ++ )
Console.Write("_____|");
Console.WriteLine();
}
Console.WriteLine();
public void Pozice (int vyber) //find the user choice
{
for ( int i = 0; i < pole.GetLength(1); i ++ )
{
for ( int j = 0; j < pole.GetLength(0); j ++ )
{
if (pole[i, j] == vyber)
{
pole[i, j] = 'X';
hraci.Vypis();
}
}
}
}
public void Vypis() //print the same with change of user choice
{
for ( int i = 0; i < pole.GetLength(1); i ++ )
{
Console.Write(" ");
for ( int j = 0; j < pole.GetLength(0); j ++ )
{
if (pole[j,i] < 10)
Console.Write(" " + pole[j, i] + " | ");
else if (pole[j,i] < 100)
Console.Write(pole[j, i] + " | ");
else
Console.Write(pole[j, i] + " | ");
}
Console.WriteLine();
for (int v = 0; v < roz1; v++)
Console.Write("_____|");
Console.WriteLine();
}
}
I am the new one in C# especially the OOP. So if you have any more advice I would be happy.
Just taking your question as it is, I can imagine two ways I'd do it without getting too fancy.
The first one would be to use two arrays. One for holding the numbers (and int array), one for holding the player input (a char array holding "x" and "o"). This could look like this:
public class Program
{
public static void Main()
{
int width = 10;
int height = 10;
char[,] playerBoard = new char[width, height];
int[,] numberedBoard = new int[width, height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
// Fill the numbered board with 1 to 100
numberedBoard[x, y] = x * width + y + 1;
// And the player board with emptyness
playerBoard[x, y] = ' ';
}
}
System.Console.WriteLine("Number at x = 3 / y = 5: " + numberedBoard[3, 5]);
System.Console.WriteLine("Current owner of x = 3 / y = 5: \"" + playerBoard[3, 5] + "\"");
// Let's change the owner of x = 3 and y = 5
playerBoard[3, 5] = 'X';
System.Console.WriteLine("New owner of x = 3 / y = 5: \"" + playerBoard[3, 5] + "\"");
}
}
A second solution could be to create an object to fit your needs, and have an array of this one. The benefit is that you only have one array, and each cell holds all the information relevant to this cell. Consider the following:
using System;
public class Program
{
struct BoardEntry {
public int number;
public char owner;
}
public static void Main()
{
int width = 10;
int height = 10;
BoardEntry[,] board = new BoardEntry[width, height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
// For each "cell" of the board, we create a new instance of
// BoardEntry, holding the number for this cell and a possible ownage.
board[x, y] = new BoardEntry() {
number = x * width + y + 1,
owner = ' '
};
}
}
// You can access each of those instances with `board[x,y].number` and `board[x,y].owner`
System.Console.WriteLine("Number at x = 3 / y = 5: " + board[3, 5].number);
System.Console.WriteLine("Current owner of x = 3 / y = 5: \"" + board[3, 5].owner + "\"");
// Let's change the owner of x = 3 and y = 5
board[3, 5].owner = 'X';
System.Console.WriteLine("New owner at x = 3 / y = 5: \"" + board[3, 5].owner + "\"");
}
}
It seems like you are new to programming, so the first solution might be easier to understand and use right now, but I suggest you read at some point a little bit into what structs and classes are, because they let you do some really powerful things, like in this case bundle the relevant information that belongs together.

Rotating matrices

Objectives
Imagine that, we have matrix like
a11 a12 a13
a21 a22 a23
a31 a32 a33
What I want to do is, from textbox value rotate this matrix so that, for example if I write 2 and press rotate, program must keep both diagonal values of matrix (in this case a11, a22, a33, a13, a31) and rotate 2 times clockwise other values. So result must be like
a11 a32 a13
a23 a22 a21
a31 a12 a33
It must work for all N x N size matrices, and as you see every 4 rotation takes matrix into default state.
What I've done
So idea is like that, I have 2 forms. First takes size of matrix (1 value, for example if it's 5, it generates 5x5 matrix). When I press OK it generates second forms textbox matrix like that
Form 1 code
private void button1_Click(object sender, EventArgs e)
{
int matrixSize;
matrixSize = int.Parse(textBox1.Text);
Form2 form2 = new Form2(matrixSize);
form2.Width = matrixSize * 50 + 100;
form2.Height = matrixSize *60 + 200;
form2.Show();
//this.Hide();
}
Form 2 code generates textbox matrix from given value and puts random values into this fields
public Form2(int matrSize)
{
int counter = 0;
InitializeComponent();
TextBox[] MatrixNodes = new TextBox[matrSize*matrSize];
Random r = new Random();
for (int i = 1; i <= matrSize; i++)
{
for (int j = 1; j <= matrSize; j++)
{
var tb = new TextBox();
int num = r.Next(1, 1000);
MatrixNodes[counter] = tb;
tb.Name = string.Format("Node_{0}{1}", i, j);
Debug.Write(string.Format("Node_{0}{1}", i, j));
tb.Text = num.ToString();
tb.Location = new Point(j * 50, i * 50);
tb.Width = 30;
tb.Visible = true;
this.splitContainer1.Panel2.Controls.Add(tb);
counter++;
}
}
}
Form 2 has 1 textbox for controlling rotation (others are generated on the fly, programmatically). What I want to do is, when I enter rotation count and press Enter on this textbox, I want to rotate textbox matrix as I explained above. Can't figure out how to do it.
Copy both diagonals to separate arrays, then rotate your matrix and replace diagonals. Below code shows each step:
class Program
{
static void Main(string[] args)
{
int matrixSize = 3;
string[,] matrix = new string[matrixSize,matrixSize];
//create square matrix
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
matrix[x, y] = "a" + (x + 1).ToString() + (y + 1).ToString();
}
}
Console.WriteLine(Environment.NewLine + "Base square matrix");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(matrix[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//copy diagonals
string[] leftDiagonal = new string[matrixSize];
string[] rightDiagonal = new string[matrixSize];
for (int x = 0; x < matrixSize; x++)
{
leftDiagonal[x] = matrix[x, x];
rightDiagonal[x] = matrix[matrixSize - 1 - x, x];
}
Console.WriteLine(Environment.NewLine + "Diagonals");
for (int x = 0; x < matrixSize; ++x)
{
Console.Write(leftDiagonal[x] + " " + rightDiagonal[x] + Environment.NewLine);
}
Console.ReadKey();
//rotate matrix
string[,] rotatedMatrix = new string[matrixSize, matrixSize];
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
rotatedMatrix[x, y] = matrix[matrixSize - y - 1, x];
}
}
Console.WriteLine(Environment.NewLine + "Rotated");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrix[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//rotate matrix again
string[,] rotatedMatrixAgain = new string[matrixSize, matrixSize];
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
rotatedMatrixAgain[x, y] = rotatedMatrix[matrixSize - y - 1, x];
}
}
Console.WriteLine(Environment.NewLine + "Rotated again");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrixAgain[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//replace diagonals
for (int x = 0; x < matrixSize; x++)
{
rotatedMatrixAgain[x, x] = leftDiagonal[x];
rotatedMatrixAgain[matrixSize - 1 - x, x] = rightDiagonal[x];
}
Console.WriteLine(Environment.NewLine + "Completed" + Environment.NewLine);
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrixAgain[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
}
}
I don't know C#, so I can only give a suggestion in pseudocode:
Input: An N by N matrix in
Output: The input matrix rotated as described in the OP out
for i = 1 to N
for j = 1 to N
if N - j != i and i != j // Do not change values on either diagonal
out[j][N-i] = in[i][j]
else
out[i][j] = in[i][j]
Disclaimer: This algorithm is untested. I suggest you use a debugger to check that it works as you want.
This seems like quite an unorthodox UI presentation, but you're not too far off in terms of being able to achieve your functionality. Instead of a linear array, a rectangular array will make your job much easier. The actual rotation could be implemented with a for loop repeating a single rotation (which would be implemented as in the case 1 code), but I've decided to combine them into the four possible cases. This actually allows you to enter a negative number for number of rotations. Which reminds me, you really should do more error checking. At least protect against int.Parse throwing an exception both places it's used (with a try catch block or by switching to int.TryParse) and make sure it returns a meaningful number (greater than 0, possibly set a reasonable maximum other than int.MaxValue) for matrixSize in button1_Click.
namespace RotatingMatrices
{
public class Form2 : Form
{
// note these class fields
private TextBox[,] matrixNodes;
private int matrixSize;
public Form2(int matrSize)
{
InitializeComponent();
// note these inits
matrixSize = matrSize;
matrixNodes = new TextBox[matrixSize, matrixSize];
Random r = new Random();
// note the new loop limits
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
var tb = new TextBox();
int num = r.Next(1, 1000);
// note the change in indexing
matrixNodes[i,j] = tb;
tb.Name = string.Format("Node_{0}_{1}", i, j);
Debug.Write(string.Format("Node_{0}_{1}", i, j));
tb.Text = num.ToString();
tb.Location = new Point(j * 50, i * 50);
tb.Width = 30;
tb.Visible = true;
this.splitContainer1.Panel2.Controls.Add(tb);
}
}
}
private void buttonRotate_Click(object sender, EventArgs e)
{
string[,] matrix = new string[matrixSize, matrixSize];
int rotations = (4 + int.Parse(textBoxRotations.Text)) % 4; // note the addition of and mod by 4
switch(rotations)
{
case 1: // rotate clockwise
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[j, matrixSize - i - 1] = matrixNodes[i, j].Text;
}
}
break;
case 2: // rotate 180 degrees
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[i, j] = matrixNodes[matrixSize - i - 1, matrixSize - j - 1].Text;
}
}
break;
case 3: // rotate counter-clockwise
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[i, j] = matrixNodes[j, matrixSize - i - 1].Text;
}
}
break;
default: // do nothing
return;
}
// restore diagonals
for(int i = 0; i < matrixSize; i++)
{
matrix[i, i] = matrixNodes[i, i].Text;
matrix[i, matrixSize - i - 1] = matrixNodes[i, matrixSize - i - 1].Text;
}
// write strings back to text boxes
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrixNodes[i, j].Text = matrix[i, j];
}
}
}
}
}
I decided to tackle the issue using a listView instead of a text box, which makes the logic easier for me. Using this method, I was able to think of the matrix as successive boxes. I start on the outside and move in toward the middle, changing the size of my box each time.
Also, rather than using two forms, I use one. At the top I have a textbox where the user enters the size they want the array to be, and a button labeled "Fill" (button2). And at the bottom I have a textbox where the user enters the degree of rotation. When they click "Rotate," it kicks off a process of adding values to linked lists, combining and shifting the list, and then writing back out to the matrix. I'm sure I made it more convoluted than it has to be, but it was a great learning exercise.
After looking over jerry's code above, I think I'm going to look into rectangular arrays. :)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Recycle
{
public partial class Form1 : Form
{
public int size;
public LinkedList<string> topRight = new LinkedList<string>();
public LinkedList<string> bottomLeft = new LinkedList<string>();
public LinkedList<string> myMatrix = new LinkedList<string>();
public LinkedList<string> shiftMatrix = new LinkedList<string>();
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
listView1.Clear();
size = int.Parse(textBox2.Text);
int c = 0;
int q = 0;
int w = 0;
string[] content = new string[size];
Random rnd = new Random();
for (c = 0; c < size; c++)
{
listView1.Columns.Add("", 25);
}
for (q = 0; q < size; q++)
{
for (w = 0; w < size; w++)
{
content[w] = rnd.Next(9,100).ToString();
}
ListViewItem lvi = new ListViewItem(content);
listView1.Items.Add(lvi);
}
}
public bool iseven(int size)
{
if (size % 2 == 0)
{
return true;
}
else
{
return false;
}
}
public void button1_Click(object sender, EventArgs e)
{
if (listView1.Items.Count < 3)
{
MessageBox.Show("Matrix cannot be rotated.");
return;
}
bool even = false;
int shift = int.Parse(textBox1.Text); //amount to shift by
int box = listView1.Items.Count - 1; //size of box
int half = Convert.ToInt32(listView1.Items.Count / 2);
int corner = 0; //inside corner of box
if (shift > listView1.Items.Count)
{
shift = shift % ((listView1.Items.Count - 2) * 4);
}
do
{
eachPass(shift, box, corner);
++corner;
--box;
} while (box >= half + 1);
}
public void eachPass(int shift, int box, int corner)
{
int x;
int i;
//Read each non-diagonal value into one of two lists
for (x = corner + 1; x < box; x++)
{
topRight.AddLast(listView1.Items[corner].SubItems[x].Text);
}
x = box;
for (i = corner + 1; i < box; i++)
{
topRight.AddLast(listView1.Items[i].SubItems[x].Text);
}
for (x = box - 1; x > corner; x--)
{
bottomLeft.AddLast(listView1.Items[box].SubItems[x].Text);
}
x = corner;
for (i = box - 1; i > corner; i--)
{
bottomLeft.AddLast(listView1.Items[i].SubItems[x].Text);
}
string myTest = "";
//join the two lists, so they can be shifted
foreach (string tR in topRight)
{
myMatrix.AddLast(tR);
}
foreach (string bL in bottomLeft)
{
myMatrix.AddLast(bL);
}
int sh;
//shift the list using another list
for (sh = shift; sh < myMatrix.Count; sh++)
{
shiftMatrix.AddLast(myMatrix.ElementAt(sh));
}
for (sh = 0; sh < shift; sh++)
{
shiftMatrix.AddLast(myMatrix.ElementAt(sh));
}
//we need the sizes of the current lists
int trCnt = topRight.Count;
int blCnt = bottomLeft.Count;
//clear them for reuse
topRight.Clear();
bottomLeft.Clear();
int s;
//put the shifted values back
for (s = 0; s < trCnt; s++)
{
topRight.AddLast(shiftMatrix.ElementAt(s));
}
for (s = blCnt; s < shiftMatrix.Count; s++)
{
bottomLeft.AddLast(shiftMatrix.ElementAt(s));
}
int tRn = 0;
int bLn = 0;
//write each non-diagonal value from one of two lists
for (x = corner + 1; x < box; x++)
{
listView1.Items[corner].SubItems[x].Text = topRight.ElementAt(tRn);
++tRn;
}
x = box;
for (i = corner + 1; i < box; i++)
{
listView1.Items[i].SubItems[x].Text = topRight.ElementAt(tRn);
++tRn;
}
for (x = box - 1; x > corner; x--)
{
listView1.Items[box].SubItems[x].Text = bottomLeft.ElementAt(bLn);
++bLn;
}
x = corner;
for (i = box - 1; i > corner; i--)
{
listView1.Items[i].SubItems[x].Text = bottomLeft.ElementAt(bLn);
++bLn;
}
myMatrix.Clear();
shiftMatrix.Clear();
topRight.Clear();
bottomLeft.Clear();
}
}
}

Writing stars with specific shape

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

Categories