How do I print # in a nested for loop [duplicate] - c#

This question already has an answer here:
What is an "index out of range" exception, and how do I fix it? [duplicate]
(1 answer)
Closed 4 years ago.
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[6];
doors[i] = "#";
for (int j = 1; j <=i; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
Hi guys. I need to print # one and then # twice, until i get to six times. It says System.index.out.of.range. How come?

You should try to extend your array, it's limited to 6 elements but you try to access 7 elements as you go through 0 to 6.
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[7];
doors[i] = "#";
for (int j = 1; j <=i; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}

If
I need to print # one and then # twice, until i get to six times.
You don't want any array - string[] doors = new string[6];, just loops:
for (int line = 1; line <= 6; ++line) {
for (int column = 1; column <= line; ++column) {
Console.Write('#');
}
Console.WriteLine();
}
If you have to work with array (i.e. array will be used somewhere else), get rid of magic numbers:
// Create and fill the array
string[] doors = new string[6];
for (int i = 0; i < doors.Length; i++)
doors[i] = "#";
// Printing out the array in the desired view
for (int i = 0; i < doors.Length; i++) {
for (int j = 0; j < i; j++) {
Console.Write(doors[j]);
}
Console.Writeline();
}
Please, notice that arrays are zero-based (array with 6 items has 0..5 indexes for them)

because it is out of range.
change it to this:
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[6];
doors[i] = "#";
for (int j = 0; j <=i.length; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}

No need to use 2 loops. Just repeat that character
for (int i = 0; i <= 6; i++)
{
Console.Write(new String("#",i));
Console.WriteLine():
}

Related

No. of rows 1 less than expected in a jagged array

While printing this jagged array I am getting the no of rows to be 1 less than expected. It should start with 0th index to (h-1)th index creating a total of h rows. What am I doing wrong?
h is the no. of rows.
int h = int.Parse(Console.ReadLine());
int[][] arr = new int[h][];
for(int i = 0; i < h; ++i)
{
arr[i] = new int[i+1];
}
for(int i = 0; i < h; i++)
{
Console.WriteLine();
for(int j = 0; j < i; j++)
{
Console.Write(arr[i][j] + " ");
}
}
That's because your inner for-loop has the condition j < i. If i is 0 in the first pass, the inner for-loop will not be passed.
Try it with
for(int j = 0; j < arr[i].Length; j++)
{
Console.Write(arr[i][j] + " ");
}
The arrays have a growing list of elements, starting with 1 so if you want to scan all the items:
for(int i = 0; i < h; i++)
{
Console.WriteLine();
for(int j = 0; j < (i + 1); j++)
{
Console.Write(arr[i][j] + " ");
}
}
Shouldn't it be:
new int[i];
Instead of:
new int[i+1]
Or is it h - 1? Just change that index of the array.
Or you need j < arr[i].Length

Why there is "index exceeded the number of group boundaries" in this code?

For this code in Visual Studio
Point[,] point = new Point[9, 10];
for (int i = 0; i < 9; i++)
{
for(int j = 0; i < 10; j++)
{
point[i, j].X = i;//mark1
point[i, j].Y = j;
}
}
at //mark1,system tell me"index exceeded the number of group boundaries"
Why?
you are doing
for (int j = 0; i < 10; j++)
so the condition i < 10 which is maybe a typo is causing your loop to go out of the range in the array (you are then trying to access the eleement 0, 10 in the array)
replace that with:
for (int j = 0; j < 10; j++)

Multidimensional array initialization c#

I have a problem initializing the following array
char[,] omar = new char[4, 4];
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
omar[i, j] = (char)(Console.Read());
}
}
when I try entering input like this
....
####
####
##..
It only take the first 3 lines not all the fourth , so any help please ?
You're using Console.Read() to read an individual character entered, but when you hit enter, Read() will return either:
A single linefeed character (\n, or decimal 10) if you're on a *nix-like platform;
A carriage-return character (\r, or decimal 13) if you're on Windows. The Read() call directly subsequent to this will then return a linefeed character.
A small modification to get the code you've got to work the way you expect:
char[,] omar = new char[4, 4];
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
omar[i, j] = (char)(Console.Read());
}
Console.Read();
if (Environment.NewLine.Length > 1)
Console.Read();
}

Multiplication table using 2D arrays and methods

I'm a new student having a bit of trouble with this assignment, but the powerpoint notes and other online guides don't seem to help. If anyone can give me a pointer it would be much appreciated!
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 1; i < size+1; i++)
{
for (int j = 1; j < i+1; j++)
{
table[i-1, j-1] = i * j;
}
}
return table;
}
private static void DisplayTT(int[,] table)
{
Console.WriteLine();
Console.WriteLine("Here is the times table for that size:");
Console.WriteLine();
for (int i = 1; i <= table.Length; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("{0}\t", table[i-1, j-1]);
}
Console.WriteLine("\n");
}
Console.WriteLine();
}
The output is supposed to be like this (if you enter 4 for example):
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
But what I'm getting is this, plus it crashes at DisplayTT(table):
1
2 4
3 6 9
4 8 12 16
here's the relevant part of the Main method if it helps.
int size = GetValue("Please enter the size (4-20) of the times table: ", 4, 20);
Console.WriteLine();
int[,] table = GenerateTT(size);
DisplayTT(table);
You can use GetLength(X) Property for multi dimensional arrays where X is index of dimension.
for (int i = 0; i < table.GetLength(0); i++)
{
for (int j = 0; j < table.GetLength(1); j++)
{
Console.Write("{0}\t", table[i, j]);
}
Console.WriteLine("\n");
}
You should change the inner loop hi-bound:
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 1; i < size+1; i++)
{
for (int j = 1; j < i+1; j++) // <-- change i+1 to size+1
{
table[i-1, j-1] = i * j;
}
}
return table;
}
I'd rather keep the loops start from zero:
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
table[i, j] = (i+1) * (j+1);
}
}
return table;
}

C# How to Print 2 arrays together

Ok, so lets say i have two arrays like these;
int[] wow = new int[50];
for (int j = 0; j < wow.Length; j++)
{
wow[j] = j + 1;
}
int[] wew = new int[50];
for (int i = 0; i < wew.Length; i++)
{
wew[i] = i + 10;
}
and i want to print them like;
1 , 11
2 , 12
3 , 13
for (int j = 0; j < wow.Length; j++)
{
wow1[j] = j + 1;
wow2[j] = j + 10;
//print wow1 & wow2 here.
Console.WriteLine("{0},{1}", wow1[j], wow2[j]);
}
Note that in your two loops, i is no different with j, they are essentially the same!
How about using two for-loops?
for(int i = 0; i < wow.Length;i++)
{
for(int j = 0; j < wew.Length;i++)
{
//Print
Console.WriteLine("{0} , {1}", wow[i].ToString(), wew[j].ToString());
}
}
Try this code in case both arrays are the same length
for (int i=0; i<wew.Length; i++)
{
Console.WriteLine(wow[i] + ", " + wew[i]);
}
If the length is different more logic is needed

Categories