Question

In: Computer Science

(Gaddis) Programming Challenges 8. Search Function for Customer Accounts Program Program pág. 653, Cap. 11 Instruction:...

(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

Solutions

Expert Solution

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


Related Solutions

Starting out with Control Structures: Program challenges in C++ by Tony Gaddis This problem was adapted...
Starting out with Control Structures: Program challenges in C++ by Tony Gaddis This problem was adapted from questions 9 and 10 on page 661 Write a program that keeps track of a speakers’ bureau. The program should use a structure to store the following data about a speaker: Name, Telephone Number, Speaking Topic, Fee Required. The program should use a vector of structures. It should let the user enter data into the vector, change the contents of any element, and...
1. Chapter 5, Programming Challenge #8, Conversion Program (page 314). Program Name: FinalExamConversion. Write a program...
1. Chapter 5, Programming Challenge #8, Conversion Program (page 314). Program Name: FinalExamConversion. Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selection: 1. Convert to Kilometers 2. Convert to Inches 3. Convert to Feet 4. Quit the Program The program will convert the distance to kilometers, inches or feet, depending on the user’s selection. Write the following methods: • getInput: This method prompts user to enter...
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that asks for the name of the item and the price of each item, and then displays these on the screen. Refer to the print() and input() statements covered in the module. Do NOT use loops as it will be covered in later modules. Apples 2.10 Hamburger 3.25 Milk 3.49 Sugar 1.99 Bread 1.76 Deli Turkey 7.99 Pickles 3.42 Butter 2.79
11.7: Customer Accounts Write a program that uses a structure to store the following data about...
11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Prompts And...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However,...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects...
Create a program/function using PYTHON that takes cents and returns to the customer how MANY coins...
Create a program/function using PYTHON that takes cents and returns to the customer how MANY coins it takes to make the change... Ex. if the change owed is 50 cents then return a 2 (for two quarters) if the change owed is 10 cents then return a 1 (for one dime) AGAIN please write this in java and please provide EXPLANATION of answer
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays the number of days in each month. The program’s output should be similar to this: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has 31 days. June has 30 days. July has 31 days. August has 31 days. September has 30 days. October has 31 days. November has 30 days. December has 31 days. The...
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
8-11 Outsourcing (LO 3) Merit Bay Communications operates a customer call center that handles billing inquiries...
8-11 Outsourcing (LO 3) Merit Bay Communications operates a customer call center that handles billing inquiries for several large insurance firms. Since the center is located on the outskirts of town, where there are no restaurants within a 20-minute drive, the company has always operated an on-site cafeteria for employees. The cafeteria uses $180,000 in food products each year and serves 5,000 meals per month, at a price of $5 each. It employs five workers whose salaries and benefits total...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT