So the problem is that I should be able to first write a value and then it should be passed but, its executing all the code at once and not giving me time to write the value i want to give it
public void FillMatrix(string [,] v) {
for (int i = 2; i <= 4; i+=2) {
for (int j = 2; j <= 4; j++) {
lblFute.Text = "Write the number you want to put on index " + i + " " + j;
v[i, j] = txtVlera.Text;
if (j == 3 ) {
lblFute.Text = "Write the number you want to put on index " + j + " " + i;
v[j, i] = txtVlera.Text;
}
}
}
}
Related
Attempting the hackerRank Q from https://www.hackerrank.com/challenges/2d-array
static void Main(String[] args)
{
int[][] arr = new int[6][];
for (int arr_i = 0; arr_i < 6; arr_i++)
{
string[] arr_temp = Console.ReadLine().Split(' ');
arr[arr_i] = Array.ConvertAll(arr_temp, Int32.Parse);
}
int[] sum = new int[6];
List<int> n = new List<int>();
for (int i = 0; i + 2 < arr.Length; i++)
{
for (int j = 0; j + 2 < arr.GetLength(0); j++)
{
sum[j] = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] +
arr[i + 1][j + 1] +
arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
n.Add(sum[j]);
}
}
Console.WriteLine(n.Max());
}
}
}
If I run this program and enter the following as contents of 'arr'
111111
222222
333333
444444
555555
666666
Here I am trying to add 1 + 1 + 1
+ 2 +
3 + 3 + 3
using sum[j] = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] +
arr[i + 1][j + 1] +
arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
but arr[i][j] returns 111111
how can I access 1? is this the right answer to the question on hackerrank?
Hope below code helps you
public class Solution {
private static final int _MAX = 6; // size of matrix
private static final int _OFFSET = 2; // hourglass width
private static int matrix[][] = new int[_MAX][_MAX];
private static int maxHourglass = -63; // initialize to lowest possible sum (-9 x 7)
/** Given a starting index for an hourglass, sets maxHourglass
* #param i row
* #param j column
**/
private static void hourglass(int i, int j){
int tmp = 0; // current hourglass sum
// sum top 3 and bottom 3 elements
for(int k = j; k <= j + _OFFSET; k++){
tmp += matrix[i][k];
tmp += matrix[i + _OFFSET][k];
}
// sum middle element
tmp += matrix[i + 1][j + 1];
if(maxHourglass < tmp){
maxHourglass = tmp;
}
}
public static void main(String[] args) {
// read inputs
Scanner scan = new Scanner(System.in);
for(int i=0; i < _MAX; i++){
for(int j=0; j < _MAX; j++){
matrix[i][j] = scan.nextInt();
}
}
scan.close();
// find maximum hourglass
for(int i=0; i < _MAX - _OFFSET; i++){
for(int j=0; j < _MAX - _OFFSET; j++){
hourglass(i, j);
}
}
// print maximum hourglass
System.out.println(maxHourglass);
}
}
I'm currently trying to implement the Diamond Square algorithm and while the thing is somewhat working, I'm confused by the HeightMap.
Here it is :
As you can see the squares are clearly outlined by brighters values, while the losanges are outlined by darker values.
I really cant understand why. I know that the size of the map is really small but I don't think that's the expected result anyway, and I got the same behavior with larger sizes.
Here's my code :
public class TerrainBuilder
{
private Terrain Terrain = null;
private Random r = new Random();
private int espace;
public void Init(Terrain _terrain)
{
Terrain = _terrain;
espace = Terrain.SIZE - 1;
}
public void DiamondAlgoritm()
{
if (Terrain == null)
return;
//Initialization
Terrain.HeightMap[0, 0] = r.Next() % 255;
Terrain.HeightMap[0, Terrain.SIZE - 1] = r.Next() % 255;
Terrain.HeightMap[Terrain.SIZE - 1, 0] = r.Next() % 255;
Terrain.HeightMap[Terrain.SIZE - 1, Terrain.SIZE - 1] = r.Next() % 255;
//randominess
int decalage = 128;
while (espace > 1)
{
int demiSpace = espace / 2;
//diamond phase
for (int i = demiSpace; i < espace; i = i + espace)
{
for (int j = demiSpace; j < espace; j = j + espace)
{
var avg = Terrain.HeightMap[i + demiSpace, j + demiSpace] + Terrain.HeightMap[i + demiSpace, j - demiSpace] + Terrain.HeightMap[i - demiSpace, j + demiSpace] + Terrain.HeightMap[i - demiSpace, j - demiSpace];
avg /= 4;
Terrain.HeightMap[i, j] = Normalize(avg + r.Next() % decalage);
}
}
//carre phase
for (int i = 0; i < Terrain.SIZE; i += demiSpace)
{
int delay = 0;
if (i % espace == 0)
delay = demiSpace;
for (int j = delay; j < Terrain.SIZE; j += espace)
{
double somme = 0;
int n = 0;
if (i >= demiSpace)
somme = somme + Terrain.HeightMap[i - demiSpace, j];
n = n + 1;
if (i + demiSpace < Terrain.SIZE)
somme = somme + Terrain.HeightMap[i + demiSpace, j];
n = n + 1;
if (j >= demiSpace)
somme = somme + Terrain.HeightMap[i, j - demiSpace];
n = n + 1;
if (j + demiSpace < Terrain.SIZE)
somme = somme + Terrain.HeightMap[i, j + demiSpace];
n = n + 1;
Terrain.HeightMap[i, j] = Normalize(somme / n + r.Next() % decalage);
}
}
espace = demiSpace;
}
}
private double Normalize(double value)
{
return Math.Max(Math.Min(value, 255), 0);
}
}
I would like some help to understand this problem.
During the diamond phase, you don't iterate over the whole map. You only calculate the first square (equal to espace).
Change your loop termination conditions like this:
for (int i = demiSpace; i < Terrain.SIZE; i = i + espace)
{
for (int j = demiSpace; j < Terrain.SIZE; j = j + espace)
{
var avg = Terrain.HeightMap[i + demiSpace, j + demiSpace] +
Terrain.HeightMap[i + demiSpace, j - demiSpace] +
Terrain.HeightMap[i - demiSpace, j + demiSpace] +
Terrain.HeightMap[i - demiSpace, j - demiSpace];
avg /= 4;
Terrain.HeightMap[i, j] = Normalize(avg + r.Next() % decalage);
}
}
Also, you never adjust your randomness variable (decalage), which should get smaller as you reduce the size of espace.
hi everyone i have an c# windows phone 8.1 app i have a result text box, i want to search in the result text box for text that will be print on him after a for loop start that's mean if my result TextBox have the value already i don't want him to print it again and if not print it my app calculate the pythagorean number i don't know how to do it plz help my faild try is :
if (from.Text == "0")
start = start + 1;
for (int i = start; i <= end; i++) {
for (int j = start+1; j <= end; j++) {
for (int k = start; k <= end; k++) {
if (i * i + j * j == k*k) {
string now = j + "+" + i ;
result.FindName(now);
result.Text += Environment.NewLine + (i + " + " + j + " = " + k) + Environment.NewLine;
c++;
}
}
}
}
i search in google for it but it look like there's no answer ! plz help me !!!!
the real answer is this:
result.Text.Contains(someString)
I am trying to use a bubble sort, to order numbers from least to greatest in c#, whats wrong with what i have so far?
private void Order_Click(object sender, EventArgs e) {
value1 = Convert.ToInt32(textBox1.Text);
value2 = Convert.ToInt32(textBox2.Text);
value3 = Convert.ToInt32(textBox3.Text);
value4 = Convert.ToInt32(textBox4.Text);
value5 = Convert.ToInt32(textBox5.Text);
int[] myarray = { value1, value2, value3, value4, value5 };
int n = 0;
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++)
{
if (myarray[i] > myarray[i + 1]) {
tmp = myarray[i];
myarray[i] = myarray[i + 1];
myarray[i + 1] = tmp;
swapped = true;
order1.Text = Convert.ToString(myarray[0] + "," +
myarray[1] + "," +
myarray[2] + "," +
myarray[3] + "," +
myarray[4]);
}
}
}
}
First thing which is wrong with your code is:
for (int i = 0; i < n - j; i++)
Your check i < n - j will never let the control to fall through the loop. Because you have initialized n to 0 and j is 1 (after j++), so i is not going to be less then -1, Thus your loop will not work as intended. Since you set the swapped to false, the control will fall out of the while loop, hence no sorting.
You need to change your code to:
while (swapped)
{
swapped = false;
for (int i = 0; i < myarray.Length - 1; i++)
{
if (myarray[i] > myarray[i + 1])
{
int t = myarray[i];
myarray[i] = myarray[i + 1];
myarray[i + 1] = t;
swapped = true;
Console.WriteLine(Convert.ToString(myarray[0] + "," + myarray[1] + "," + myarray[2] + "," + myarray[3] + "," + myarray[4]));
}
}
}
the n and j variables seem unnessesary. leave n and j out of your program, and in your for loop, do this:
for (int i = 0; i < (array1.Length - 1); i++)
Bubble Sort
using System;
class AscendingBubbleSort
{
public static void Main()
{
int i = 0,j = 0,t = 0;
int []c=new int[20];
for(i=0;i<20;i++)
{
Console.WriteLine("Enter Value p[{0}]:", i);
c[i]=int.Parse(Console.ReadLine());
}
// Sorting: Bubble Sort
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
if(c[i]>c[j])
{
Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]);
t=c[i];
c[i]=c[j];
c[j]=t;
}
}
}
Console.WriteLine("Here comes the sorted array:");
// Print the contents of the sorted array
for(i=0;i<20;i++)
{
Console.WriteLine ("c[{0}]={1}", i, c[i]);
}
}
}
source
Is it possible to get the current index while looping?
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = DGV.CurrentRow.Index + " " + DGV.Rows[i].Cells[1].Value.ToString();
}
But I have in output :
0 First
0 Second
0 ...
I want to get :
1 First
2 Second
3 ...
Thanks.
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = (i + 1).ToString() + " " + DGV.Rows[i].Cells[1].Value.ToString();
}
BTW: I'd prefer:
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = String.Format("{0} {1}", i + 1, DGV.Rows[i].Cells[1].Value);
}
It's the index just i+1 in this case?
Try:
myValue = (i+1) + " " + DGV.Rows[i].Cells[1].Value.ToString();
you can do this way also
for (int i = 1; i < DGV.Rows.Count - 1; i++)
{
myValue = i.ToString() + " " + DGV.Rows[i-1].Cells[1].Value.ToString();
}