binary search help

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 11
Reputation: mejohnm is an unknown quantity at this point 
Solved Threads: 0
mejohnm mejohnm is offline Offline
Newbie Poster

binary search help

 
0
  #1
Oct 24th, 2009
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???


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. namespace sortingarray
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. String phoneareacode, state, line;
  15. AreaCode myAreaCode;
  16. bool isNum = false;
  17.  
  18. try
  19. {
  20. string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv");
  21. string[][] unsortedarray = new string[strings.Length][];
  22. for (int i = 0; i < strings.Length; i++)
  23. {
  24. unsortedarray[i] = strings[i].Split(',');
  25. }
  26.  
  27. Array.Sort(unsortedarray, AreaCode.sortAreaCode());
  28.  
  29. for (int i = 0; i < unsortedarray.Length; i++)
  30. {
  31. phoneareacode = unsortedarray[i][0];
  32. state = unsortedarray[i][1];
  33.  
  34. myAreaCode = new AreaCode(phoneareacode, state);
  35. }
  36.  
  37. int searchAreaCode;
  38. string searchString = Console.ReadLine();
  39. isNum = Int32.TryParse(searchString, out searchAreaCode);
  40.  
  41. if (isNum)
  42. {
  43. int lowNum = 0;
  44. int highNum = unsortedarray.Length - 1;
  45.  
  46. while (lowNum <= highNum)
  47. {
  48. int midNum = (lowNum + highNum) / 2;
  49. if (searchAreaCode < unsortedarray[midNum])
  50. {
  51. highNum = midNum - 1;
  52. }
  53. else if (searchAreaCode > unsortedarray[midNum])
  54. {
  55. lowNum = midNum + 1;
  56. }
  57. else if (searchAreaCode = unsortedarray[midNum])
  58. {
  59. Console.WriteLine(" The state for Area Code {0} is: ", searchAreaCode);
  60. return;
  61. }
  62. }
  63.  
  64. Console.WriteLine("Please enter a 3 digit area code!");
  65. //Console.WriteLine(myAreaCode);
  66. //Console.WriteLine();
  67. //Console.ReadKey(true);
  68. }
  69. }
  70. catch (IOException e)
  71. {
  72. Console.WriteLine(e);
  73. }
  74. }
  75. }
  76. }

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'
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,984
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 289
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
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.
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: mejohnm is an unknown quantity at this point 
Solved Threads: 0
mejohnm mejohnm is offline Offline
Newbie Poster
 
0
  #3
Oct 25th, 2009
Originally Posted by ddanbe View Post
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.
I thought you were completely right, but after putting in those corrections, I get the same errors.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,984
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 289
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #4
Oct 25th, 2009
Please show us your updated code. Just from a statement that it is still wrong we can infer little...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: mejohnm is an unknown quantity at this point 
Solved Threads: 0
mejohnm mejohnm is offline Offline
Newbie Poster
 
0
  #5
