| | |
binary search help
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
I cannot get my array to convert to int. I tried some ways but always ran into errors. Here is my program as it stands now. Can anyone give me some pointers???
This is the errors I am getting:
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(49,33): error CS0019: Operator '<' cannot be applied to operands of type 'int' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(53,38): error CS0019: Operator '>' cannot be applied to operands of type 'int' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(57,55): error CS0029: Cannot implicitly convert type 'string[]' to 'int'
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.IO; namespace sortingarray { class Program { static void Main(string[] args) { String phoneareacode, state, line; AreaCode myAreaCode; bool isNum = false; try { string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv"); string[][] unsortedarray = new string[strings.Length][]; for (int i = 0; i < strings.Length; i++) { unsortedarray[i] = strings[i].Split(','); } Array.Sort(unsortedarray, AreaCode.sortAreaCode()); for (int i = 0; i < unsortedarray.Length; i++) { phoneareacode = unsortedarray[i][0]; state = unsortedarray[i][1]; myAreaCode = new AreaCode(phoneareacode, state); } int searchAreaCode; string searchString = Console.ReadLine(); isNum = Int32.TryParse(searchString, out searchAreaCode); if (isNum) { int lowNum = 0; int highNum = unsortedarray.Length - 1; while (lowNum <= highNum) { int midNum = (lowNum + highNum) / 2; if (searchAreaCode < unsortedarray[midNum]) { highNum = midNum - 1; } else if (searchAreaCode > unsortedarray[midNum]) { lowNum = midNum + 1; } else if (searchAreaCode = unsortedarray[midNum]) { Console.WriteLine(" The state for Area Code {0} is: ", searchAreaCode); return; } } Console.WriteLine("Please enter a 3 digit area code!"); //Console.WriteLine(myAreaCode); //Console.WriteLine(); //Console.ReadKey(true); } } catch (IOException e) { Console.WriteLine(e); } } } }
This is the errors I am getting:
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(49,33): error CS0019: Operator '<' cannot be applied to operands of type 'int' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(53,38): error CS0019: Operator '>' cannot be applied to operands of type 'int' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(57,55): error CS0029: Cannot implicitly convert type 'string[]' to 'int'
0
#2 Oct 24th, 2009
The 2 errors have the same cause. You are trying to compare a string with an int. You can compare strings with strings or ints with ints. The last error tells you, you are trying to assign a string to an int.BTW on line 57: you cannot use assign(=) here, you must use equal(==)
You could compare with searchstring, not with SearchAreaCode.
You could compare with searchstring, not with SearchAreaCode.
Last edited by ddanbe; Oct 25th, 2009 at 12:05 am.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
0
#3 Oct 25th, 2009
•
•
•
•
The 2 errors have the same cause. You are trying to compare a string with an int. You can compare strings with strings or ints with ints. The last error tells you, you are trying to assign a string to an int.BTW on line 57: you cannot use assign(=) here, you must use equal(==)
You could compare with searchstring, not with SearchAreaCode.
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
0
#5 Oct 25th, 2009
Yea, you are right. Here is the updated code:
The errrors:
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(49,33): error CS0019: Operator '<' cannot be applied to operands of type 'string' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(53,38): error CS0019: Operator '>' cannot be applied to operands of type 'string' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(57,38): error CS0019: Operator '==' cannot be applied to operands of type 'int' and 'string[]'
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.IO; namespace sortingarray { class Program { static void Main(string[] args) { String phoneareacode, state, line; AreaCode myAreaCode; bool isNum = false; try { string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv"); string[][] unsortedarray = new string[strings.Length][]; for (int i = 0; i < strings.Length; i++) { unsortedarray[i] = strings[i].Split(','); } Array.Sort(unsortedarray, AreaCode.sortAreaCode()); for (int i = 0; i < unsortedarray.Length; i++) { phoneareacode = unsortedarray[i][0]; state = unsortedarray[i][1]; myAreaCode = new AreaCode(phoneareacode, state); } int searchAreaCode; string searchString = Console.ReadLine(); isNum = Int32.TryParse(searchString, out searchAreaCode); if (isNum) { int lowNum = 0; int highNum = unsortedarray.Length - 1; while (lowNum <= highNum) { int midNum = (lowNum + highNum) / 2; if (searchString < unsortedarray[midNum]) { highNum = midNum - 1; } else if (searchString > unsortedarray[midNum]) { lowNum = midNum + 1; } else if (searchAreaCode == unsortedarray[midNum]) { Console.WriteLine(" The state for Area Code {0} is: ", searchAreaCode); return; } } Console.WriteLine("Please enter a 3 digit area code!"); //Console.WriteLine(myAreaCode); //Console.WriteLine(); //Console.ReadKey(true); } } catch (IOException e) { Console.WriteLine(e); } } } }
The errrors:
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(49,33): error CS0019: Operator '<' cannot be applied to operands of type 'string' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(53,38): error CS0019: Operator '>' cannot be applied to operands of type 'string' and 'string[]'
Visual Studio 2008\Projects\sortingarray\sortingarray\Program.cs(57,38): error CS0019: Operator '==' cannot be applied to operands of type 'int' and 'string[]'
•
•
Join Date: Jul 2009
Posts: 938
Reputation:
Solved Threads: 152
0
#8 Oct 25th, 2009
I think you need to read up on using multi-dimensional arrays in C# so you understand what you are doing, but in both of those brackets you need numbers to access the string instead of a string[], which is what it currently resolves to.
Not to discourage you from learning how to access multi-dimensional arrays, but you should consider using a strongly typed List<> instead, if you instructor would approve.
Not to discourage you from learning how to access multi-dimensional arrays, but you should consider using a strongly typed List<> instead, if you instructor would approve.
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
0
#9 Oct 25th, 2009
Type List<> is the very next question. In fact, I got the rest of this assignment done just not this one. This one, I need to read in the file that has area code with state abbreviation. Sort it and then do a binary search to find what the user inputs. I did it using Array.BinarySearch but that is not what the professor wants. I will go back and reread about multi-dimensional arrays since I am so stucked here.
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
0
#10 Oct 25th, 2009
ok, I went back to the basics. I got the sorting done and even the binary search comes up correct. Now I just need the index to show the actual state. Here is the code and it has no errors:
Now it should be easy to show when index is for example 0, to show the actual state that is listed in the array at index 0...
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.IO; namespace sortingarray { class Program { static void Main(string[] args) { String phoneareacode, state, line; AreaCode myAreaCode; bool isNum = false; try { string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv"); string[][] unsortedarray = new string[strings.Length][]; for (int i = 0; i < strings.Length; i++) { unsortedarray[i] = strings[i].Split(','); } Array.Sort(unsortedarray, AreaCode.sortAreaCode()); for (int i = 0; i < unsortedarray.Length; i++) { phoneareacode = unsortedarray[i][0]; state = unsortedarray[i][1]; myAreaCode = new AreaCode(phoneareacode, state); Console.WriteLine(myAreaCode); Console.WriteLine(); // Console.ReadKey(true); } int key; int index; int[] data = new int[unsortedarray.Length]; for (int i = 0; i < data.Length; i++) data[i] = int.Parse(unsortedarray[i][0]); Console.Write("Enter the area code to search: "); key = int.Parse(Console.In.ReadLine()); index = Search(data, key, 0, data.Length - 1); if (index == -1) Console.WriteLine("Area code {0} was not found.", key); else Console.WriteLine("Area code {0} is for this state: {1} ", key, index); } catch (IOException e) { Console.WriteLine(e); } } public static int Search(int[] data, int key, int left, int right) { if (left <= right) { int middle = (left + right) / 2; if (key == data[middle]) return middle; else if (key < data[middle]) return Search(data, key, left, middle - 1); else return Search(data, key, middle + 1, right); } return -1; } } }
Now it should be easy to show when index is for example 0, to show the actual state that is listed in the array at index 0...
![]() |
Similar Threads
- binary search (C)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Binary search tree removal (C++)
- binary search (C++)
- Insertion in a binary search tree (C++)
Other Threads in the C# Forum
- Previous Thread: How to import a Microsoft Access database into mySQL by using c#
- Next Thread: Active Directory - Retrieving Password Expiration Date
| Thread Tools | Search this Thread |
adsense adult advanced advertising api app apple array art article asp.net binary bing blogging book bookmark business consumersearch copyright cuil daniweb data database datalist delete development earth engine enterprisesearch europe exchange facebook failure flash google googlesitesearch googlewave hacking image imagesearch internet internetadvertising iphone ipod key knol legal location login malware marketing mcafee media men microsoft monetization msn multiple mysql news opinion optimization pdf php privacy publishing revenue search searchengine searchwiki security seo serps sex silverlight soap socialnetworking software space spyware string system tree tveyes twitter universe upload videosearch visual_search web webmaster websearch website wiki wolfram wolfram-alpha wolframalpha women yahoo yahoo!







