Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I made a two dimensional array in c # with random numbers but I want the numbers are not repeated
For example, a successful output giving the input 4 (x), 4 (y), 15 (maxElem), would be:
14|8|1|7
3|13|2|4
2|6|12|8
10|9|4|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TwoDimensionArray
{
class Program
{
static void Main(string[] args)
{
int x = 4;
int y = 4;
int[,] z = new int[x, y];
Random r = new Random();
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
z[i, j] = r.Next(1, 15);
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (i == 3 && j == 3)
{
Console.Write(" ");
break;
}
else
{
if (z[i, j] > 9)
Console.Write(z[i, j] + " ");
else
Console.Write(z[i, j] + " ");
}
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
Generate an array containing the valid numbers, then use a shuffle algorithm to randomize the order in the array, and then finally populate your two-dimensional array by retrieving in order the values from your shuffled array.
Try doing it like this:
int x = 4;
int y = 4;
int[,] z = new int[x, y];
Random r = new Random();
var values =
Enumerable
.Range(1, x * y)
.OrderBy(n => r.Next())
.ToArray();
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
z[i, j] = values[i * 4 + j];
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
does anyone know how to convert a List to a 3D Array?. My input list will actually always be a "flattened" verison of a 3D Array, so I will always know the arrays dimensions. Any clues would be great
T[,] output = new T[height, width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
output[i, j] = input[i * width + j];
}
}
This is to convert a List/Array to a 2D Array, but I dont seem to wrap my head around to convert to a 3D Array
You will need to know each dimension of the three of the 3D array. Lets say they are d1, d2, and d3, then you can use this code to get the array you want, assuming an int array:
int i, j, k, p;
int[,,] Arr = new int[d1, d2, d3];
p = 0;
for (i = 0; i < d1; i++)
for (j = 0; j < d2; j++)
for (k = 0; k < d3; k++)
a[i, j, k] = lst[p++];
If you want a solution similar to you example you can try this:
int i, j, k;
int[,,] Arr = new int[d1, d2, d3];
for (i = 0; i < d1; i++)
for (j = 0; j < d2; j++)
for (k = 0; k < d3; k++)
a[i, j, k] = lst[i * d2 * d3 + j * d3 + k];
You just need to know which order the items were stored in when going from an array to a list, and from that you can see how to calculate the index into the list:
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// N.B. You will have to pick which order the dimensions go in.
var ni = 4;
var nj = 3;
var nk = 2;
// Make a list that could be interpreted as a 3-d array:
var x = new List<string>();
for (int k = 0; k < nk; k++)
{
for (int j = 0; j < nj; j++)
{
for (int i = 0; i < ni; i++)
{
x.Add($"{k}-{j}-{i}");
}
}
}
Console.WriteLine(string.Join(", ", x));
// Copy the content of the list to a 3-d array:
string[,,] array1 = new string[nk, nj, ni];
for (int k = 0; k < nk; k++)
{
for (int j = 0; j < nj; j++)
{
for (int i = 0; i < ni; i++)
{
var idx = i + j * (nj + 1) + k * (nk + 1) * (nj + 1);
array1[k, j, i] = x[idx];
Console.Write(array1[k, j, i] + ", ");
}
}
}
Console.ReadLine();
}
}
}
Which outputs, for confirmation,
0-0-0, 0-0-1, 0-0-2, 0-0-3, 0-1-0, 0-1-1, 0-1-2, 0-1-3, 0-2-0, 0-2-1, 0-2-2, 0-2-3, 1-0-0, 1-0-1, 1-0-2, 1-0-3, 1-1-0, 1-1-1, 1-1-2, 1-1-3, 1-2-0, 1-2-1, 1-2-2, 1-2-3
0-0-0, 0-0-1, 0-0-2, 0-0-3, 0-1-0, 0-1-1, 0-1-2, 0-1-3, 0-2-0, 0-2-1, 0-2-2, 0-2-3, 1-0-0, 1-0-1, 1-0-2, 1-0-3, 1-1-0, 1-1-1, 1-1-2, 1-1-3, 1-2-0, 1-2-1, 1-2-2, 1-2-3,
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 3; k++)
a[i, j ,k] = x;
This question already has answers here:
Get minimum and maximum value using linq
(3 answers)
Closed 4 years ago.
I'm trying to make this code as efficient as possible, what is the best and most correct way to do this? Also, I need to find the MIN and MAX values for each column and row and display the values next to the specific row and column.
Code:
using System;
using System.Linq;
public class Program
{
public static void Main()
{
Random rand = new Random();
int ri, ki;
int i, j;
int max = 1;
int min = 0;
int rndnumber = rand.Next(64, 128);
int[,] array = new int[9, 4]; // Izveido array 9x4 (rin x kol)
for (i = 0; i < 9; i++) //
{
for (j = 0; j < 4; j++)
array[i, j] = rand.Next(1, 100 + 1);
}
for (i = 0; i < 9; i++) //
{
for (j = 0; j < 4; j++)
{
Console.Write("|\t{0}\t ", array[i, j]); // /t - tab
}
Console.WriteLine("\n"); // new line
}
for (i = 0; i < 9; i++) //
{
for (j = 0; j < 4; j++)
if (max < array[i, j]) // Aprekina max
{
max = array[i, j];
}
}
Console.WriteLine("Maksimalais sk tabula: {0}", max);
Console.WriteLine("Min sk tabula: {0}", min);
//rndnumber = rand.Next();
//Console.WriteLine("Hello World {0}", rndnumber);
Console.ReadKey();
/*
Izveidot ka zem katras rindas un kol ir min un max skaitlis
*/
}
}
If you make the multidimensional array into an IEnumerable, you can use Linq on it:
// Order and use Cast<int>() to get an ordered IEnumerable<int>,
var orderedAscending = from x in array.Cast<int>()
orderby x
select x;
// Use linq First() & Last() to get min and max.
var min = orderedAscending.First();
var max = orderedAscending.Last();
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The following program is going in infinite loop when I am trying to access a multiple line input,do you have any idea why is it not working?
namespace AlternatingCharacters
{
class Program
{
static void Main(string[] args)
{
int N = Int32.Parse(Console.ReadLine());
string[] str = new string[N];
for (int i = 0; i < N ; i++)
{
str[i] = Console.ReadLine();
}
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; j < strArray.Length; j++)
{
if (strArray[i] == strArray[i + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
}
}
}
The problem is in this line:
for (int j = 0; i < strArray.Length; j++)
Your condition is checking on i, not on j, so i will always be 0 (start value) and will never change during the loop.
The right code is:
for (int j = 0; j < strArray.Length; j++)
After that it will fail on this line:
if (strArray[i] == strArray[i + 1])
At the end, it can't find 'the last index + 1' which you can prevent by subtracting one on the end, so this (also I think you need j here):
for (int j = 0; j < strArray.Length - 1; j++)
{
if (strArray[j] == strArray[j + 1])
{
count++;
}
}
The error comes from this piece of code:
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; i < strArray.Length; j++)
{
if (strArray[i] == strArray[i + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
First your loop-condition of the inner loop is whrong. While you increment j you check if i equals the number of elements in your array. More critical than this however is that you also use the whring array-elements. I guess you should use if (strArray[j] == strArray[j + 1]) instead of using i as index.
So all in all this should work:
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; j < strArray.Length; j++)
{
if (strArray[j] == strArray[j + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Currently designing an application that will compute for the sale of salesment for 4 month given an initial sale. The sale is expected to raise by 5% every month in 4 months.
ProduceSalesProjectionTable function seems to be giving me an out of bound error and i don't know why can't seem to figure it out.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace itse
{
class TempAgency
{
static string[] salesman1 = new string[4];
static double[,] sales1 = new double[4, 5];
static void DisplayInstructions()
{
Console.WriteLine("You will be asked to enter data for four salesmen.");
Console.WriteLine("For their name, enter their first name followed");
Console.WriteLine("by a space and then their las name.");
Console.WriteLine("");
Console.WriteLine("For testing purposes enter data for four <4> salesmen.");
Console.WriteLine("");
Console.WriteLine("");
}
static void GetSalesData(string[] salesman, double[,] sales)
{
for (int x = 0; x < 4; x++)
{
Console.WriteLine("Enter name: ");
salesman[x] = Console.ReadLine();
string [] split = salesman[x].Split(new Char [] {' '});
Console.WriteLine(split[0]+"'s Sales goal: ");
sales[x,0]=Convert.ToDouble(Console.ReadLine());
}
}
static void ProduceSalesProjectionTable(double[,] sales)
{
for (int i = 0; i < 4; i++)
{
double salesPercentage = 0.05;
for (int j = 0; j < 4; i++)
{
sales[i, j + 1] = sales[i, 0] + (sales[i, 0] * salesPercentage); //here lies the problem
salesPercentage += 0.05;
}
}
}
static void DisplaySalesProjections()
{
}
public static void Main(String[] args)
{
DisplayInstructions();
GetSalesData(salesman1, sales1);
//Console.WriteLine("salesman: " + salesman[0]);
//Console.WriteLine("sales: " + sales[0, 0]);
ProduceSalesProjectionTable(sales1);
//for (int i = 0; i < 4; i++)
//{
// //double salesPercentage = 0.05;
// for (int j = 0; j < 4; i++)
// {
// Console.WriteLine("salesman: " + salesman[i] + "\tsales: " + sales[i, j]);
// }
//}
//Console.WriteLine("sales:" + sales[0, 1]);
//Console.WriteLine("sales:" + sales[1, 1]);
//Console.WriteLine("sales:" + sales[2, 1]);
//Console.WriteLine("sales:" + sales[3, 1]);
for (int i = 0; i < 4; i++)
{
//double salesPercentage = 0.05;
for (int j = 0; j < 4; i++)
{
Console.WriteLine("salesman: " + salesman1[i] + "\tsales: " + sales1[i, 0]);
}
}
Console.ReadLine();
}
}
}
Look carefully at the line
for (int j = 0; j < 4; i++)
Your indexer is j but you are increasing i instead of j.
Eventually sales[i, j + 1] evaluates to sales[4, 1] which is out of bounds.
Solution: change your code to
for (int j = 0; j < 4; j++)
(You have done this in 2 places)
If you build your for-loops like this, there should be no "out of bound error":
static void ProduceSalesProjectionTable(double[,] sales)
{
for (int i = 0; i < sales1.GetLength(0); i++)
{
for (int j = 0; j < sales1.GetLength(1); i++)
{
...Do Stuff...
}
}
}
When you use 2-dimensional arrays GetLength(0) gives you the length of the first dimension of your 2-dimensional array and GetLength(1) gives you the length of he second dimension.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
In c# I've got four 10x10 int square matrices and I need to create a 20x20 square matrix by merging the four smaller matrices.
What is the best way to accomplish this?
EDIT: This is my code
int[] first = myArray.Take(myArray.Length / 2).ToArray();
int[] second = myArray.Skip(myArray.Length / 2).ToArray();
int[,] matrice0 = MatrixCalc(first, first);
int[,] matrice1 = MatrixCalc(first, second);
int[,] matrice2 = MatrixCalc(second, first);
int[,] matrice3 = MatrixCalc(second, second);
// Need to join these four matrices here like this: [[0 1][2 3]]
Quickly put together a simple non-scalable solution (only for 4 matrices, if you need a scalable solution you can look at having the matrix as a list of lists and concatenate them) that supposes the matrix lenght is the same. Haven't compiled it so sorry for any bugs
int[,] joinedMatrice = new int[matrice0.GetLength(0) + matrice1.GetLength(0), matrice0.GetLength(1) + matrice2.GetLength(1)];
for (int i = 0; i < matrice0.GetLength(0) + matrice1.GetLength(0); i++)
{
for (int j = 0; j < matrice0.GetLength(1) + matrice2.GetLength(1); j++)
{
int value = 0;
if (i < matrice0.GetLength(0) && j < matrice0.GetLength(1))
{
value = matrice0[i, j];
}
else if (i >= matrice0.GetLength(0) && j < matrice0.GetLength(1))
{
value = matrice1[i - matrice0.GetLength(0), j];
}
else if (i < matrice0.GetLength(0) && j >= matrice0.GetLength(1))
{
value = matrice2[i, j - matrice0.GetLength(1)];
}
else if (i >= matrice0.GetLength(0) && j >= matrice0.GetLength(1))
{
value = matrice3[i - matrice0.GetLength(0), j - matrice0.GetLength(1)];
}
joinedMatrice[i, j] = value;
}
}
You can do this:
// pre-arrange them in the form you want
List<List<int[,]>> sources = new List<List<int[,]>>() {
new List<int[,]>() {matrice0, matrice1},
new List<int[,]>() {matrice2, matrice3}
};
int[,] joint = new int[20, 20];
for (int i = 0; i < joint.GetLength(0); i++) {
for (int j = 0; j < joint.GetLength(1); j++) {
// select the matrix corresponding to value (i,j)
int[,] source = sources[i / matrice0.GetLength(0)][j / matrice0.GetLength(1)];
// and copy the value
joint[i, j] = source[i % matrice0.GetLength(0), j % matrice0.GetLength(1)];
}
}