How to find value in specific index of Multidimensional array - c#

I have a multidimensional array that represents the days of the week (Monday, Tuesday etc..) and weeks for a month (Week 1 etc..), which shows the amount of products made on each day. The array is being filled by user entry (Input box) and I am trying to figure out a way to prompt the user for a week: Week 1 etc and a day. I know of IndexOf but that gives me the index not the value in the index.
My code is:
int[,] week = new int[4, 5];
for (int Row = 0; Row < week.GetLength(0); Row++)
{
if (Row == 0)
{
Week = "Week 1 ";
}
else if (Row == 1)
{
Week = "Week 2 ";
}
else if (Row == 2)
{
Week = "Week 3 ";
}
else if (Row == 3)
{
Week = "Week 4 ";
}
txtOutput.Text += "\r\n" + Week + ": ";
for (int Col = 0; Col < week.GetLength(1); Col++)
{
if (Col == 0)
{
Day = "Monday";
}
else if (Col == 1)
{
Day = "Tuesday";
}
else if (Col == 2)
{
Day = "Wednesday";
}
else if (Col == 3)
{
Day = "Thursday";
}
else if (Col == 4)
{
Day = "Friday";
}
string value = Microsoft.VisualBasic.Interaction.InputBox("Enter the amount of products made on " + Day + " for " + Week, "Product Amount");
week[Row, Col] = Int32.Parse(value);
txtOutput.Text += " ";
txtOutput.Text += value + " ";
}
}
I will probably have to make some custom method for this as there is no inbuilt method I know of but I cannot figure out what this would look like. Any help would be appreciated.

week[Row, Col] is just the answer.
e.g. week[0,3], week[2,4]...

Related

In a Baccarat Road Map Simulation, how do I count every row and column of the multidemensional array?

I have a multidimensional array which is declared like this
string[,] table = new string[104,15];
Now it has all the data that I need and I got the data to put it on the table by doing this
int xIndex = 0;
int yIndex = 0;
string newPreviousValue = "placeholder";
for (int i = 0; i < list.Count; i++)
{
newString[0] += list[i].r;
newString[0] += ",";
}
string[] newChars = newString[0].Split(',');
foreach (string previousValue in newChars)
{
table[xIndex, yIndex] = result;
}
Now what I am trying to do is from this Data A
All these value of Data B is depending on what value of Data A has
RED means "The same in length"
BLUE means "Not the same in length"
So here how it works .
PSEUDO CODE
//I am talking about Data A
if table[1,1] is equal to null or T then
{
if table[1,0] and table[0,0] has the same length
//Display blue circle on Data B
display a blue circle
}
//I am talking about Data A
else if table[1,1] is not equal to null then
{
compare table[0,1] and table[0,0] if they have the same length as table[1,1] and table[1,0]
//Display a red circle
display a red circle }
}
If I am not clear here's the rule . What I am talking about is the Big Eye Road Rules.
What I have tried so far is this
//lets check for the 2nd row and 2nd column of the big road table
if (table[1,1] == null && table[2,0] != null)
{
Move = true;
if (Move)
{
//lets move to the 3rd row and compare if they have the same in length (1st row and 1st column)
if (table[0, 0] != null && table[1, 0] != null)
{
//red circle
}
else
{
//blue circle
}
}
}
What I want to achieve here is to count every row and column in DATA A if how many data's that are stored there for example
table[0,0] to table[0,6] has 1 data
table[1,0] to table[1,6] has 1 data
table[2,0] to table[2,6] has 2 data
Just like that . Could someone help with it please. Thank you.
EDIT
On the last part of my question I will share what I did
//COLUMN
for(int col = 0; col < table.GetLength(0); col++)
{
int sum = 0;
//ROW
for (int row = 0; row < table.GetLength(1); row++)
{
if (table[col,row] != null)
{
sum++;
}
}
Debug.Log("table column: " + col + " has " + sum + " data");
}
I can now get this
table[0,0] to table[0,6] has 1 data
table[1,0] to table[1,6] has 1 data
table[2,0] to table[2,6] has 2 data
for(int col = 0; col < table.GetLength(0); col++)
{
int sum = 0;
//ROW
for (int row = 0; row < table.GetLength(1); row++)
{
if (table[col,row] != null)
{
sum++;
}
}
Debug.Log("table column: " + col + " has " + sum + " data");
}

