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

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

Related

C# also gives Double Folded Array Leash

int[] x = { 1, 2, 3, 4,5 };
int[] y = { 5, 4, 3, 2, 1 };
int[,] s=new int[(x.Length)*(x.Length),2];
for (int i = 0; i < x.Length; i++)
{
for (int j = 0; j < y.Length; i++)
{
s[i, j] = x[i] * y[j];
Console.WriteLine(x[i] + " * " + y[j] + " = " + s[i, j]);
}
}
IndexOutOfRangeException: Index was outside the bounds of the array.
It gives a memory error, but it says it hangs the memory of the variable, but I transfer more than the field of the series. I couldn't figure out what the problem was. Can you help?
IndexOutOfRangeException: Index was outside the bounds of the array.
Change:
for (int i = 0; i < x.Length; i++)
{
for (int j = 0; j < y.Length; i++)
To:
for (int i = 0; i <= x.Length-1; i++)
{
for (int j = 0; j <= y.Length-1; j++)
Also this line:
int[,] s=new int[(x.Length)*(x.Length),2];
Although no big problems but if x[] and y[] are different lengths might cause issues with storing i,j values. Did you mean to declare like :=
int[,] s=new int[(x.Length)*(y.Length),2];

How to find the smallest value in a 2d dynamic array

I am trying to find the smallest number in a dynamic 2d array but i have
hit a block and cannot figure out how this is done. I have done all the set up and populated with values.
using System;
namespace _2D_Array
{
class Program
{
static void Main(string[] args)
{
// Create integers n and r and set up the 2D array
int n = 5;
int[,] a;
int R = 50;
int x;
a = new int[n + 1, n + 1];
// Create number input randomizer
Random r = new Random();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
a[i, j] = r.Next(1, R);
// Print array with random numbers to screen to check set up is correct for i and j
Console.Write(" ");
for (int i = 1; i <= n; ++i)
Console.Write(i.ToString("00" + " "));
Console.WriteLine();
Console.WriteLine();
for (int i = 1; i <= n; ++i)
{
Console.Write(i.ToString("00" + " "));
for (int j = 1; j <= n; ++j)
Console.Write(a[i, j].ToString("00" + " "));
Console.WriteLine();
}
// Get Search Value
Console.Write("What's the value to look for? ");
x = int.Parse(Console.ReadLine());
// look through the array
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
// If the element is found
if (a[i, j] == x)
{
Console.Write("Element found at (" + i + ", " + j + ")\n");
}
}
Console.Write(" Element not found");
```
This is all i have so far. in the end i want to have the smallest
value from a set of random numbers set in my 5x5 array.
First of all, format out your code: hard to read code is hard to debug.
Often, we can query array with a help of Linq:
using System.Linq;
...
int min = a.OfType<int>().Min();
Console.Write($"The smallest element is {min}");
If you want good old nested loops:
// not 0! What if min = 123?
// Another possibility is min = a[0, 0]; - min is the 1st item
int min = int.MaxValue;
// note a.GetLength(0), a.GetLength(0): n is redundant here
// and there's no guarantee that we have a square array
for (int r = 0; r < a.GetLength(0); ++r)
for (int c = 0; c < a.GetLength(1); ++c)
min = Math.Min(min, a[r, c]);
Console.Write($"The smallest element is {min}");

Check common elements of two-dimensional arrays of integers, considering the positions they are on

at the beginning I want to mention that I am a beginner in programming. So, I want to write a program that checks the similarity of two-dimensional arrays of integers. The similarity is to be determined by the amount of numbers that are in the same positions in both tables. The user gives the number of columns in the table and the elements themselves, number of rows is the same all the time.The similarity result is displayed as a percentage and the similarity itself should be calculated taking into account the number of elements of the larger array. My problem is: When the two arrays are the same size, the program throws the exception and it doesn't check all the numbers in the column.(I wrote before program for one dimensional array and it works perfectly) So far I have managed to write something like this:
This is what I want to do In the picture, the similarity between the arrays is 20%
{
Console.WriteLine("How extensive is the first table supposed to be?");
int n = int.Parse(Console.ReadLine());
int z = 2;
int[,] tab1 = new int[2, n];
Console.WriteLine("Enter the numbers into the first array:");
for (int i = 0; i < z; i++)
{
for (int j = 0; j < n; j++)
{
tab1[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
int rowLength = tab1.GetLength(0);
int colLength = tab1.GetLength(1);
for (int i = 0; i < rowLength; i++)
{
for (int j = 0; j < colLength; j++)
{
Console.Write(string.Format("{0} ", tab1[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
Console.WriteLine("How extensive is the second table supposed to be?");
int m = int.Parse(Console.ReadLine());
int b = 2;
int[,] tab2 = new int[2, m];
Console.WriteLine("Enter the numbers into the second array: ");
for (int i = 0; i < b; i++)
{
for (int j = 0; j < m; j++)
{
tab2[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
int Len4gth = tab2.GetLength(0);
int Len2gth = tab2.GetLength(1);
for (int i = 0; i < Len4gth; i++)
{
for (int j = 0; j < Len2gth; j++)
{
Console.Write(string.Format("{0} ", tab2[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
double similarity= 0;
if (tab1.GetLength(1) > tab2.GetLength(1))
{
for (int i = 0; i < tab2.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.GetLength(1) < tab2.GetLength(1))
{
for (int i = 0; i < tab1.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab2[i, j] == tab1[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.GetLength(1) == tab2.GetLength(1))
{
for (int i = 0; i < tab1.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.Length < tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab2.Length) * 100 + "%");
}
if (tab1.Length > tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab1.Length) * 100 + "%");
}
if (tab1.Length == tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab2.Length) * 100 + "%");
}
Console.ReadKey();
You must compare each element of the first array with the elements of the second array.
use this code :
//get first array items
Console.WriteLine("How extensive is the first table supposed to be?");
int n = int.Parse(Console.ReadLine());
int[,] tab1 = new int[2, n];
Console.WriteLine("Enter the numbers into the first array:");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
tab1[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
//write first array items
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(string.Format("{0} ", tab1[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
//get second array items
Console.WriteLine("How extensive is the second table supposed to be?");
int m = int.Parse(Console.ReadLine());
int[,] tab2 = new int[2, m];
Console.WriteLine("Enter the numbers into the second array: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < m; j++)
{
tab2[i, j] = int.Parse(Console.ReadLine());
}
}
//write second array items
Console.WriteLine("\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(string.Format("{0} ", tab2[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
//find similarity items
double similarity = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
int firstValue = tab1[i, j];
for (int k = 0; k < 2; k++)
{
for (int d = 0; d < m; d++)
{
if (firstValue == tab2[k, d])
{
similarity += 1;
}
}
}
}
}
double percentage = n > m ? ((similarity / tab1.Length) * 100) : ((similarity / tab2.Length) * 100);
Console.WriteLine("The similarity of the arrays is: " + percentage + "%");
Console.ReadKey();
this code work without error and It does not matter which array is larger.
If you want similar elements like this example enter link description here, use this code snippet to find similar elements
//find similarity items
double similarity = 0;
int z = n > m ? m : n;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity += 1;
}
}
}
double percentage = n > m ? ((similarity / tab1.Length) * 100) : ((similarity / tab2.Length) * 100);
Console.WriteLine("The similarity of the arrays is: " + percentage + "%");
Console.ReadKey();

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