Oct 25th, 2009
Yea, you are right. Here is the updated code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. namespace sortingarray
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. String phoneareacode, state, line;
  15. AreaCode myAreaCode;
  16. bool isNum = false;
  17.  
  18. try
  19. {
  20. string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv");
  21. string[][] unsortedarray = new string[strings.Length][];
  22. for (int i = 0; i < strings.Length; i++)
  23. {
  24. unsortedarray[i] = strings[i].Split(',');
  25. }
  26.  
  27. Array.Sort(unsortedarray, AreaCode.sortAreaCode());
  28.  
  29. for (int i = 0; i < unsortedarray.Length; i++)
  30. {
  31. phoneareacode = unsortedarray[i][0];
  32. state = unsortedarray[i][1];
  33.  
  34. myAreaCode = new AreaCode(phoneareacode, state);
  35. }
  36.  
  37. int searchAreaCode;
  38. string searchString = Console.ReadLine();
  39. isNum = Int32.TryParse(searchString, out searchAreaCode);
  40.  
  41. if (isNum)
  42. {
  43. int lowNum = 0;
  44. int highNum = unsortedarray.Length - 1;
  45.  
  46. while (lowNum <= highNum)
  47. {
  48. int midNum = (lowNum + highNum) / 2;
  49. if (searchString < unsortedarray[midNum])
  50. {
  51. highNum = midNum - 1;
  52. }
  53. else if (searchString > unsortedarray[midNum])
  54. {
  55. lowNum = midNum + 1;
  56. }
  57. else if (searchAreaCode == unsortedarray[midNum])
  58. {
  59. Console.WriteLine(" The state for Area Code {0} is: ", searchAreaCode);
  60. return;
  61. }
  62. }
  63.  
  64. Console.WriteLine("Please enter a 3 digit area code!");
  65. //Console.WriteLine(myAreaCode);
  66. //Console.WriteLine();
  67. //Console.ReadKey(true);
  68. }
  69. }
  70. catch (IOException e)
  71. {
  72. Console.WriteLine(e);
  73. }
  74. }
  75. }
  76. }

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[]'
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 938
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 152
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #6
Oct 25th, 2009
Your unsortedarray is two dimensions, but you are comparing it as though it were only one. Look at line 31 and see how you accessing areacode, then look at your if statement at line 49--see the difference?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: mejohnm is an unknown quantity at this point 
Solved Threads: 0
mejohnm mejohnm is offline Offline
Newbie Poster
 
0
  #7
Oct 25th, 2009
So I should have for line 49 something like unsortedarray[][]?

edit: I can see that will not work either.
Last edited by mejohnm; Oct 25th, 2009 at 5:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 938
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 152
DdoubleD DdoubleD is offline Offline
Posting Shark
 
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: mejohnm is an unknown quantity at this point 
Solved Threads: 0
mejohnm mejohnm is offline Offline
Newbie Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: mejohnm is an unknown quantity at this point 
Solved Threads: 0
mejohnm mejohnm is offline Offline
Newbie Poster
 
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:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. namespace sortingarray
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. String phoneareacode, state, line;
  15. AreaCode myAreaCode;
  16. bool isNum = false;
  17.  
  18. try
  19. {
  20. string[] strings = File.ReadAllLines("C:/CS280files/AreaCodes.csv");
  21. string[][] unsortedarray = new string[strings.Length][];
  22. for (int i = 0; i < strings.Length; i++)
  23. {
  24. unsortedarray[i] = strings[i].Split(',');
  25. }
  26.  
  27. Array.Sort(unsortedarray, AreaCode.sortAreaCode());
  28.  
  29. for (int i = 0; i < unsortedarray.Length; i++)
  30. {
  31. phoneareacode = unsortedarray[i][0];
  32. state = unsortedarray[i][1];
  33.  
  34. myAreaCode = new AreaCode(phoneareacode, state);
  35.  
  36. Console.WriteLine(myAreaCode);
  37. Console.WriteLine();
  38. // Console.ReadKey(true);
  39. }
  40.  
  41. int key;
  42. int index;
  43. int[] data = new int[unsortedarray.Length];
  44. for (int i = 0; i < data.Length; i++)
  45. data[i] = int.Parse(unsortedarray[i][0]);
  46. Console.Write("Enter the area code to search: ");
  47. key = int.Parse(Console.In.ReadLine());
  48. index = Search(data, key, 0, data.Length - 1);
  49. if (index == -1)
  50. Console.WriteLine("Area code {0} was not found.", key);
  51. else
  52.  
  53. Console.WriteLine("Area code {0} is for this state: {1} ", key, index);
  54.  
  55. }
  56. catch (IOException e)
  57. {
  58. Console.WriteLine(e);
  59. }
  60. }
  61.  
  62. public static int Search(int[] data, int key, int left, int right)
  63. {
  64. if (left <= right)
  65. {
  66. int middle = (left + right) / 2;
  67. if (key == data[middle])
  68. return middle;
  69. else if (key < data[middle])
  70. return Search(data, key, left, middle - 1);
  71. else
  72. return Search(data, key, middle + 1, right);
  73. }
  74. return -1;
  75. }
  76. }
  77. }

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...
Reply With Quote Quick reply to this message  
Reply

Tags
search

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC