User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,721 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,584 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 196 | Replies: 2
Reply
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation: edman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
edman edman is offline Offline
Newbie Poster

Template<class T> to partially fill array

  #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.

//This is the file SearchArrayValue.cpp
//Searches a partially filled array for a number and returns the position of the number if found and
//-1 if not
#include <iostream>
const int SIZE_DECLARED = 10;

template<class T>
void fill_array(T values[], int size, int& number_used);
//Pre condition: 
//Post condition: number_used is the number of elements used in the array
//The array is filled with non negative number from values[0] to values[number_used - 1]
//Values will be entered from the keyboard

template<class T>
T search_array(const T values[], int number_used, T search_value);
//Pre condition: array values is filled with <= number_used values
//Post condition: search_value will be searched in the array and if found the position will be returned
//otherwise -1 will be returned

template<class T>
void display_result(T search_value, int search_result);
//Pre condition:
//Post Condition: The position of the searched item in the array is displayed if it was found
//otherwise the appropriate message is returned.

using namespace std;
int main()
{
	int number_used;
	int int_values[SIZE_DECLARED];
	double double_values[SIZE_DECLARED];
	char char_values[SIZE_DECLARED];
	fill_array(int_values, SIZE_DECLARED, number_used);
	fill_array(double_values, SIZE_DECLARED, number_used);
	fill_array(char_values, SIZE_DECLARED, number_used);
	
	
	cin.get();//To keep console window open
	cin.get();//To keep console window open
	return 0;
}

template<class T>
void fill_array(T values[], int size, int& number_used)
{
	T next, search_value;
	int index = 0, search_result;
	char limit = '.';
	cout << "Enter a maximum of " << size << " non negative values: \n";
	cout << "(Enter a 0 to stop entry) \n";
	cin >> next;
	while ((next >= 0)&&(index < size))//||((next != limit)&&(index < size))
	{
		values[index] = next;
		index++;
		cin >> next;
	}
	number_used = index;

	cout << "Enter number/char to search for: ";
	cin >> search_value;
	search_result = search_array(values, number_used, search_value);
	display_result(search_value, search_result);
}
template<class T>
T search_array(const T values[], int number_used, T search_value)
{
	int index = 0;
	bool found = false;
	while ((!found) && (index < number_used))
		if (search_value == values[index])
			found = true;
		else
			index++;

	if (found)
		return index;
	else
		return -1;
}

template<class T>
void display_result(T search_value, int search_result)
{
	if (search_result == -1)
		cout << "The number " << search_value << " is not in the list" << endl << endl;
	else
		cout << "The number " << search_value << " was first found at values["
		<< search_result << "] of the array" << endl << endl;
}
Last edited by edman : Jul 17th, 2008 at 3:31 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,825
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 187
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Template<class T> to partially fill array

  #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  
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation: edman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
edman edman is offline Offline
Newbie Poster

Re: Template<class T> to partially fill array

  #3  
Jul 17th, 2008
Thank you Duoas. I will try it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC