In: Computer Science
(Gaddis) Programming Challenges 8. Search Function for Customer Accounts Program Program pág. 653, Cap. 11
Instruction: Add the exercise 1 to the program provided.
.1. Search Function for Customer Accounts Program Add a function to Programming Challenge 7 that allows the user to search the structure array for a particular customer’s account. It should accept part of the customer’s name as an argument and then search for an account with a name that matches it. All accounts that match should be displayed. If no account matches, a message saying so should be displayed.
#include<iostream>
using namespace::std;
C++ Program
#include"ArrayBag.h"
int main(){
ArrayBag<int> myArrayInteger;
myArrayInteger.add(5);
myArrayInteger.add(3);
myArrayInteger.add(5);
myArrayInteger.add(10);
myArrayInteger.display();
myArrayInteger.remove(5);
myArrayInteger.display();
myArrayInteger.add(3);
myArrayInteger.add(3);
cout << "El nuemo 3 esta "
<<myArrayInteger.getFrequencyOf(3)
<< "veces repetido en el arreglo\n";
if (myArrayInteger.contains(7)){
cout <"El numero 7 esta dentro del arreglo\n";
}
else{
cout<<"El numero 7 no esta dentro del arreglo\n";
}
system("pause");
return 0;
}//end main
#include <iostream>
#define MAX 10
using namespace std;
template<class T>
// Defines a template class ArrayBag
class ArrayBag
{
// Pointer type template to store data
T *data;
// To store the position
int index;
public:
// Default constructor
ArrayBag()
{
// Dynamically allocate memory of size MAX
data = (T*)new T[MAX];
// Initializes index to 0
index = 0;
}
// Function to add parameter value to array bag
void add(T value)
{
if(index == MAX)
cout<<"\n ERROR: Cannot add more element. Reached
maximum.";
else
data[index++] = value;
}
// Function to delete parameter value from array bag
void remove(T value)
{
int pos = -1;
// Loops till number of elements in the list
for(int c = 0; c < index; c++)
{
// Checks if current index position data is equals to parameter
data
if(data[c] == value)
{
// Assigns the found index position
pos = c;
// Come out of the loop
break;
}
}
// Loops from found index position to end minus one times
for(int c = pos; c < index - 1; c++)
// Shift each data to one position left
data[c] = data[c + 1];
// Checks if found position is -1 then not found
if(pos == -1)
cout<<"\n ERROR: No such element "<<value<<"
found.";
// Otherwise found reduce the index counter by one
else
index--;
}
// Function to display array bag contents
void display()
{
cout<<"\n\n List contents: ";
// Loops till number of elements in the list
for(int c = 0; c < index; c++)
// Displays each element
cout<<data[c]<<" ";
cout<<endl;
}
// Function to return true if parameter value is available in
array bag
// Otherwise returns false
bool contains(T value)
{
int pos = -1;
// Loops till number of elements in the list
for(int c = 0; c < index; c++)
// Checks if current index position data is equals to parameter
data
if(data[c] == value)
// Returns true for found
return true;
// Returns false for not found
return false;
}
// Function to return number of times parameter value available
in array bag
int getFrequencyOf(T value)
{
// Variable to store frequency
int counter = 0;
// Loops till number of elements in the list
for(int c = 0; c < index; c++)
// Checks if current index position data is equals to parameter
data
if(data[c] == value)
// Increase the counter by one
counter++;
return counter;
}
};// End of class
---------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include"ArrayBag.h"
using namespace std;
// main function definition
int main()
{
// Creates an object using parameterized constructor of type
int
ArrayBag<int> myArrayInteger;
// Calls the function to add element
myArrayInteger.add(5);
myArrayInteger.add(3);
myArrayInteger.add(5);
myArrayInteger.add(10);
// Calls the function to display
myArrayInteger.display();
// Calls the function to remove element
myArrayInteger.remove(5);
// Calls the function to display
myArrayInteger.display();
// Calls the function to add element
myArrayInteger.add(3);
myArrayInteger.add(3);
// Calls the function to display
myArrayInteger.display();
// Calls the function to display frequency
cout << "Element 3 available: "
<<myArrayInteger.getFrequencyOf(3)
<< " times in array bag\n";
// Calls the function to check availability
if (myArrayInteger.contains(7))
{
cout <<"Element 7 available in array bag\n";
}
else
{
cout<<"Element 7 not available in array bag\n";
}
system("pause");
return 0;
}//end main
Sample Output:
List contents: 5 3 5 10
List contents: 3 5 10
List contents: 3 5 10 3 3
Element 3 available: 3 times in array bag
Element 7 available in list
Press any key to continue . . .