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;
}
Related
I am creating a 2D array that can have any custom size 1x1, 2x2, 3x3 .... nxn and depending on what the size of the array is, it gets filled with 0 to (n^2)-1, so if it is 2x2 it outputs:
3 2
1 0
It should be something like this int[,] arr = new int [n, n];, to create the 2d array, but how would the for loops be constructed?
what I have tried
using System;
namespace moveElement {
public class move {
static void Main(string[] args) {
int n = 3;
int[, ] arr = new int[n, n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
The result of the code:
0 0 0
0 0 0
0 0 0
int k = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i,j] = k++;
}
}
full code to the question:
using System;
namespace moveElement
{
public class move
{
static void Main(string[] args)
{
int n = 3;
int[,] arr = new int [n, n];
//Ivan Smyrnov solution
int k = (n * n) - 1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i,j] = k--;
}
}
//Ivan Smyrnov solution
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
Output when n is 3:
8 7 6
5 4 3
2 1 0
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
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
So the start is:
Random r = new Random();
int[,] mas = new int[4, 5];
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = 0; j < mas.GetLength(1); j++)
{
mas[i, j] = r.Next(1, 10);
Console.Write("{0}\t", mas[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Looks something like
4 3 5 6 2
3 5 6 7 4
2 3 4 5 5
2 3 4 5 6
What i i need is to get 0 above the diagonal.
4 0 0 0 0
3 5 0 0 0
2 3 4 0 0
2 3 4 5 0
This is what i've got so far, no what i need , but atleast got some diagonal and some 0's.
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = i; j < mas.GetLength(1); j++)
{
mas[i, j] = 0;
Console.Write("{0}\t", mas[i, j]);
}
Console.WriteLine();
}
If you want to do everything in one go, you could do this:
Random r = new Random();
int[,] mas = new int[4, 5];
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = 0; j < mas.GetLength(1); j++)
{
mas[i, j] = j > i ? 0 : r.Next(1, 10);
Console.Write("{0}\t", mas[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Otherwise, your second section needs to be:
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = i; j < mas.GetLength(1); j++)
{
if(j > i) mas[i, j] = 0;
Console.Write("{0}\t", mas[i, j]);
}
Console.WriteLine();
}
try this one (inside the inner loop):
if(j > i ) mas[i, j] = 0; // column number > row number, above diagonal
or the (better) alternative (inside the outer loop):
for (int j = i+1; j < mas.GetLength(1); j++)
{
mas[i, j] = 0;
Console.Write("{0}\t", mas[i, j]);
}
Using Linq
Random r = new Random();
//int[,] mas = new int[4, 5];
int[][] mas = new int[4][];
for (int i = 0; i < mas.Length; i++)
{
mas[i] = new int[5];
for (int j = 0; j < mas[i].Length; j++)
{
mas[i][j] = r.Next(1, 10);
Console.Write("{0}\t", mas[i][j]);
}
Console.WriteLine();
}
for (int i = 0; i < mas.Length; i++)
{
mas[i] = mas[i].Select((c, ind) =>
{
if (ind > i)
c = 0;
return c;
}).ToArray();
}
I am wanting to create a 2d array to produce a grid of int values the size of the grid needs to be 100 * 100
Example Grid
1 2 3 .........100
101 102 103.........200
201 202 303.........300
I produced ths
int rows = 100;
int columns = 100;
public int[,] InitIntArray() {
int[,] grid = new int[rows,columns];
int number = 1;
for (int i = 0; i != rows; i++)
{
for (int j = 0; j != columns; j++)
grid[i, j] = number;
number++;
}
return grid;
}
When I use my code everything seems to work fine until I try to access the columns. all I get when I use grid.getValue() is 1.
Missed one curvy
for (int i = 0; i != rows; i++)
{
for (int j = 0; j != columns; j++)
{
grid[i, j] = number;
number++;
}
}
for (int i = 0; i < rows; i++)
And the other for-loop
for (int j = 0; j < columns; j++)
{
grid[i, j] = number;
number ++;
}
You might want to read on how for-loops work, http://www.thegeekstuff.com/2012/12/c-loops-examples/