C# Array, String, error - c#

int i;
int [,] Prices = new int [2, 7]{{1,2,3,4,5,6,7},{700,600,500,400,300,200,100}};
string[,] City = new string [2,1]{{"A"},{"B"}};
bool found = false;
for (i = 0; i <= City.Length -1; i++)
// for (y = 0; y <= City.Length - 1; y++)
{
if (LstDestinationCity.Text == City[i]) <<-- i get error here
{
im planing to do a program that if i select A city i get first row if B city i get 2 row

I think that's because City[i] "don't contain anything" you should check City[i,0]
if (LstDestinationCity.Text == City[i,0])// this should access the first element which is the text you are looking for

I would rather do it as
if (LstDestinationCity.Text == City[i,i])
{
// ...
}

Your City variable does not need to be a two dimensional array. If you change it to a one dimensional array you can access the values with one index instead of 2.
string[] City = new string [2]{"A","B"};

Related

I need to find the max value in a array with C#

I have a multidimensional array with five countries and some medals. The last column is the total and i need to return the country with most medals. My code is "working" as long as there are no two countries with the same number of medals and those being the max number.
Problably i just don't have enough experience with arrays, but how can i return more than one contry, if i need?
string [,] medals = new string[5, 5];
Random rdn = new Random();
medals[0, 0] = "Brazil";
medals[1, 0] = "USA";
medals[2, 0] = "Russia";
medals[3, 0] = "France";
medals[4, 0] = "Egypt";
for (int i = 0; i < medals.GetLength(0); i++)
for (int j = 1; j < medals.GetLength(1); j++)
{
//This fills the gold, silver and copper medals
if (j < medals.GetLength(1)-1)
medals[i, j] = rdn.Next(1, 6).ToString();
else
//This fills the total medals
medals[i, j] = (int.Parse(medals[i, 1]) + int.Parse(medals[i,2]) + int.Parse(medals[i,3])).ToString();
}
//The first index stores the max number so far and the second stores the name of the country
string [] winner = { "0", "0" };
for (int i = 0; i < medals.GetLength(0); i++)
{
if (int.Parse(medals[i,4]) > int.Parse(winner[0]))
{
winner[0] = medals[i, 4];
winner[1] = medals[i, 0];
}
}
You could use a list to store the winners.
var listWinners = new List<KeyValuePair<string,string>>();
for (int i = 0; i < medals.GetLength(0); i++)
{
if ((listWinners.Count() == 0)||(int.Parse(medals[i,4]) > int.Parse(listWinners.First().Value)))
{
listWinners.Clear();
listWinners.Add(new KeyValuePair<string,string>(medals[i,0],medals[i,4]));
}
else if (int.Parse(medals[i,4]) == int.Parse(listWinners.First().Value))
{
listWinners.Add(new KeyValuePair<string,string>(medals[i,0],medals[i,4]));
}
}
But I would like reiterate what has been said in the comments, it would be much better if you could use a List of Object rather than multi-dimentional array. It would increase your readability of code and make things much simpler.
A dictionary would be a much easier starting place. You could then just sort based on the values in the dict and use a simple loop to check for duplicate values.
There are a lot of things I would do differently than you, but that's besides the point (see note at the end). Your issue is how to deal with the situation where you have multiple countries with equal and max number of medals.
How to return multiple countries?
Well, that's easy: imagine you return the winner from the below function:
string[] GetWinner() {
// your code
return new[] { "20", "USA" };
}
Then there is no change in the data type, simply add another country to the array and the calling code should check how many you have
string[] GetWinner() {
// your code
return new[] { "20", "USA", "CAN" };
}
Now if you have a problem as to how to create an array whose size is unknown while creating it, then the usual solution is to use List<>, which in this case will act as a re-sizable array for you.
The final code would look something like below
static string[] GetWinner(string[,] medals) {
int rows = medals.GetLength(0);
var winners = new List<string>();
int maxMedals = 0;
for (var i = 0; i < rows; i++) {
var n = int.Parse(medals[i, 4]);
maxMedals = n > maxMedals ? n : maxMedals;
}
for (var i = 0; i < rows; i++) {
var n = int.Parse(medals[i, 4]);
if (n == maxMedals)
winners.Add(medals[i, 0]);
}
winners.Insert(0, maxMedals.ToString());
return winners.ToArray();
}
Now, I would advise you to use a better data structure to store your medals. But since I don't know the full situation, I would guess, something like below is reasonable. Instead of using named types, you can use ValueTuple - to make the code concise, if the code related to these objects are restricted to a small area of a the code file. This should avoid allocating big chunk of memory associated with 2D arrays, and also storing of int in string.
public class CountryMedalTally {
public string Country { get; set; }
public string[] Medals { get; set; }
public int MedalCount { get; set; }
}
public class Winner {
public List<string> Country { get; set; }
public int MedalCount { get; set; }
}
List<CountryMedalTally> AllMedals;

listboxes passing values from each other

I'm trying to multiply the values of 2 listboxes together and make their product appear at another list box I'm getting the results I need but the problem is when I rerun the loop using a command button the listbox removes the next instance of the first value calculated by ppc[i] * qty[i] but when I try to remove the the listBox4.Items.Remove(ppc[i] * qty[i]) it reprints the whole array again from first element to last element
string myString = textBox1.Text.ToString();
int index = listBox6.FindString(myString, -1);
int[] qty = new int[99];
int[] ppc = new int[99];
int[] gt1 = new int[99];
listBox3.Items.Add(listBox5.Items[index]);
listBox1.Items.Add(textBox2.Text.ToString());
if (index != -1)
{
listBox6.SetSelected(index, true);
listBox2.Items.Add(textBox1.Text); //name
}
listBox3.Items.Add(listBox5.Items[index]);
listBox3.Items.Remove(listBox5.Items[index]);
for (int i = 0; i != listBox2.Items.Count ; i++)
{
ppc[i] = Convert.ToInt32(listBox3.Items[i]);
qty[i] = Convert.ToInt32(listBox1.Items[i]);
listBox4.Items.Remove(ppc[i] * qty[i]);
listBox4.Items.Add((ppc[i] * qty[i]));
}
My understanding is that this loop works once, and then when it is re-run it is out of order. Are you making sure to clear listbox4 each time this loop is executed? Also since listBox2 isn't used, it is probably better not to use it for your loop bounds.
if(listBox1.Items.Count == listBox3.Items.Count)
{
int rowCount = listBox1.Items.Count;
listBox4.Items.Clear();
for (int i=0; i < rowCount; i++)
{
ppc[i] = Convert.ToInt32(listBox3.Items[i]);
qty[i] = Convert.ToInt32(listBox1.Items[i]);
listBox4.Items.Insert(i , (ppc[i] * qty[i]));
}
}

Write list to csv file c#

Imagine i have 2 lists filled with values. I want all the elements from the first list, written into the first column, all the elements from the second list written into the second column and so on.
If both list have the same size, this works fine:
for (int i = 0; i < valueArray.Count(); i++)
{
var newLine = string.Format("{0},{1}", valueArray.ElementAt(i), secondValueArray.ElementAt(i));
sw.Write(newLine);
}
My problem is that if the lists have different sizes, code fails with out of range exception obviously. I tried adding ',' between columns but it's not working.
Instead of ElementAt you should use ElementAtOrDefault :
According to msdn it
Returns the element at a specified index in a sequence or a default
value if the index is out of range.
try this :
List<int?> valueArray = new List<int?>();
List<int?> secondValueArray = new List<int?>();
//... fill lists
valueArray.Add( 1 );
valueArray.Add(2);
valueArray.Add(3);
secondValueArray.Add( 4 );
while (valueArray.Count > secondValueArray.Count)
secondValueArray.Add(null);
while (secondValueArray.Count > valueArray.Count)
valueArray.Add(null);
for (int i = 0; i < valueArray.Count(); i++)
{
var newLine = string.Format("{0},{1}", valueArray.ElementAt(i), secondValueArray.ElementAt(i));
Console.WriteLine(newLine);
}
;
Result :
1,4
2,
3,
As mentioned before, use ElementAtOrDefault(). And check which array is the longest one. Furthermore, you might want to write an empty string instead of NULL if there's no value.
int count = Math.Max(firstArray.Count(), secondArray.Count());
for (int i = 0; i < count; i++)
{
var value1 = firstArray.ElementAtOrDefault(i) ?? String.Empty;
var value2 = secondArray.ElementAtOrDefault(i) ?? String.Empty;
var newLine = string.Format("{0},{1}", value1, value2);
sw.Write(newLine);
}

Returning -1 as value of variable;

When I try run the aplication, it shows the Index was outside the bounds of the array at line float[] u_f = a[userid]; when I check the value of userid, it is -1;
Any idea?
PS. the user ID can be every integer,but I take the index of the integer with is between(0, 1143600 for item) and (0, 89395 for user) and my calculation is based on that.Then, my calculation is based on the index of userid value which is stored in array a not based on the value of userid.
Thanks in advance.
float[][] a = Enumerable.Range(0, 89395).Select(i => new float[100]).ToArray();
float[][] b = Enumerable.Range(0, 1143600).Select(j => new float[100]).ToArray();
int[] c = new int[1258038];
int[] d = new int [92160];
........
public float dotproduct(int userid, int itemid)
{
result = 0f;
float[] u_f = a[userid]; // <----Error Line (index was outside the bounds of array-The value of user id is -1)
float[] i_f = b[itemid];
for (int i = 0; i < u_f.Length; i++)
{
result += u_f[i] * i_f[i];
}
return result;
}
private void btn_recomm_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtbx_id.Text) && String.IsNullOrEmpty(txtbx_itemid.Text) && !String.IsNullOrEmpty(txtbx_noofrecomm.Text))
{
int sc = Convert.ToInt32(txtbx_id.Text);
int n = Convert.ToInt32(txtbx_noofrecomm.Text);
int userseq=Array.IndexOf(d, sc);
var results = new List<float>(1143600);
for (int z = 0; z <= 1143600; z++)
{
results.Add(dotproduct(userseq, z));
}
var sb1 = new StringBuilder();
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
{
sb1.AppendFormat("{0}: {1}", d[resultwithindex.Index], resultwithindex.result);
sb1.AppendLine();
}
MessageBox.Show(sb1.ToString());
}
if (!String.IsNullOrEmpty(txtbx_id.Text) && !String.IsNullOrEmpty(txtbx_itemid.Text) && String.IsNullOrEmpty(txtbx_noofrecomm.Text))
{
int uid = Convert.ToInt32(txtbx_id.Text);
int iid = Convert.ToInt32(txtbx_itemid.Text);
int userseq0 = Array.IndexOf(d, uid);
int itemseq0 = Array.IndexOf(c, iid);
dotproduct(userseq0, itemseq0);
MessageBox.Show("The Score of item id " + itemseq0 + " is " + result);
}
Your problem is this line:
int userseq=Array.IndexOf(d, sc);
is presumably returning -1 which you are then passing into your dotproduct function which is then causing it to fail. You need to decide on what logic to apply in the case that you can't find sc in d and implement that.
Though as others have said validating your input to dotproduct would help you find problems a bit more easily.
I am not sure what you are asking. You have an error because you are try to access to the index -1 of a. An array begin at 0. The value -1 does not exist. userid should be between 0 and a.Length.
Is your method supposed to accept a userid value that's inferior to zero? If so then you cannot use that to define an array's length. Period.
If -1 is not a valid value, I would add this right on the first line of your dotproduct method, since it's public:
if (userid < 0) throw new ArgumentOutOfRangeException("userid");
As for why it's -1 in the current context, as others pointed out if the text value you try to find in the d array isn't there, the IndexOf method will return -1. You will have to validate the user's input before calling in your method.
I will also say what I already told you in your other post: name your variables in a clear way and - if that makes sense in your scenario - use custom classes rather than jagged arrays. Your code will be far easier to read and maintain afterwards.
int userseq=Array.IndexOf(d, sc);
it returns -1 if item sc is not present in array d. Then you pass userseq variable as first argument of a function dotproduct and take array index by it's first argument (userid). So here is your problem - in the IndexOf.
I don't know how to fix it - it's your buiseness logic and you must decide how to handle the case when input data isn't present in array. Maybe show message to user immediately.

loop though a array then if and then output result?

i have a little problem, in the code below (C#) it loops thought the arrays, it then check if the user_id has a user_post greater than 50, it then write the user_id, the expected outcome is
12
13
but the actual output is
12
12
12
whats wrong with the code? I tried a standard for loop but could not get it right?
int[] user_id = new int[64];
int[] group_id = new int[64];
int[] user_post = new int[64];
//user 55
user_id[0] = 10;
group_id[0] = 8;
user_post[0] = 4;
//user56
user_id[1] = 11;
group_id[1] = 2;
user_post[1] = 15;
//user57
user_id[2] = 12;
group_id[2] = 2;
user_post[2] = 55;
//user58
user_id[3] = 13;
group_id[3] = 2;
user_post[3] = 56;
foreach (int i in group_id)
{
if (group_id[i] == 2)
if (user_post[i] > 50)
Console.WriteLine(Convert.ToString(user_id[i]));
}
Console.WriteLine("Press any key too continue...");
Console.ReadLine();
// continue...
Because you have an if statement that only checks for 2
if (group_id[i] == 2)
where as "i" is not a counter instead the element from the foreach loop.
and items at 2 & 3rd position have 2 group id so it alway ends like this:
if (group_id[8] == 2) //false
if (group_id[2] == 2) //true
if (group_id[2] == 2) //true
Instead of that vague code you should have your loop as this:
for(int i = 0 ; i< 64 ; i++)
{
if (group_id[i] == 2)
{
if (user_post[i] > 50)
Console.WriteLine(Convert.ToString(user_id[i]));
}
}
in your foreach statement, i takes on the values stored in your group_id array - 8, 2, 2, 2
In your if and output statements, your are using this value as an index into the arrays
So, your if statements end up doing this:
if(group_id[8])...
if(group_id[2])...
if(group_id[2])...
if(group_id[2])...
You are only examining elements 8 and 2 in your arrays.
Use a for loop to iterate through the array indexes
The for syntax is as follows:
for (int i = 0; // incremental variable
i < 100; // determining a limit
++i) // increment the variable
While foreach works like this:
foreach (var element // the element itself, not an index
in
elementCollection) // iterating through the collection
You're looping around the wrong array with the for each statement. You should loop using the regular for statement instead like this:
for (int i = 0;i < user_post.Length; i++)
{
if (user_post[i] > 50 && group_id[i] == 2)
{
Console.WriteLine(Convert.ToString(user_id[i]));
}
}
foreach (int i in group_id)
is wrong, you must use:
for(int i = 0; i < group_id.Length; i++)
because with the former you're using the values in group_id as indexes of your arrays.
BTW, I'd suggest you to create a class e.g. Info like:
class Info
{
public int GroupId {get; set;};
public int UserId {get; set;};
public int PostId {get; set;}
}
this would allow you to create one array only (i.e. Info[]) and avoid possible errors due to different lenghts of the 3 arrays...

Categories