How can i skip each 9 items when adding to a List?

public void ImagesLinks()
{
int count = 0;
foreach(string c in DatesAndTimes)
{
if (count == 9)
{
count = 0;
}
string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true";
imagesUrls.Add(imageUrl);
count++;
}
}
The List DatesAndTimes is in this format: Each line is item in the List.
201612281150201612281150
201612281150201612281151
201612281150201612281152
201612281150201612281153
201612281150201612281154
201612281150201612281155
201612281150201612281156
201612281150201612281157
201612281150201612281158
201612281150201612281159
Europe
201612281150201612281150
201612281150201612281151
201612281150201612281152
201612281150201612281153
201612281150201612281154
201612281150201612281155
201612281150201612281156
201612281150201612281157
201612281150201612281158
201612281150201612281159
Turkey
Every 10 items there is a name.
I want to skip the name when i build the links.
So i count to 9 each time ( count 10 times from 0 to 9 ) then i reset the counter to 0 and i also want to skip the name for example Europe then count to 10 again and skip Turkey and so on.
You can also use (count % 9 == 0), this way you don't have to reset the counter:
public void ImagesLinks()
{
int count = 0;
foreach(string c in DatesAndTimes)
{
if (count++ % 9 == 0)
continue;
string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true";
imagesUrls.Add(imageUrl);
}
}
Use continue in the if statement. It will skip the line after and resume the foreach loop execution.
public void ImagesLinks()
{
int count = 0;
foreach(string c in DatesAndTimes)
{
if (count == 9)
{
count = 0;
continue;
}
string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true";
imagesUrls.Add(imageUrl);
count++;
}
}

Required a spacing while merging two identical column values

