I'm trying to learn how to work with 2D-array and I can't seem to understand how to print them correctly. I want to print them in a "square" like 5x5 but all I get is one line. I've tried both WriteLine and Write and changed some of the variables in the loops but I get either an error or not the result I want to have. The code is supposed to print out a 5x5 with a random sequence of 15 numbers in each column. I get the correct numbers out of it, it's only the layout that is wrong.
static void Main(string[] args)
{
Random rnd = new Random();
int[,] bricka = new int[5, 5];
int num = 0;
int num1 = 1;
for (int i = 0; i < bricka.GetLength(1); i++)
{
num += 16;
for (int j = 0; j < bricka.GetLength(0); j++)
{
bricka[j, i] = rnd.Next(num1, num);
}
num1 += 16;
}
for (int i = 0; i < bricka.GetLength(0); i++)
{
for (int j = 0; j < bricka.GetLength(1); j++)
{
Console.Write(bricka[i, j]+ " " );
}
}
Console.ReadKey();
}
This is my print, I would like to have the the 12 under the 8 and 14 under the 12 and so on.
http://i.imgur.com/tfyRxf1.png
You need to call WriteLine() after each line, so that each line is printed on a separate line:
for (int i = 0; i < bricka.GetLength(0); i++)
{
for (int j = 0; j < bricka.GetLength(1); j++)
{
Console.Write(bricka[i, j]+ " " );
}
Console.WriteLine();
}
That would be one way of doing it, anyway.
Related
So I wrote bucket sort implementation (I use rnd.NextDouble() to fill my array, so all the elements are in the range between 0 and 1). And I have number of experiments (10 experiments for each array size) and different array sizes (I start with 1000 and then +1000, etc. till 300.000). And sometimes it works fine, but sometimes it gives me OutOfRange exception:
public static void BucketSortImplement(ref int countOfElements, ref float[] array)
{
List<float>[] buckets = new List<float>[countOfElements];
for (int i = 0; i < countOfElements; i++)
{
buckets[i] = new List<float>();
}
for (int i = 0; i < countOfElements; i++)
{
float indexOfElement = array[i] * countOfElements;
buckets[(int)indexOfElement].Add(array[i]); // right here
}
for (int i = 0; i < countOfElements; i++)
{
for (int j = 0; j < buckets[i].Count; j++)
{
float keyElement = buckets[i][j];
int k = j - 1;
while (k >= 0 && buckets[i][k] > keyElement)
{
buckets[i][k + 1] = buckets[i][k];
k -= 1;
}
buckets[i][k + 1] = keyElement;
}
}
int arrayIndex = 0;
for (int i = 0; i < countOfElements; i++)
{
for (int j = 0; j < buckets[i].Count; j++)
{
array[arrayIndex++] = buckets[i][j];
}
}
}
I am a bit confused, because the algorithm itself looks fine, that is I have to calculate array[i] * countOfElements to get the index where I can put my element. Could you please direct me?
If you array contain value '1' it will lead to OutOfRange, because if you have a size of the list equal to 3 (for example) then valid indexes will be in the range [0, 2].
I am new to programming and I want my program to run a table consisting of stars(asterisks). E.g. table 4x3 has 4x3 stars. But my initial problem is that I do not know how to implement a multidimensional array in such a way that I just need to change the initial value of rows and columns in order to create more or less stars.
So: here is my code at the moment:
using System;
namespace ConsoleApplication1
{
class Multidimensional_array
{
static void Main(string[] args)
{
int arrayRows = 4;
int arrayCols = 3;
int[,] arrayTimes;
arrayTimes = new int [arrayRows, arrayCols];
//String star = "*";
for( int i = 0; i <= arrayRows; i++) {
for( int j = 0; j <= arrayCols; j++) {
//Console.WriteLine("*");
//arrayTimes[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.ReadLine();
}
}
}
So I just want that if I change the int arrayRows to 5 and int arrayCols to 5 then I receive a star table of 5x5. T
I think you're very close, you're just using the wrong data type for your array, haven't assigned the * to each position in said array, and your current code would give you an ArrayIndexOutOfBounds exception. You may have 4 rows and 3 columns, but array indices are zero-based, meaning when you access positions 1, 2, 3, etc. you use an index of 0, 1, 2, etc. respectively.
So, since you want to store the text "*", you should use a char[,] or a string[,] for your multi-dimensional array. I've chosen char[,] for this:
int arrayRows = 4;
int arrayCols = 3;
char[,] arrayTimes = new char[arrayRows, arrayCols];
const char star = '*';
// Set it up
for (int i = 0; i <= arrayRows - 1; i++)
{
for (int j = 0; j <= arrayCols - 1; j++)
{
arrayTimes[i, j] = star;
}
}
// Print it out
for (int i = 0; i <= arrayRows - 1; i++)
{
for (int j = 0; j <= arrayCols - 1; j++)
{
Console.Write(arrayTimes[i, j]);
}
Console.WriteLine();
}
Working ideone sample
Simple solution
static void Main(string[] args)
{
// Get the number of rows
Console.WriteLine("Enter the number of rows:");
int arrayRows = Convert.ToInt32(Console.ReadLine());
// Get the number of columns
Console.WriteLine("Enter the number of columns:");
int arrayCols = Convert.ToInt32(Console.ReadLine());
// For each item in the row
for (int i = 0; i < arrayRows; i++)
{
// For each item in the column
for (int j = 0; j < arrayCols; j++)
{
// Show a star
Console.Write("*");
}
// End the line
Console.WriteLine("");
}
// Read the line to stop the app from closing
Console.ReadLine();
}
If you want to store the stars
static void Main(string[] args)
{
// Get the number of rows
Console.WriteLine("Enter the number of rows:");
int arrayRows = Convert.ToInt32(Console.ReadLine());
// Get the number of columns
Console.WriteLine("Enter the number of columns:");
int arrayCols = Convert.ToInt32(Console.ReadLine());
// Create an array
char[,] arrayTimes = new char[arrayRows, arrayCols];
char star = '*';
// For each item in the row
for (int i = 0; i < arrayRows; i++)
{
// For each item in the column
for (int j = 0; j < arrayCols; j++)
{
// Show a star
arrayTimes[i, j] = star;
}
}
// Read the line to stop the app from closing
Console.ReadLine();
}
This do what you want,
int arrayRows = 2;
int arrayCols = 2;
char[,] arrayTimes;
arrayTimes = new char[arrayRows, arrayCols];
//String star = "*";
for (int i = 0; i < arrayRows; i++)
{
for (int j = 0; j < arrayCols; j++)
{
arrayTimes[i, j] = '*';
Console.Write("{0}",arrayTimes[i, j]);
}
Console.WriteLine();
}
Console.ReadKey();
I'm trying to make a program that: When given a 4 digit number (for example, 1001) it sums the digits of that number, for 1001 this is 1 + 0 + 0 + 1 = 2, than it finds all sequences of 6 numbers from 1 to 6 (including permutations, i.e. 1*1*1*1*1*2 is different than 2*1*1*1*1*1) whose product is that number.
The result should be printed on the console in the following format: each sequence of 6 numbers, with their Morse representation, separated with a single pipe: 1 is .---- , 2 is ..---: .----|.----|.----|.----|..---|, on a new row the next permutation: .----|.----|.----|..---|.----| and so on.
The problem is, my solution doesn't show the correct answers, not even the correct number of them.
Here's my code (and please, if possible, tell me where my mistake is, and not some one line hack solutions to the problem with LINQ and regex and God knows what):
string n = Console.ReadLine();
string[] digitsChar = new string[n.Length];
for (int i = 0; i < 4; i++)
{
digitsChar[i] = n[i].ToString();
}
int[] digits = new int[digitsChar.Length];
for (int i = 0; i < 4; i++)
{
digits[i] = Convert.ToInt32(digitsChar[i]);
}
int morseProduct = digits.Sum();
Console.WriteLine(morseProduct);
List<int> morseCodeNumbers = new List<int>();
for (int i = 1; i < 6; i++)
{
for (int j = 1; i < 6; i++)
{
for (int k = 1; i < 6; i++)
{
for (int l = 1; i < 6; i++)
{
for (int m = 1; i < 6; i++)
{
for (int o = 1; o < 6; o++)
{
int product = i * j * k * l * m * o;
if (product == morseProduct)
{
morseCodeNumbers.Add(i);
morseCodeNumbers.Add(j);
morseCodeNumbers.Add(k);
morseCodeNumbers.Add(l);
morseCodeNumbers.Add(m);
morseCodeNumbers.Add(o);
}
}
}
}
}
}
}
int numberOfNumbers = morseCodeNumbers.Count;
string[] morseCodes = new string[] { "-----", ".----", "..---", "...--", "....-", "....." };
for (int i = 0; i < numberOfNumbers; i++)
{
int counter = 0;
if (i % 5 == 0)
{
Console.WriteLine();
counter = 0;
}
if (counter < 5)
{
int index = morseCodeNumbers[i];
Console.Write(morseCodes[index] + "|");
counter++;
}
A lot of your for-loop conditions refer to i instead of j,k,l and m. The same for the increment part. For example:
for (int j = 1; i < 6; i++)
should be
for (int j = 1; j < 6; j++)
Furthermore if the range is from 1 to 6 you need to change < to <=, see:
for (int i = 1; i <= 6; i++)
You don't need to convert the string to a string array to get the int array of digits btw, so while this is correct:
for (int i = 0; i < 4; i++)
{
digitsChar[i] = n[i].ToString();
}
int[] digits = new int[digitsChar.Length];
for (int i = 0; i < 4; i++)
{
digits[i] = Convert.ToInt32(digitsChar[i]);
}
you could it do like that (sry for LINQ):
var digits = n.Select(c=>(int)char.GetNumericValue(c) ).ToArray();
Console.WriteLine automatically move text on a new line and I have output on the console like:
1
2
3
4
5
..
But I need:
1234
5..
Code:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
Console.WriteLine(aField[i, j]);
Console.WriteLine();
}
If you don't want to write a line, don't use Console.Write*Line*. Just use Console.Write:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(aField[i, j]);
}
Console.WriteLine();
}
(Note that your sample output only contains four characters, whereas your inner loop has 5 iterations. I'm hoping this is just a sample discrepancy, and nothing more complicated.)
I have written this segment of C# to help me understand how nested for loops can be used to render 2 dimensional data.
Here is what the output looks like.
████
███
██
█
I would like to make it so that the 4 blocks up top are rendered at the bottom, basically in the reverse order so that the steps ascend. However the console window only renders downward, so the conventional thinking won't be right. The following is my code.
static void Main(string[] args)
{
int i = 0;
int j = 0;
for (i = 0; i < 4; i++)
{
Console.Write('\n');
for (j = i; j < 4; j++)
{
Console.Write("█");
}
}
Console.ReadKey();
}
This is what I'd like the output to look like.
█
██
███
████
You need to reverse your loop condition from inremant to decremant..
for (i = 0; i < 4; i++)
{
Console.Write('\n');
for (j = i; j >= 0; j--)
{
Console.Write("█");
}
}
Output will be;
Here is a DEMO.
UPDATE: Since you change your mind, you need to add space every column (column number isi) 4 - 1 times.
public static void Main(string[] args)
{
int i = 0;
int j = 0;
for ( i = 0; i < 4; i++ )
{
for ( j = 0; j < 4; j++ )
{
if ( j < 3 - i )
Console.Write(" ");
else
Console.Write("█");
}
Console.Write('\n');
}
Console.ReadKey();
}
Here is a DEMO.
class Program
{
const int Dimension = 4;
static void Main(string[] args)
{
char[] blocks = new char[Dimension];
for (int j = 0; j < Dimension; j++)
blocks[j] = ' ';
for (int i = 0; i < Dimension; i++)
{
blocks[Dimension - i - 1] = '█';
for (int j = 0; j < Dimension; j++)
Console.Write(blocks[j]);
Console.WriteLine();
}
Console.ReadKey();
}
}
Should be:
for (j = 3 - i; j < 4; j++)
{
Console.Write("█");
}
The easiest way would be: just reverse your inner loop condition and decrement the counter instead of incrementing it:
for (i = 0; i < 4; i++)
{
Console.Write('\n');
for (j = i; j >= 0; j--)
{
Console.Write("█");
}
}
Console.ReadKey();
returning:
█
██
███
████
And for right-to-left version:
for (i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(j < 3 - i)
Console.Write(" ");
else
Console.Write("█");
}
Console.Write('\n');
}
Console.ReadKey();
with a result:
█
██
███
████