Template<class T> to partially fill array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 8
Reputation: edman is an unknown quantity at this point 
Solved Threads: 0
edman edman is offline Offline
Newbie Poster

Template<class T> to partially fill array

 
0
  #1
Jul 17th, 2008
I need to create a function that uses a template to partially fill any array and from there search for a value in the array and return its position if found. The program below finds the values for double and int but I do know how to partially fill the array with string or char values. Could someone please help.

  1. //This is the file SearchArrayValue.cpp
  2. //Searches a partially filled array for a number and returns the position of the number if found and
  3. //-1 if not
  4. #include <iostream>
  5. const int SIZE_DECLARED = 10;
  6.  
  7. template<class T>
  8. void fill_array(T values[], int size, int& number_used);
  9. //Pre condition:
  10. //Post condition: number_used is the number of elements used in the array
  11. //The array is filled with non negative number from values[0] to values[number_used - 1]
  12. //Values will be entered from the keyboard
  13.  
  14. template<class T>
  15. T search_array(const T values[], int number_used, T search_value);
  16. //Pre condition: array values is filled with <= number_used values
  17. //Post condition: search_value will be searched in the array and if found the position will be returned
  18. //otherwise -1 will be returned
  19.  
  20. template<class T>
  21. void display_result(T search_value, int search_result);
  22. //Pre condition:
  23. //Post Condition: The position of the searched item in the array is displayed if it was found
  24. //otherwise the appropriate message is returned.
  25.  
  26. using namespace std;
  27. int main()
  28. {
  29. int number_used;
  30. int int_values[SIZE_DECLARED];
  31. double double_values[SIZE_DECLARED];
  32. char char_values[SIZE_DECLARED];
  33. fill_array(int_values, SIZE_DECLARED, number_used);
  34. fill_array(double_values, SIZE_DECLARED, number_used);
  35. fill_array(char_values, SIZE_DECLARED, number_used);
  36.  
  37.  
  38. cin.get();//To keep console window open
  39. cin.get();//To keep console window open
  40. return 0;
  41. }
  42.  
  43. template<class T>
  44. void fill_array(T values[], int size, int& number_used)
  45. {
  46. T next, search_value;
  47. int index = 0, search_result;
  48. char limit = '.';
  49. cout << "Enter a maximum of " << size << " non negative values: \n";
  50. cout << "(Enter a 0 to stop entry) \n";
  51. cin >> next;
  52. while ((next >= 0)&&(index < size))//||((next != limit)&&(index < size))
  53. {
  54. values[index] = next;
  55. index++;
  56. cin >> next;
  57. }
  58. number_used = index;
  59.  
  60. cout << "Enter number/char to search for: ";
  61. cin >> search_value;
  62. search_result = search_array(values, number_used, search_value);
  63. display_result(search_value, search_result);
  64. }
  65. template<class T>
  66. T search_array(const T values[], int number_used, T search_value)
  67. {
  68. int index = 0;
  69. bool found = false;
  70. while ((!found) && (index < number_used))
  71. if (search_value == values[index])
  72. found = true;
  73. else
  74. index++;
  75.  
  76. if (found)
  77. return index;
  78. else
  79. return -1;
  80. }
  81.  
  82. template<class T>
  83. void display_result(T search_value, int search_result)
  84. {
  85. if (search_result == -1)
  86. cout << "The number " << search_value << " is not in the list" << endl << endl;
  87. else
  88. cout << "The number " << search_value << " was first found at values["
  89. << search_result << "] of the array" << endl << endl;
  90. }
Last edited by edman; Jul 17th, 2008 at 4:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Template<class T> to partially fill array

 
0
  #2
Jul 17th, 2008
Ah, I see the problem.

On line 52 you are assuming something about the input with (next >= 0). (According to the instructions it should be (next > 0).)

If you wish to use a semaphore item to terminate input, it must be passed as argument to the function.

However, a better idiom is to forget the semaphore stuff. Users consider it unnatural. Rather, accept a blank line as end of input. For this you'll need to #include <string> and <sstream>.
  1. #include <iostream>
  2. #include <limits>
  3. #include <sstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. ...
  8.  
  9. template<class T>
  10. void fill_array(T values[], int size, int& number_used, const string& itemname );
  11.  
  12. ...
  13.  
  14. int main()
  15. {
  16. ...
  17.  
  18. fill_array(int_values, SIZE_DECLARED, number_used, "integer");
  19. fill_array(double_values, SIZE_DECLARED, number_used, "floating-point number");
  20. fill_array(char_values, SIZE_DECLARED, number_used, "character");
  21.  
  22. ...
  23. }
  24.  
  25. template<class T>
  26. void fill_array(T values[], int size, int& number_used, const string& itemname )
  27. {
  28. T next, search_value;
  29. int index = 0, search_result;
  30. cout << "Enter a maximum of " << size << ' ' << itemname << "s: \n";
  31. cout << "Press ENTER on a blank line to end.\n";
  32.  
  33. string s;
  34. while (getline( cin, s ) && !s.empty() && (index < size))
  35. {
  36. stringstream ss( s );
  37. while ((ss >> next) && (index < size))
  38. {
  39. values[index] = next;
  40. index++;
  41. }
  42. }
  43. number_used = index;
  44.  
  45. cout << "Enter a " << itemname << " to search for: ";
  46. cin >> search_value;
  47. cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  48. search_result = search_array(values, number_used, search_value);
  49. display_result(search_value, search_result);
  50. }

There are a few other things I dislike (like accessing cin and cout directly in a template function, or having the entry function call the search and display functions...), but those aren't critical (atm).

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 8
Reputation: edman is an unknown quantity at this point 
Solved Threads: 0
edman edman is offline Offline
Newbie Poster

Re: Template<class T> to partially fill array

 
0
  #3
Jul 17th, 2008
Thank you Duoas. I will try it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC