Nested For Loop To Print Below Mentioned Pattern - c#

I wanting to print
1
23
456
78910
in C# Console application, Can any one help in doing it for me
I am using this code which works fine for me but I want not to use if statement just want to show my result in nested loop
for (int i = 1; i <= 10; i++)
{
Console.Write(i.ToString());
if (i==1 || i == 3 || i == 6)
{
Console.WriteLine();
}
}

Try this This should print nummber in format as you mentioned
int len = 0;
int count = 1;
for (int i = 1; i < 11; i++)
{
count++;
if (count >= len)
{
len++;
count = 0;
Console.WriteLine("");
}
Console.Write(i);
}

int len = 1;
int count = 0;
for(int i = 0; i < 11 ; i++)
{
count++;
if(count > len)
{
len++;
count = 0;
Console.WriteLine("");
}
Console.Write(i);
}

Related

Cross Search generate char Matrix

I am trying to create a word search puzzle matrix, this is the code I have,
static void PlaceWords(List<string> words)
{
Random rn = new Random();
foreach (string p in words)
{
String s = p.Trim();
bool placed = false;
while (placed == false)
{
int nRow = rn.Next(0,10);
int nCol = rn.Next(0,10);
int nDirX = 0;
int nDirY = 0;
while (nDirX == 0 && nDirY == 0)
{
nDirX = rn.Next(3) - 1;
nDirY = rn.Next(3) - 1;
}
placed = PlaceWord(s.ToUpper(), nRow, nCol, nDirX, nDirY);
}
}
}
static bool PlaceWord(string s, int nRow, int nCol, int nDirX, int nDirY)
{
bool placed = false;
int LetterNb = s.Length;
int I = nRow;
int J = nCol;
if (MatriceIndice[nRow, nCol] == 0)
{
placed = true;
for (int i = 0; i < s.Length-1; i++)
{
I += nDirX;
J += nDirY;
if (I < 10 && I>0 && J < 10 && J>0)
{
if (MatriceIndice[I, J] == 0)
placed = placed && true;
else
placed = placed && false;
}
else
{
return false;
}
}
}
else
{
return false;
}
if(placed==true)
{
int placeI = nRow;
int placeJ = nCol;
for (int i = 0; i < s.Length - 1; i++)
{
placeI += nDirX;
placeJ += nDirY;
MatriceIndice[placeI,placeJ] = 1;
MatriceChars[placeJ, placeJ] = s[i];
}
}
return placed;
}
However it seems like it is an infinite loop. I am trying to add the code in a 1010 char matrix linked to a 1010 int matrix initially filled with 0 where I change the cases to 1 if the word is added to the matrix. How should I fix the code?
There are several errors. First,
MatriceChars[placeJ, placeJ] = s[i];
should be
MatriceChars[placeI, placeJ] = s[i];
Second,
for (int i = 0; i < s.Length - 1; i++)
(two occurrences) should be
for (int i = 0; i < s.Length; i++)
(You do want all the letters in the words, right?)
Third, when testing indices, you should use I >= 0, not I > 0, as the matrix indices start at 0.
However, the main logic of the code seems to work, but if you try to place too many words, you will indeed enter an infinite loop, since it just keeps trying and failing to place words that can never fit.

char 2D array compression

There is a .txt file what I have to read, compress and make an output txt for the compressed txt. Could anyone tell me what should I fix in my code?
My code:
namespace Tomorites
{
class Compression
{
public void Compress(char[,] source)
{
for (int i = 0; i < source.GetLength(0); i++)
{
int white = 0;
int red = 0;
for (int j = 0; j < source.GetLength(1); j++)
{
if (source[i, j] == 'P')
{
if (source[i, j] > 0)
{
red++;
Console.Write(red + " P ");
}
}
else if(source[i,j]=='F')
{
if (source[i, j] > 0)
{
white++;
Console.Write(white + " F ");
}
}
}
Console.WriteLine();
}
}
}
}
My console output
Source txt file
The compressed txt file which has to be
You was pretty close, but messed one important thing: reset first char counter when second char appear or second char counter when first char appear.
Also source[i, j] > 0 condition do nothing for you.
So at result we may have:
Fill source array:
static void Main(string[] args)
{
// Declare array
char[,] source = new char[7, 10];
// Fill array as at example
for (int i = 0; i < source.GetLength(0); i++)
for (int j = 0; j < source.GetLength(1); j++)
source[i, j] = i == 3 || j == 3 ? 'F' : 'P';
// Print filled array to Console
Console.WriteLine("Source:\n" + new string('-', 10));
for (int i = 0; i < source.GetLength(0); i++)
{
for (int j = 0; j < source.GetLength(1); j++)
Console.Write(source[i, j] + " ");
Console.WriteLine();
}
// Just new line
Console.WriteLine();
// Now "compress"
Console.WriteLine("Compressed:\n" + new string('-', 10));
Compress(source);
Console.ReadKey();
}
Source output:
Compress source array:
static void Compress(char[,] source)
{
// Declare counters for P and F chars
int PCount = 0, FCount = 0;
for (int i = 0; i < source.GetLength(0); i++)
{
for (int j = 0; j < source.GetLength(1); j++)
{
if (source[i, j] == 'P')
{
// If char is P - then count P's up
PCount++;
// If there was F char counted before (> 0) - print it and reset counter
if (FCount > 0)
{
Console.Write(FCount + " F ");
FCount = 0;
}
}
else if (source[i, j] == 'F')
{
// If char is P - then count F's up
FCount++;
// If there was P char counted before (> 0) - print it and reset counter
if (PCount > 0)
{
Console.Write(PCount + " P ");
PCount = 0;
}
}
}
// Before go to next "row" - print remained P or F counts and again, reset counters
if (PCount > 0)
{
Console.Write(PCount + " P ");
PCount = 0;
}
else if (FCount > 0)
{
Console.Write(FCount + " F ");
FCount = 0;
}
// Go new "row"
Console.WriteLine();
}
}
Compressed output:
UPD.
My answer is just solution for a provided example source and example output. #Dmitry Bychenko's solution is universal, not dependent on any two specific characters so in general use I recommend his way.
You can try something like this (you can fiddle with the code):
public static string Compress(char[,] source) {
if (null == source || source.GetLength(0) <= 0 || source.GetLength(1) <= 0)
return "";
StringBuilder sb = new StringBuilder();
for (int row = 0; row < source.GetLength(0); ++row) {
if (sb.Length > 0)
sb.AppendLine();
int count = 0;
char prior = '\0';
bool left = true;
for (int col = 0; col < source.GetLength(1); ++col) {
if (count == 0 || prior == source[row, col])
count += 1;
else {
if (!left)
sb.Append(' ');
left = false;
sb.Append($"{count} {prior}");
count = 1;
}
prior = source[row, col];
}
if (!left)
sb.Append(' ');
sb.Append($"{count} {prior}");
}
return sb.ToString();
}
Demo:
char[,] test = new char[,] {
{'a', 'a', 'a', 'a'},
{'a', 'b', 'c', 'c'},
};
Console.WriteLine(Compress(test));
Outcome:
4 a
1 a 1 b 2 c