I have a requirement to merge 2 column values - It's working good. But, there is no space between 2 values. Below is my code. Please let me know where i have to correct:
DataGridViewTextBoxColumn dgvctime = new DataGridViewTextBoxColumn();
dgvctime.Name = cmbColumn1.Text;
dgvctime.HeaderText = cmbColumn1.Text;
dataGridView1.Columns.Add(dgvctime);
//dataGridView1.Rows.Add(dgvctime);
int rowadded = 0;
for (int RowCount = 1; RowCount <= strfile.Length - 1; RowCount++)
{
if (strfile[RowCount].ToString() != "")
{
if (RowCount != 0)
{
//dataGridView1.Rows.Add();
string[] column = strfile[RowCount].Split('รพ');
rowadded = 0;
for (int i = 1; i < column.Length - 1; i++)
{
//dataGridView1.Rows.Add();
if ((cmbColumn1.SelectedIndex == ((i - 1) / 2)) || (cmbColumn2.SelectedIndex == ((i - 1) / 2)))
{
if (rowadded == 0)
{
rowadded = 1;
dataGridView1.Rows.Add();
}
if (column[i].ToString() != "\u0014")
{
//dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += column[i].ToString();
dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += column[i].ToString();
` }
}
}
}
}
Change this line ...
dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += " " + column[i].ToString();
Just add a short update
dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += " " + column[i].ToString();

Implementing a string based calendar in C#

I want to create a C# program that displays a calendar and uses a string that contains the names of the days:
string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
So far I have....
int weeks = 1;
int days = 1;
string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
string dayName;
int commaIndex = 0;
int date = 1;
while (weeks < 5)
{
while (days < 8)
{
commaIndex = Names.IndexOf(","); // find the period
dayName = Names.Remove(commaIndex);
lblCalendar.Text += dayName + "." + " " + date + " ";
Names = Names.Remove(0, commaIndex + 1);
days++;
date++;
}
weeks++;
}
But that only writes the first week.. can anybody help me figure out where there is an error?
First, you forgot to restart days variable after every iteration.
Also, after first iteration your Names string is empty.
I suggest to create array of day names and use it instead of one string.
int weeks = 1;
int days = 1;
var Names = new[] {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
string dayName;
int commaIndex = 0;
int date = 1;
while (weeks < 5)
{
while (days < 8)
{
dayName = Names[days-1];
lblCalendar.Text += dayName + "." + " " + date + " ";
days++;
date++;
}
days = 1;
weeks++;
}
You forgot to reset days.
while (weeks < 5)
{
days = 1
string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
while (days < 8)
{

Fizz buzz program wierd output?

Why does this result in 1 2 Fizz 3 ? Shouldn't it result in 1 2 Fizz?! I'am a little confused with the output of this loop..? Adding else if removes this problem,my question is why?
for (int i = 1; i <= value; i++)
{
if (i % 3 == 0)
{
ViewBag.Output += "Fizz ";
}
if (i % 5 == 0)
{
ViewBag.Output += "Buzz ";
}
else
{
ViewBag.Output += i.ToString() + " ";
}
}
You are not skipping iteration if first if matches. Thus you have else part of i % 5 if block evaluated when current i is not divided by 5:
for (int i = 1; i <= value; i++)
{
if (i % 3 == 0)
ViewBag.Output += "Fizz ";
if (i % 5 == 0)
ViewBag.Output += "Buzz ";
if ((i % 3 != 0) && (i % 5 != 0))
ViewBag.Output += i.ToString() + " ";
}
Alternative solution which uses common else block as you have it now:
for (int i = 1; i <= value; i++)
{
if (i % 3 == 0)
{
ViewBag.Output += "Fizz ";
if (i % 5 == 0)
ViewBag.Output += "Buzz ";
}
else if (i % 5 == 0)
{
ViewBag.Output += "Buzz "
}
else // neither divided by 3 nor by 5
{
ViewBag.Output += i.ToString() + " ";
}
}
And one more solution with dictionary, to avoid all this additional if...else checks:
var valuesToCheck = new Dictionary<int, string> {
{ 3, "Fizz" },
{ 5, "Buzz" }
};
for (int i = 1; i <= value; i++)
{
bool divisorFound = false;
foreach(var kvp in valuesToCheck)
{
if (i % kvp.Key == 0)
{
divisorFound = true;
ViewBag.Output += kvp.Value + " ";
}
}
if (!divisorFound)
ViewBag.Output += i + " ";
}
It is because your second if is evaluated, no matter the result of the first if.
So when i == 3, the first if evaluates to true and the second to false, and therefore the second if's else block is executed.
The second if is executed, regardless of the preceeding if so, the output you experience should be expected.
Here's a simple solution to the FizzBuzz problem lifted from a #KonradRudolph answer to a previous question
for (int i = 1; i <= value; i++)
ViewBag.Output +=
i % 15 == 0 ? "FizzBuzz " :
i % 3 == 0 ? "Fizz " :
i % 5 == 0 ? "Buzz " :
i.ToString() + " ";
or, alternatively.
string FizzBuzz(int value, int a = 3, int b = 5)
{
if value % (a * b) == 0 return "FizzBuzz";
if value % a == 0 return "Fizz";
if value % b == 0 return "Buzz";
return value.ToString();
}
used with,
viewBag.Output = string.Join(" ", Enumerable.Range(1, value).Select(FizzBuzz));
or, going beyond reason
IEnumerable<string> Checker<T>(
this IEnumerable<T> source,
params KeyValuePair<Predicate<T>, string>>[] checks)
(
var found = false;
foreach(var t int source)
{
var result =
string.Concat(checks.Where(c => c.Key(t)).Select(c => c.Value));
if (result.IsNullOrEmpty())
{
yield return t.ToString();
continue;
}
yield return result;
}
)
which could be used like this,
ViewBag.Output = string.Join(" ", Enumerable.Range(1, value).Checker(
new KeyValuePair<Predicate<T>, string>>(i => i % 3 == 0, "Fizz"),
new KeyValuePair<Predicate<T>, string>>(i => i % 5 == 0, "Buzz"));

Categories

Resources