#include <string> #include <iostream> using namespace std; struct Address { unsi
ID: 3748822 • Letter: #
Question
#include <string>
#include <iostream>
using namespace std;
struct Address {
unsigned number;
string street;
string suffix;
string city;
string state;
unsigned zip;
};
struct Time {
unsigned day;
bool pm;
unsigned hour;
unsigned minute;
};
void show(const Address & address);
void show(const Address address[], unsigned elements, unsigned desiredZip);
void show(const Address address[], unsigned addressElements, const unsigned desiredZip[], unsigned desiredZipElements);
void show(const Time & time);
bool ok(const Time & time);
int compare(const Time& time0, const Time& time1);
void input(Time& time);
*
*
Output all the addresses in address whose zip code matches desiredZip. Call the other show function to output the
individual addresses. Write a bunch of hyphens after each address to visually separate them. For instance, if address
were an array of 10000 addresses, 3 of which were in in the 91371 zip code, and desiredZip were 91371, then thefunction might output
1234 Winnetka Avenue
Woodland Hills CA 91371
-------------------------------------------
4792 Ventura Boulevard
Woodland Hills CA 91371
-------------------------------------------
2001 Porter Ranch Road
Canoga Park CA 91371
Output all the addresses in address whose zip code matches any of the zip codes in desiredZip. Call the previous show
function to do the output for each zip code. For instance, if desiredZip contained the two elements 91371 and 91375,
then this show function might. output Display the time,
Explanation / Answer
#include <string>
#include <iostream>
#define MAX 10
using namespace std;
// Structure definition for Address
struct Address
{
// Data member to store address data
unsigned number;
string street;
string suffix;
string city;
string state;
unsigned zip;
};// End of structure
// Structure definition for Time
struct Time
{
// Data member to store time data
unsigned day;
bool pm;
unsigned hour;
unsigned minute;
};// End of structure
// Function to show a address
void show(const Address &address)
{
cout<<" ------------------------------------- ";
// Displays a address
cout<<address.number<<" "<<address.street<<" "<<address.suffix<<endl;
cout<<address.city<<" "<<address.state<<" "<<address.zip;
cout<<" ------------------------------------- ";
}// End of function
// Function to show a selected address based on zip
void show(const Address address[], unsigned elements, unsigned desiredZip)
{
// Initializes found status to -1 for not found
int found = -1;
// Loops till number of addresses
for(int x = 0; x < elements; x++)
{
// Checks if current zip is equals to parameter zip
if(address[x].zip == desiredZip)
{
// Updates the found status
found = x;
// Calls the function to show the address
show(address[x]);
}// End of if condition
}// End of for loop
// Checks if found status is -1 then record not found
if(found == -1)
cout<<" Address for zip "<<desiredZip<<" not found";
}// End of function
// Function to display address based on the array of zips
void show(const Address address[], unsigned addressElements, const unsigned desiredZip[], unsigned desiredZipElements)
{
// To store found status
int found;
// Loops till number of zip codes
for(int x = 0; x < desiredZipElements; x++)
{
// Sets the found status to -1 for not found
found = -1;
// Loops till number of addresses
for(int y = 0; y < addressElements; y++)
{
// Checks if current zip is equals to parameter current zip
if(address[y].zip == desiredZip[x])
{
// Updates the found status
found = y;
// Calls the function to show the address
show(address[y]);
}// End of if condition
}// End of inner for loop
if(found == -1)
cout<<" Address for zip "<<desiredZip[x]<<" not found";
}// End of outer for loop
}// End of function
// Function to display time information
void show(const Time & time)
{
// Displays the time
cout<<" Day: "<<time.day<<" "<<time.hour<<" : "<<time.minute;
// Checks if pm value is true then display "pm"
if(time.pm == true)
cout<<" pm";
// Otherwise display am
else
cout<<" am";
}// End of function
// Function to return 1 if both the time is equal otherwise returns 0
int compare(const Time& time0, const Time& time1)
{
// Checks time0 and time1 all the data members
// If equal then return 1
if(time0.day == time1.day && time0.hour == time1.hour && time0.minute == time1.minute && time0.pm == time1.pm)
return 1;
// Otherwise return 0
else
return 0;
}// End of function
// Function to accept time information
void input(Time& time)
{
// To store am or pm
string ampm;
// Accepts time data
cout<<" Enter day: ";
cin>>time.day;
cout<<"Enter hour: ";
cin>>time.hour;
cout<<"Enter minute: ";
cin>>time.minute;
cout<<"Enter am / pm: ";
cin>>ampm;
// Checks if entered data is am store false
if(ampm.compare("am"))
time.pm = false;
// Otherwise stores true
else
time.pm = true;
}// End of function
// Accepts address information
void input(Address& Address)
{
cout<<" Enter number: ";
cin>>Address.number;
cout<<"Enter street: ";
cin>>Address.street;
cout<<"Enter suffix: ";
cin>>Address.suffix;
cout<<"Enter city: ";
cin>>Address.city;
cout<<"Enter state: ";
cin>>Address.state;
cout<<"Enter zip: ";
cin>>Address.zip;
}// End of function
// main function definition
int main()
{
// Declares Address class array of object of size MAX
Address address[MAX];
// Declares Time class array of object of size MAX
Time time[MAX];
// To store the zip code to search
unsigned zip;
// To store the zip codes to search
unsigned zipArr[MAX];
// To store the time to search
Time temp;
// To store number of address, times
int noAdd, noTime, no;
// Accepts number of address
cout<<" Enter how many address you want to enter (Maximum 10): ";
cin>>noAdd;
// Loops till number of address
for(int x = 0; x < noAdd; x++)
// Calls the function accept data address for x index position
input(address[x]);
// Accepts number of times
cout<<" Enter how many time you want to enter (Maximum 10): ";
cin>>noTime;
// Loops till number of times
for(int x = 0; x < noTime; x++)
// Calls the function accept time for x index position
input(time[x]);
// Accepts zip to search
cout<<" Enter the zip code to show: ";
cin>>zip;
// Calls the function to search a zip code and display address information
show(address, noAdd, zip);
// Accepts number of zip codes to search
cout<<" Enter how many zip codes to search: ";
cin>>no;
// Loops to accept number of zip codes
for(int x = 0; x < no; x++)
cin>>zipArr[x];
// Calls the function to search a zip codes and display addresses
show(address, noAdd, zipArr, no);
// Loops till number of times
for(int x = 0; x < noTime; x++)
// Calls the function to display time
show(time[x]);
// Accepts a time
cout<<" Enter a time for availability: ";
input(temp);
// Sets the found status to -1 for not found
int found = -1;
// Loops till number of times
for(int x = 0; x < noTime; x++)
{
// Calls the function to checks both the times are equals to not
if(compare(temp, time[x]) == 1)
{
// Updates the found status
found = x;
// Calls the function to display found time information
show(time[x]);
}// End of if condition
}// End of for loop
// Checks if found status is -1 then record not found
if(found == -1)
cout<<" Time not found.";
}// End of main function
Sample Output:
Enter how many address you want to enter (Maximum 10): 4
Enter number: 1111
Enter street: sun
Enter suffix: mon
Enter city: Bam
Enter state: Ori
Enter zip: 112233
Enter number: 2222
Enter street: ran
Enter suffix: ban
Enter city: Bbsr
Enter state: Ori
Enter zip: 223311
Enter number: 3333
Enter street: dan
Enter suffix: nad
Enter city: Rkl
Enter state: Ori
Enter zip: 112233
Enter number: 4444
Enter street: mon
Enter suffix: nom
Enter city: Den
Enter state: Bihar
Enter zip: 332211
Enter how many time you want to enter (Maximum 10): 2
Enter day: 1
Enter hour: 12
Enter minute: 3
Enter am / pm: am
Enter day: 2
Enter hour: 11
Enter minute: 40
Enter am / pm: pm
Enter the zip code to show: 112233
-------------------------------------
1111 sun mon
Bam Ori 112233
-------------------------------------
-------------------------------------
3333 dan nad
Rkl Ori 112233
-------------------------------------
Enter how many zip codes to search: 2
112233
332211
-------------------------------------
1111 sun mon
Bam Ori 112233
-------------------------------------
-------------------------------------
3333 dan nad
Rkl Ori 112233
-------------------------------------
-------------------------------------
4444 mon nom
Den Bihar 332211
-------------------------------------
Day: 1
12 : 3pm
Day: 2
11 : 40am
Enter a time for availability:
Enter day: 5
Enter hour: 12
Enter minute: 3
Enter am / pm: pm
Time not found.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.