Number Pattern Programs in C#

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

What to use instead of null

static int min(int[] arr, int a)
{
int min = arr[0];
for (int i = 1; i < a; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
static void Main(string[] args)
{
int[] arr = new int[10];
int i;
Console.WriteLine("Vnesi števila: ");
for (i = 0; i < 10; i++)
{
int stevilo = int.Parse(Console.ReadLine());
if (stevilo == 0)
break;
arr[i] = stevilo;
}
if (i < 2)
{
Console.WriteLine("Napaka - premalo števil!");
Console.ReadKey();
return;
}
int min1 = min(arr, i);
int? min2 = null;
for (int j = 0; j < i; j++)
{
int stevilo = arr[j];
if(stevilo != min1 && (min2 == null || stevilo < min2))
min2 = stevilo;
}
if(min2 == null)
Console.WriteLine("Napaka - vsa števila enaka!");
else
Console.WriteLine("Drugo najmanjše: " + min2.ToString());
Console.ReadKey();
}
}
}
So this code reads from 2 to 10 numbers and writes out the second smallest out of one of them. You can cancel entering the numbers with the key 0. In a case where the second smallest number can't be written out(all entered are for instance 5 5 5 5), then no number is written out).
BUT I found out that we can't use null values, so what can I replace it with? Thanks!
Initialize the min2 variable to 0 then test it when you need to search for the min value and do not use a nullable integer
int min2 = 0;
for (int j = 0; j < i; j++)
{
int stevilo = arr[j];
if(stevilo != 0 && stevilo != min1 && stevilo < min2))
min2 = stevilo;
}
if(min2 == 0)
Console.WriteLine("Napaka - vsa števila enaka!");
else
Console.WriteLine("Drugo najmanjše: " + min2.ToString());
You should also change the input loop to avoid problems with the user input
int stevilo;
string userInput = Console.ReadLine();
if(!Int32.TryParse(userInput, out stevilo) || stevilo == 0)
break;
arr[i] = stevilo;
The use of Int32.TryParse avoids an exception if your user types something that cannot be converted to an integer number
Well, a strange constraint, but once you have it, then just use a bool flag or the fact that the min2 must be different from min1 like this
// ...
int min1 = min(arr, i);
int min2 = min1;
for (int j = 0; j < i; j++)
{
int stevilo = arr[j];
if(stevilo != min1 && (min2 == min1 || stevilo < min2))
min2 = stevilo;
}
if(min2 != min1)
Console.WriteLine("Napaka - vsa števila enaka!");
else
Console.WriteLine("Drugo najmanjše: " + min2.ToString());

infinite loop in C#

This simple program take an input of integers and print them , but stop printing if it sees 60
string input = string.Empty;
int intValue = 0;
int[] numbers = new int[5];
for (int i = 0; i < 4; i++)
{
input = Console.ReadLine();
if (int.TryParse(input, out intValue))
numbers[i] = intValue;
}
for (int i = 0; i < numbers.Length; i++)
{
while (numbers[i] != 60)
{
Console.WriteLine(intValue);
}
}
the program go on an infinite loop after the 4th input
like that
Input:
1
2
3
4
4
4
4
4
4 ........ and so on
and i don't know the reason .... ^_^
while (numbers[i] != 60)
{
Console.WriteLine(intValue);
}
should be:
if (numbers[i] != 60)
{
Console.WriteLine(intValue);
}
string input = string.Empty;
int intValue = 0;
int[] numbers = new int[5];
for (int i = 0; i < 4; i++)
{
input = Console.ReadLine();
if (int.TryParse(input, out intValue))
numbers[i] = intValue;
}
for (int i = 0; i < numbers.Length; i++)
{
/*while (numbers[i] != 60)*/
if (numbers[i] != 60) // it should be if condition, while statement made it infinite
{
Console.WriteLine(intValue);
}
}

Categories